Thread: Numbers printed with characters

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36

    Numbers printed with characters

    Hello, i have to make a program that would print out numbers represented with characters like this:

    8317

    Code:
    +--+ +--+ + +--+
     !  !    ! !    !
     +--+ +--+ +    + 
     !  !    ! !    !
     +--+ +--+ +    !
    
    srry i could get itto look any better, the first number is 8 and then 3 and so on...
    So far i did this:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *deliStevilke[]={ // numbers representet with characters
    		"+--+", // 0
    			"+  +", // 1
    			"!  !", // 2
    			"   +", // 3
    			"   !", // 4
    			"!   ", // 5
    			"+",	// 6
    			"!",	// 7
    	};
    	
    	int stevilke[10][5]={ // numbers how they are to be printed on the screen
    		{0,2,1,2,0}, // 0
    		{6,7,6,7,6}, // 1
    		{0,4,0,5,0}, // 2
    		{0,4,0,4,0}, // 3
    		{1,2,0,4,3}, // 4
    		{0,5,0,4,0}, // 5
    		{0,5,0,2,0}, // 6
    		{0,4,3,4,3}, // 7
    		{0,2,0,2,0}, // 8
    		{0,2,0,4,0}, // 9
    	};
    	
    	int stevilo;
    	
    	scanf("%d", &stevilo);
    	// print the number in character style
    	printf("%s\n", deliStevilke[stevilke[stevilo][0]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][1]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][2]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][3]]);
    	printf("%s\n", deliStevilke[stevilke[stevilo][4]]);
    	
    	return 0;
    }
    I have no truble printing if you put in 1 number like 9. I dont know what to add so that i could print a number like 9999.

    Could somebody give me some idea what to do?

    Thank you.
    bnk

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    134
    take the no and use &#37;10 operand to find out the digits at the unit place and then the answer in the same loop again and print this as a single digit.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, there's different way of doing so. Basically you decompose your number into digit and make it work altogether.

    Code:
    #include <stdlib.h>
    
    int* decomposeNumber(int n, int *nbOfDigits)
    //  Return an array of *nbOfDigits numbers between 0 and 9,
    //  which is the decomposition in digits from n
    {
        int origN = n;
        int *tab;
        int i;
    
        *nbOfDigits = 0;
        do
        {
            n = n / 10;
            (*nbOfDigits)++;
        } while (n != 0);
    
        tab = malloc(*nbOfDigits * sizeof(*tab));
        // Here you should check if tab != NULL
    
        n = origN;
        for (i = *nbOfDigits - 1; i >= 0; i--)
        {
            tab[i] = n % 10;
            n = n / 10;
        }
    
        return tab;
    }
    
    void printNumberBig(int n, FILE *file)
    //  Print only non-negative numbers
    {
        static char *deliStevilke[]={
            "+--+", // 0
            "+  +", // 1
            "!  !", // 2
            "   +", // 3
            "   !", // 4
            "!   ", // 5
            "+",	// 6
            "!",	// 7
        };
    
        static int stevilke[10][5]={
            {0,2,1,2,0}, // 0
            {6,7,6,7,6}, // 1
            {0,4,0,5,0}, // 2
            {0,4,0,4,0}, // 3
            {1,2,0,4,3}, // 4
            {0,5,0,4,0}, // 5
            {0,5,0,2,0}, // 6
            {0,4,3,4,3}, // 7
            {0,2,0,2,0}, // 8
            {0,2,0,4,0}, // 9
        };
    
        if (n >= 0 && file != NULL)
        {
            int *tab;
            int nbOfDigits;
            int i, j;
    
            tab = decomposeNumber(n, &nbOfDigits);
            for (j = 0; j < 5; j++)
            {
                // Magic number aren't great
                for (i = 0; i < nbOfDigits; i++)
                {
                    fprintf(file, "%s ", deliStevilke[stevilke[tab[i]][j]]);
                }
                putchar('\n');
            }
        }
    }
    Sorry for giving the answer but it's the only way i could be clear.
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    take the no and use %10 operand to find out the digits at the unit place and then the answer in the same loop again and print this as a single digit.
    Kepil, i understad how to use the %10 operand. If for example i do:

    8713%10

    the result is 3, but this is the last digit and i have to print this one at the last place. Just thinking if i print the 3 and then jump to the start of the line and then with the help of a loop calculate the next digit. But how do i substract the 3 from the number 8713 to get the next number 1 and so on?

    foxman thank you for your help, but my knowledge is not so big in the way i could use the malloc function for the memory and you used so many pointers i kind of lost my way

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You can use a combination of divide and mod to get the digit you want. Like:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 8713;
        int digit;
        while( num > 0 )
        { 
           digit = num % 10;
           num /= 10;
           printf("%i\n", digit);
        }
        getchar();
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Mike thank you again for helping.

    This is what i did:

    Code:
    #include <stdio.h>
    
    	char *deliStevilke[]={ // numbers representet with characters
    		"+--+", // 0
    		"+  +", // 1
    		"!  !", // 2
    		"   +", // 3
    		"   !", // 4
    		"!   ", // 5
    		"+",	// 6
    		"!",	// 7
    	};
    	
    	int stevilke[10][5]={ // numbers how they are to be printed on the screen
    		{0,2,1,2,0}, // 0
    		{6,7,6,7,6}, // 1
    		{0,4,0,5,0}, // 2
    		{0,4,0,4,0}, // 3
    		{1,2,0,4,3}, // 4
    		{0,5,0,4,0}, // 5
    		{0,5,0,2,0}, // 6
    		{0,4,3,4,3}, // 7
    		{0,2,0,2,0}, // 8
    		{0,2,0,4,0}, // 9
    	};
    
    int main()
    {
    
    	
    	int stevilo,i=0,k=0,j=0;
    	int pomoznoPolje[10];
    	
    	
    	
    	scanf("%d", &stevilo);
    	
    
    	while(stevilo>0)
    	{
    		pomoznoPolje[i]=stevilo%10;
    		stevilo/=10;
    		i++;
    	}
    	k=i;
    		
    	for(j;j<i;j++)
    	{
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][0]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][1]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][2]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][3]]);
    	printf("%s\n", deliStevilke[stevilke[pomoznoPolje[k-1]][4]]);
    	k--;
    	}
    	return 0;
    }
    And this is what comes out:
    Now i just have to print this in a horizontal line not vertical. Is there any way to do that?

    Code:
    8743
    +--+
    !  !
    +--+
    !  !
    +--+
    +--+
       !
       +
       !
       +
    +  +
    !  !
    +--+
       !
       +
    +--+
       !
    +--+
       !
    +--+

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'd have to form a 2D array of the output data [so, you'd have to figure out what all the digits you want on one row, then produce the first row of each digit, then second row of all the digits, etc, until you have done all lines of all digits].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    So i have to write a totaly different code. Because i have the 2D array of the numbers and the 2D array of the characters. I think you mean i have to do something like this:

    Code:
    row1: "+--+", "+", "+--+", "+--+" and so on
    row2: "!  !", "!", "   !", "   !" and so on
    row3: "+  +", "+", "+--+", "+--+" and so on
    row4: "!  !", "!", "!   ", "   !" and so on
    row5: "+--+", "+", "+--+", "+--+" and so on
    And if i would want to print out 1 and 0, i would have to go and look into:

    array [1][0] and then array [0][0]
    array [1][1] and then array [0][1]

    and so on...

    I will play around with it...well maybe the last 2 days of work are not for nothing...

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    I give up, i tried a couple of things but it didnt worked.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Here's an awful solution:

    Code:
      char *deliStevilke[]={ // numbers representet with characters
        "+--+", // 0
        "+  +", // 1
        "!  !", // 2
        "   +", // 3
        "   !", // 4
        "!   ", // 5
        "   +", // 6 <- Allocate the same width here (3 chars)
        "   !", // 7 <- idem
      };
      ...
      while(stevilo>0)
      {
        pomoznoPolje[i]=stevilo%10;
        stevilo/=10;
        i++;
      }
      k=i;
    
      for(j=0,k=i;j<i;j++,--k)
        printf("%s ", deliStevilke[stevilke[pomoznoPolje[k-1]][0]]); // Row 1
      printf("\n");
      for(j=0,k=i;j<i;j++,--k)
        printf("%s ", deliStevilke[stevilke[pomoznoPolje[k-1]][1]]); // Row 2
      printf("\n");
      for(j=0,k=i;j<i;j++,--k)
        printf("%s ", deliStevilke[stevilke[pomoznoPolje[k-1]][2]]); // Row 3
      printf("\n");
      for(j=0,k=i;j<i;j++,--k)
        printf("%s ", deliStevilke[stevilke[pomoznoPolje[k-1]][3]]); // Row 4
      printf("\n");
      for(j=0,k=i;j<i;j++,--k)
        printf("%s ", deliStevilke[stevilke[pomoznoPolje[k-1]][4]]); // Row 5
      printf("\n");
      ...
    Now, I'm sure you can improve this thing by using a single loop (as you did
    before and storing the strings in an intermediary buffer as suggested in another
    post), you're almost done.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    root4 thx, but i just did it by myself...as always man should not give up so soon

    The one loop i will try and work out too.

    And thank you mike and matsp.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Well i still have a small problem. It doesnt print a 0 if its in the first place like 0123.. The print out is only 123. I know its because in the while statement is going for as long the number is > 0. So i changed the while statement into do while statement so that if the first number would be 0 it does the do for at least one time. But know i can print the 0 only if its the only number that is entered.

    Well my question. I dont know how to change the while statement so that it would go true the number even if the first number is a 0.

    Any hint?

    Thank you.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You would need to "know how many digits it should be", so if you have 0123, then you need to know that you have 4 digits. Then you will get sufficient number of starting zero's for what you are doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    It would be easier to directly get back a string through scanf()
    instead of an integer converted to the string later (and
    losing the leading 0).

  15. #15
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Here's a better solution.. I wrote this a while ago, I believe, it explains the concept so you can adapt it to your own program.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
         char array[9][10][20] = 
         {
              {" ### ","    "," ### "," ### ","     "," ### "," ### "," ### "," ### "," ### "},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"     ","    "," ### "," ### "," ### "," ### "," ### ","     "," ### "," ### "},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {" ### ","    "," ### "," ### ","     "," ### "," ### ","     "," ### "," ### "},    
         }, 
         *nr = argv[1];
         unsigned int i = 0, h = 0;
         
         for( i = 0; i < 9; i++)
         {
              for(h = 0; h < strlen(nr); h++)
                   if(nr[h] !=  ' ')
                        printf(" %s ", array[i][nr[h] - 0x30]);
                   
              putchar('\n');
                   
         }
         
         
         return 0;
              
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2009, 02:32 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. How would I only enter numbers and not characters
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2004, 03:19 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM