I want to arrange the number 1 - 12 like shown below. like the numbers on the clock.

12
          11     1
       10           2
     9                3
       8            4
          7      5
              6

arrived with the following code:

#include<stdio.h>
int main(){
	int e = 11;
	int count = 10;
	int col = 10, sp3 = 4;
	printf("              12\n");
	for(int i = e; i > 8; i--){
		for(int sp = col; sp >=0; sp--){
			printf(" ", sp);
		}
		printf("%d", i);
		for(int sp2 = 0; sp2 <= sp3; sp2++){
			printf(" ");
		}
		col--;
		sp3++; sp3++;
		printf("%d\n",i-count);
		count--; count--;
	}
	for(int i = 8; i >= 7; i--){
		for(int sp = -2; sp <=col; sp++){
			printf(" ", sp);
		}
		printf("%d", i);
		for(int sp2 = sp3; sp2 >= 4; sp2--){
			printf(" ");
		}
		col++;
		sp3--; sp3--;
		printf("%d\n",i-count);
		count--; count--;
	}
	printf("              6\n");
	return 0;
}

out put i got is like this : [IMG]http://a.imagehost.org/t/0821/Screenshot-2.jpg[/IMG]

this code is good/bad/ugly. comments please.

Recommended Answers

All 9 Replies

Obvious errors.


If you want a nice round clock face, you need to work with a graphic mode console, not a text one, and then use math.h to calculate the number of degree's or radians needed between numbers.

If you want a square one like the red one at the top of your post, you can make that, using the text based console, if you work it out right.

What is the problem with getting the output to look like the squared off red numbered one? Is that what you want, or do you want a round (normal) clock face?

the output should look like the diamond clock posted above. the problem is getting the spacing right between the double digit number 12, 11, 10 and 1, 2 ,3. so just a little bit of alignment do u say?


by graphic mode console did u mean the turbo c environment or some gui environment? if not do linux have the console you are talking about?

ps: arranging the numbers as in the clock is the only requirement of this assignment. the requirements does not ask for a full functioning clock.

For a square clock (on edge), just keep everything lined up, and allow for ALL the numbers, to have 2 digits, even if they only have one:

As far as a squared off clock face goes I prefer this one:

12
            11    1
         10          2
       9               3
         8           4  
            7     5
               6

over the original. The gap between the 6 and 7 is more uniform.

12
          11     1
       10           2
     9                3
       8            4
          7      5
              6

A graphics console is a graphics console, whether in Turbo C or not. The problem becomes the resolution of that graphics console - in Turbo C, the resolution will be rather low, at best, because the graphics drivers available for TC, were all much lower than what we have today.

Still, it's much better than we can do in a text console! ;)

But this looks like it was meant to be done in a text console, and it's a an exercise in using logic, not graphics commands.

So, I'll look at your code, and see what's up with it. Back in an hour, real life stuff to attend to right now.

I have noticed your code uses very little logic, and lots of "magic numbers". Those are numbers just added into code that seem to have come from the sky or by magic. Let's use variables, and logic, and as few magic numbers as possible.

as far as the double digits are concerned, i tried the following code with great doubt to print 1 as 01. but the out put was 1 and 2 only.

#include<stdio.h>
int main(){
	int n = 01;
	n++;
	printf("%d", 01);
	printf("\n%d", n);
	return 0;
}

inventing any other complex logic for printing 01 i guess will be extra work for the assignment and not required.

come back soon.

After working on this problem, I like your code much better than I did before. ;)

Seriously.

I did mine with two while loops, but in my haste, the logic is not as elegant as it could be. For a clock face, I can accept it, though. :D Everything lines up.

#include <stdio.h>

int main() {
  int i, j, median, offset, rows, n;
  n = 12;
  median = 15;
  offset = 3;
  rows = 7;
  printf("\n\n");

  i=0;
  while(i<4) {         //handles the top half of the clock face
    for(j=0;j<median-offset;j++) { 
      putchar(' ');    //prints left margin spaces
    }
    printf("%2d", n);    //then the number
    if(i==0) {
      putchar('\n');
      --n;           //get ready adjustments
      ++i;
      offset+=3;
      continue;
    }
    for(j=0;j<2*(offset-4);j++) {  
      putchar(' ');  //prints center spaces on clock face
    }
    printf("%2d\n", i);   //and the second number of the row
    offset+=3;      //get ready for the next loop
    --n;
    ++i;
  }//end of while

  offset -=rows-1;
  while(i<rows) {      //handles the lower half
    for(j=0;j<median-offset;j++) {
       putchar(' ');   //first the spaces on the left
    }
    printf("%2d", n);  //then the number
    if(i==rows-1)
      break;
    for(j=0;j<2*(offset-4);j++) { 
      putchar(' ');    //then center spaces
    }
    printf("%2d\n", i); //and the second number of the row
       
    offset-=3;         //and adjust for the next loop
    ++i;
    --n;
  }
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); 
  return 0;
}

@Rje7, I'm getting beat up by the sandman, so I have to hit the sack. I couldn't work with your program because you have your variables declared all over the function - my compiler won't accept that.

By printing two numbers wide, I meant in a field 2 digits wide, like so:

printf("%2d", 8);

where 8 could be any single or double digit number. That's a neat trick to know for all kinds of formatting output, btw - even works for files.

I'll check back tomorrow. Hit me up if you have any questions.

just formatted the code for c89 compliance.

#include<stdio.h>
int main(){
	int e = 11;
	int count = 10;
	int col = 10, sp3 = 4;
	int i, sp2, sp;
	printf("              12\n");
	for(i = e; i > 8; i--){
		for(sp = col; sp >=0; sp--){
			printf(" ", sp);
		}
		printf("%d", i);
		for(sp2 = 0; sp2 <= sp3; sp2++){
			printf(" ");
		}
		col--;
		sp3++; sp3++;
		printf("%d\n",i-count);
		count--; count--;
	}
	for( i = 8; i >= 7; i--){
		for( sp = -2; sp <=col; sp++){
			printf(" ", sp);
		}
		printf("%d", i);
		for( sp2 = sp3; sp2 >= 4; sp2--){
			printf(" ");
		}
		col++;
		sp3--; sp3--;
		printf("%d\n",i-count);
		count--; count--;
	}
	printf("              6\n");
	return 0;
}

Surprised to still be up, but RL is odd that way.

I don't like the 12 and 6 printing - you can't easily move them to another column on the screen.

If you want your numbers to line up, you should use %2d, instead of %d, with ALL the numbers being printed. Right now, your picture shows the 11 overlapping the 0 in the 10, and 11 is also too far away from the 12. The one is too far away from the 12, also.

The two and the three are in the same column, so move the 3 to the right.

The six is too far away from the seven, and also from the five. You can space it out right without the %2d format, but it is MUCH more difficult.

Make that change and see what it looks like. Run my version, and you should be able to see the differences in spaces (if any exist then), very quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.