# Numbers printed with characters

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-06-2008
bnkslo
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
• 05-06-2008
kapil1089thekin
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.
• 05-06-2008
foxman
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.
• 05-06-2008
bnkslo
Quote:

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 :)
• 05-06-2008
mike_g
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; }```
• 05-07-2008
bnkslo
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 +--+ !  ! +--+ !  ! +--+ +--+   !   +   !   + +  + !  ! +--+   !   + +--+   ! +--+   ! +--+```
• 05-07-2008
matsp
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
• 05-07-2008
bnkslo
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...
• 05-07-2008
bnkslo
I give up, i tried a couple of things but it didnt worked. :(
• 05-07-2008
root4
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.
• 05-07-2008
bnkslo
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.
• 05-08-2008
bnkslo
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.
• 05-08-2008
matsp
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
• 05-08-2008
root4
It would be easier to directly get back a string through scanf()
instead of an integer converted to the string later (and
```#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;           }```