#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Exclamation Drawing number patterns


    Hello guys,
    I trying to learn C by myself and I would like to get some help
    writing the following program using loops so I could learn from it. (my friend sent it to me from his school)

    The program asks the user to print non-negative Integer number up to 9 digits in length and print it on Screen like the photo illustrated above.
    It should look like in the picture (as for the direction), but the shape and size can improvise. In addition the digits height should be between 15 to 50 chars.
    If the user doesn't input good number (more than 9 digits, a negative one or not integer) it will ask him again until he will…

    Thanks!!

    s18.postimg.org/7u3ezjm2x/numbers.png
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,577
    Rep Power
    1906
    What have you tried? Please post your current code using the code tags.

    Any specific thing you need help with? user input? creating loops? outputting the number?

    Just to have it clarified: do you want to lean "the basic" C or do you want to lean C++?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by MrFujin
    What have you tried? Please post your current code using the code tags.

    Any specific thing you need help with? user input? creating loops? outputting the number?

    Just to have it clarified: do you want to lean "the basic" C or do you want to lean C++?
    Hi MrFujin,
    1. I would like to start from the basics - C.
    2. I don't have problem to get the input from the user, but I have no Idea where to start from with the outputting the number , I was thinking about using switch-case, but I'm not sure its the best way to go with it.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Think of some old tech. Like text-mode computer displays. Each character displayed was made out of dots arranged in a rectangular grid x dots wide by y dots high; that would include the inter-character space after the character and the inter-line spacing below (which also allowed for descending characters). The CRT's electron gun (actually a battery of three guns if you had a color monitor) would scan left to right (relative to the user) one line at a time (assume non-interleaved) from top to bottom. Each character's dot pattern was stored in ROM (read-only memory), such that the character's ASCII code got you to the pattern where it was sub-addressed by line.

    So to print out a line of text, when the gun started scanning the top line, you'd access the first character's first (top) line, then the second character's first line, etc. Then on the next scan, it'd be the first character's second line, then the second character's second line, etc. And the same for the third line, the fourth line, etc, until all lines of that line of text had been scanned, then you'd start on the next line of text.

    Do you see how this approach would work for your problem? To define the dot patterns for your numerals, get some graph paper and draw them out on that.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by dwise1_aol
    Think of some old tech. Like text-mode computer displays. Each character displayed was made out of dots arranged in a rectangular grid x dots wide by y dots high; that would include the inter-character space after the character and the inter-line spacing below (which also allowed for descending characters). The CRT's electron gun (actually a battery of three guns if you had a color monitor) would scan left to right (relative to the user) one line at a time (assume non-interleaved) from top to bottom. Each character's dot pattern was stored in ROM (read-only memory), such that the character's ASCII code got you to the pattern where it was sub-addressed by line.

    So to print out a line of text, when the gun started scanning the top line, you'd access the first character's first (top) line, then the second character's first line, etc. Then on the next scan, it'd be the first character's second line, then the second character's second line, etc. And the same for the third line, the fourth line, etc, until all lines of that line of text had been scanned, then you'd start on the next line of text.

    Do you see how this approach would work for your problem? To define the dot patterns for your numerals, get some graph paper and draw them out on that.
    I will give it a try,
    Thanks!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by haim1404
    I will give it a try,
    Thanks!
    Hi guys,
    I came up with something like that, but I just cant make it work...
    any suggestions?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define DIGIT_WIDTH 17  
    #define DIGIT_HEIGHT 7
    
    char digit_0[DIGIT_HEIGHT][DIGIT_WIDTH] = {
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                                             };
    
    char digit_1[DIGIT_HEIGHT][DIGIT_WIDTH] = {
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                                             };
    
    char digit_2[DIGIT_HEIGHT][DIGIT_WIDTH] = {                   
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                                             };
    
    char digit_3[DIGIT_HEIGHT][DIGIT_WIDTH] = {                       
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                                             };
    
    char digit_4[DIGIT_HEIGHT][DIGIT_WIDTH] = {
                              {' ',' ',' ',' ',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                                             };
    
    char digit_5[DIGIT_HEIGHT][DIGIT_WIDTH] = {
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
                                             };
    char digit_6[DIGIT_HEIGHT][DIGIT_WIDTH] = {
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ',' ',' '},
    						  {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ',' ',' '},
                                             };
    
    char digit_7[DIGIT_HEIGHT][DIGIT_WIDTH] = {                         
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                                             };
    
    char digit_8[DIGIT_HEIGHT][DIGIT_WIDTH] = {            
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                                             };
    
    char digit_9[DIGIT_HEIGHT][DIGIT_WIDTH] = {
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
    						  {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                                             };
    
    
    void print_digit(char digit[DIGIT_HEIGHT][DIGIT_WIDTH]);
          void print_digit(char digit[DIGIT_HEIGHT][DIGIT_WIDTH])
     {
       int i, j;
        for(i = 0; i < DIGIT_HEIGHT; ++i)
         {
           for(j = 0; j < DIGIT_WIDTH; ++j)
           printf("%c",print_digit [i][j]);  
          printf("\n\n");
         }  
    }
    
    
    
    void main(void)
    {
    
    	long int n,rev,n1;
        int d;
    	double f;
        char ch;
    
    flushall();
    printf("Enter any whole Integer Number, no bigger than 9 digits\n");
    scanf_s("%lf",&f);
    n=f;
    while ((f!=n) || (n>99999999) || (n<0))
    	{
    	printf("The number you entered is not an whole Integer Number, Or bigger than 9 digits, TRY AGAIN\n");
    	}
    
        n1=n;  
        rev=0;
          while (n!=0)
             {
             d= n%10;
             rev=rev*10+d;
             n/=10;
             } 
          while(rev!=0)
            {
             d=rev%10;
             switch(d)
            {
    	     case 0:
    	       print_digit(digit_0);
    	       break;
    		 case 1:
    	      print_digit(digit_1);
    	       break;
             case 2:
    	      print_digit(digit_2);
    	       break;
    		 case 3:
    	       print_digit(digit_3);
    	       break;
    		 case 4:
    	       print_digit(digit_4);
    	       break;
    		 case 5:
    	      print_digit(digit_5);
    	       break;
             case 6:
    	      print_digit(digit_6);
    	       break;
    		 case 7:
    	      print_digit(digit_7);
    	       break;
    		 case 8:
    	      print_digit(digit_8);
    	       break;
    		 case 9:
    	      print_digit(digit_9);
    	       break;
              }
    		 rev=rev/10;
            }
    
    }
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    What do you mean, you "cannot get it to work"?

    Does it create output that isn't exactly what you expect?
    Does it crash when you run it?
    Does it not compile?

    Do not try to make us play guessing games with you!

    We cannot read your mind! You have to tell us what is wrong, how the problem presents itself. You wouldn't play such stupid guessing games with your physician, would you? No, you would describe the symptoms to him, in as much detail as you can muster. Then why treat us any differently?

    What compiler are you using? Here is what I get when I compile it with MinGW gcc:
    C:\otros\dcw>gcc -Wall haim1.c
    haim1.c: In function `print_digit':
    haim1.c:113: subscripted value is neither array nor pointer
    haim1.c: At top level:
    haim1.c:121: warning: return type of `main' is not `int'
    haim1.c: In function `main':
    haim1.c:128: warning: implicit declaration of function `flushall'
    haim1.c:130: warning: implicit declaration of function `scanf_s'
    haim1.c:126: warning: unused variable `ch'

    C:\otros\dcw>
    Your program doesn't even compile! Why was it so impossible for you to have told us that? And why were you so incapable of mentioning what error message was displayed and pointing out which line it referred to?

    Then following the clue of the scanf_s, I created a project in Visual Studio, making sure to rename the file as .c so that it would compile as C. It only flagged one error:
    Error 1 error C2109: subscript requires array or pointer type haim.c 117
    That was flagged as line 113 in MinGW gcc (Visual Studio required an additional #include). This is the line in question:
    Code:
           printf("%c",print_digit [i][j]);
    Exactly as both compilers tell you, print_digit is not an array nor a pointer type. Rather, print_digit is a function. As a matter of fact, print_digit is the very function that you're trying to make that function call from! You are attempting to make a recursive function call! With no exit strategy! You are trying to drill a hole down through the very metal of your computer and I can guarantee you that it will crash!

    Your compiler told you what was wrong. Fix it! That is what warnings and error messages are for, to tell you what you need to fix. I cannot even begin to try to figure out what you might be trying to do there, so I cannot tell you how to fix the problem. Do not attempt to make a recursive function call there! Nor anywhere!
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Consider this idea:
    Instead of having ten separate 2-D arrays each with a unique name that you would need to explicitly reference in your code, why not create a single 3-D array, essentially an array of 2-D arrays? That way, the digit whose 2-D array you want to access will the index into that 3-D array.

    Think about it. The alternative, which you are using, would only create horribly complex code.

IMN logo majestic logo threadwatch logo seochat tools logo