Thread: Printing number patterns - need help

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    11

    Printing number patterns - need help

    Hello guys,
    I need help writing the following program using loops.

    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!!

    Printing number patterns - need help-numbers-png

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,906
    No worries - What have you done so far?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Actually i just start learning c and Im very newbie in it. I hope some could post simple working solution, so I could learm from it..

  4. #4

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    I see, I didnt know, sorry...
    You may close/delete the thread.
    Last edited by haim1404; 11-10-2013 at 08:30 AM.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Is printing the digits sideways just to make the project more tedious than it already is?

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Maybe this may inspire you with an idea on how you might begin. The "font" isn't usable for your program -- it's too small for a start but you might be able to trick it. How might you go about creating a "font"? Your idea probably won't look much like the code below (and that's OK), but maybe the #defines at least might give you a hint.

    How might I have ended up with the code below? From the comment can you work out how to print a digit (for example the digit '5')? After that, can you work out how to print the digit sideways (hint: look up row-major on Wikipedia)?

    Code:
    #define FONT_W      6
    #define FONT_H      6
    #define GLYPH_SZ    (FONT_W * FONT_H)
    
    /* Font data. Glyphs row-major; digits 0 to 9 sequentially (i.e. digit 1 
     * starts at index GLYPH_SZ; digitnidx = n * GLYPH_SZ where 0 <= n <= 9).
     */
    static unsigned char font[] = {
        0,1,1,1,1,0,1,1,0,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0,  /* Digit 0 */
        0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,0,  /* Digit 1 */
        0,0,1,1,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1,
        0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,1,0,1,1,1,1,0,
        0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,1,0,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,0,
        1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0,
        0,0,1,1,1,0,0,1,0,0,0,0,1,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0,
        1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,
        0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0,
        0,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,1,0,0,0,0,1,0,0,1,1,1,0,0   /* Digit 9 */
    };
    Last edited by Hodor; 11-10-2013 at 08:59 PM. Reason: Added "digit" comments

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    I forgot to mention that the code above uses a 1-dimensional array because I was hoping it might trigger you to think about how array addressing/indexing works. The following is equally valid and makes accessing the array "easier".

    Code:
    static unsigned char font[10][GLYPH_SZ] = {
        { 0,1,1,1,1,0,1,1,0,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0 },
        { 0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,0 },
        { 0,0,1,1,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1 },
        { 0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,1,0,1,1,1,1,0 },
        { 0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,1,0,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,0 },
        { 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0 },
        { 0,0,1,1,1,0,0,1,0,0,0,0,1,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0 },
        { 1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0 },
        { 0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0 },
        { 0,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,1,0,0,0,0,1,0,0,1,1,1,0,0 }
    };
    I suppose you could also have a 3-dimensional array, although I personally wouldn't.
    Last edited by Hodor; 11-10-2013 at 09:32 PM. Reason: typo

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Hello guys,
    thanks for the help.
    in the end I went forward using arrays and switch-case loops.
    after getting the number, I stripped it and reversed it. than on each digit I put switch case loop, witch should print the correspond function.
    I need some help to call the function to print in the case ( I just don't know how...)
    in addition i believe I Have lots of other rookie mistakes - appreciate the understanding and would like for any advice's, or better solution's....



    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    void zero (char array [7][17]);
       void zero (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
    void one (char array [7][17]);
       void one (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
    void two (char array [7][17]);
       void two (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
    void three (char array [7][17]);
       void three (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
       void four (char array [7][17]);
       void four (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ',' ',' ',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ',' ',' ',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
       void five (char array [7][17]);
       void five (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
       void six (char array [7][17]);
       void six (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ',' ',' '},
                              {' ',' ','0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ',' ',' '},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
       void seven (char array [7][17]);
       void seven (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
       void eight (char array [7][17]);
       void eight (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\n");
         }  
       }
    
    
       void nine (char array [7][17]);
       void nine (char array [7][17])
       {
        int i, j;
           char matrix[7][17]= {
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0',' ',' ',' ',' ','0','0',' ',' ',' ',' ',' ','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                              {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
                            };
     
        for(i = 0; i < 7; ++i)
         {
           for(j = 0; j < 17; ++j)
           printf("%c", matrix[i][j]);  
          printf("\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:
               ??????????????
               break;
             case 1:
               ??????????????
               break;
             case 2:
               ??????????????
               break;
             case 3:
               ??????????????
               break;
             case 4:
               ??????????????
               break;
             case 5:
               ??????????????
               break;
             case 6:
               ??????????????
               break;
             case 7:
               ??????????????
               break;
             case 8:
               ??????????????
               break;
             case 9:
               ??????????????
               break;
              }
             rev=rev/10;
            }
    
    }
    Last edited by haim1404; 11-14-2013 at 11:40 AM.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    The thing I noted was you have made your arrays very specific. Whereas for not very much more work you could make them a great deal more general and that would allow you to experiment with the look of the output. So instead of being restricted to '0' characters you could make a choice of character simply by changing ONE variable in the code. Just a thought.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't use magic numbers, #define some constants.

    What's so hard about calling a funciton? printf is a funciton, you call it just fine. All you need to do is use the function name with some parentheses and any parameters: function_name(param1, param2); But maybe it's difficult since your functions are not so well designed.

    Your zero..nine functions are all basically identical, except for the local variable matrix. Whenever you have repeated code, consider making it a single function that you call from several places. Also, each of those functions takes a parameter, but you never use that parameter. Instead, consider defining those matrices outside your functions, and have a print_digit function you call, passing a different digit matrix.
    Code:
    #define DIGIT_WIDTH 17  // yes, this is slightly confusing, b/c the numbers are printed sideways
    #define DIGIT_HEIGHT 7
    
    char digit_0[DIGIT_HEIGHT][DIGIT_WIDTH] = {
    };
    
    void print_digit(char digit[DIGIT_HEIGHT][DIGIT_WIDTH])
    {
        // similar to the contents of any of your zero..nine functions
        for (i = 0; i < DIGIT_HEIGHT; i++
            ...
    }
    ...
    d = rev % 10;
    switch(d) {
        case 0:
            print_digit(digit_0);
            break;
        case 1:
            print_digit(digit_1);
            break;
    ...
    Since the digits 0..9 map nicely to array indices, you could even do one better, and make an array of digits, i.e. a 3-d array:
    Code:
    #define NUM_DIGITS 10
    #define DIGIT_WIDTH 17  // yes, this is slightly confusing, b/c the numbers are printed sideways
    #define DIGIT_HEIGHT 7
    
    
    char digits[NUM_DIGITS][DIGIT_HEIGHT][DIGIT_WIDTH] = {
        {
            {' ', ' ', '0', '0', '0'...},
            // all the ' ' and '0' for digit 0
        },
        {  // all the ' ' and '0' for digit 1
        },
        {  // all the ' ' and '0' for digit 2
        },
        ...
    };
    
    
    
    void print_digit(char digit[DIGIT_HEIGHT][DIGIT_WIDTH])
    {
        // similar to the contents of any of your zero..nine functions
        for (i = 0; i < DIGIT_HEIGHT; i++
            ...
    }
    ...
    d = rev % 10;
    print_digit(digits[d]);  // no switch statement needed, just the loop for processing digits
    Last edited by anduril462; 11-14-2013 at 01:07 PM.

  12. #12
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Hi anduril462,
    thanks for the help. unfortunately I got confused...
    could your please if it not so hard post the entire fixed code - so I could check it out from the beginning to the end...

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by haim1404 View Post
    Hi anduril462,
    thanks for the help. unfortunately I got confused...
    could your please if it not so hard post the entire fixed code - so I could check it out from the beginning to the end...
    Sorry, can't do that. It's against our homework policy. I gave you some pretty big hints though, and you only spent at most 11 minutes thinking about it and trying. Spend some more time, and actually try to implement it. Post back with actual code. One more hint, the last solution I gave, with the 3-d array, does not need any switch statement (I've added a comment to the code to this effect).

  14. #14
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Quote Originally Posted by anduril462 View Post
    Sorry, can't do that. It's against our homework policy. I gave you some pretty big hints though, and you only spent at most 11 minutes thinking about it and trying. Spend some more time, and actually try to implement it. Post back with actual code. One more hint, the last solution I gave, with the 3-d array, does not need any switch statement (I've added a comment to the code to this effect).
    I see...

    did you meant something like this?
    If so, it doesn't work for me...

    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;
            }
    
    
    }

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes. That was exactly what I meant. That was my first suggestion, which is pretty good. But my second suggestion was even better. It requires no switch statement. You would do somethin like
    Code:
    #define DIGIT_WIDTH 17  
    #define DIGIT_HEIGHT 7
     #define NUM_DIGITS 10
     
    char digits[NUM_DIGITS][DIGIT_HEIGHT][DIGIT_WIDTH] = {
        {  // digit 0
            {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
            {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
            {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
            {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
            {' ',' ','0','0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0','0'},
            {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
            {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
        },
        {  // digit 1
            {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
            {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
            {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
            {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
            {' ',' ','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
            {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
            {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
        },
    ...
    };
    ...
    while(rev!=0)
    {
        d=rev%10;
        // no need for a switch
        print_digit(digits[d]);
        rev=rev/10;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Patterns and anti-patterns
    By Neo1 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2013, 05:30 PM
  2. Number Patterns are killing me.
    By garrett in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2012, 01:33 PM
  3. Printing Patterns Using Recursion
    By CProgramming11 in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 03:55 PM
  4. NEED homework help! Printing out patterns.
    By omGeeK in forum C Programming
    Replies: 8
    Last Post: 09-28-2010, 07:16 PM
  5. Printing Patterns
    By ferniture in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 10:00 PM

Tags for this Thread