patterncModerate
Printing the number in large size
Viewed 0 times
numberthesizeprintinglarge
Problem
Problem Description
Receive a 5 digit number and print out in a large size.
Sample input 32145
Sample output
This is the first time I attempted this kind of question and it is working. Could you please tell me if there any other efficient ways to do the same?
```
#include
#define gotol(l) printf("\033[%dA",(l))
#define gotoc(c) printf("\033[%dC",(c))
#define gotold(d) printf("\033[%dB",(d))
void disp(int j,int i)
{
switch(j)
{
case 0:gotoc(i*15);
printf("#####\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("#####\n");gotol(8);;
break;
case 1:gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf("##\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\
Receive a 5 digit number and print out in a large size.
Sample input 32145
Sample output
##### ##### # # #####
# # ## # #
# # # # #
##### # # # # #####
# ##### # ##### #
# # # # #
# # # # #
##### ##### ### # #####This is the first time I attempted this kind of question and it is working. Could you please tell me if there any other efficient ways to do the same?
```
#include
#define gotol(l) printf("\033[%dA",(l))
#define gotoc(c) printf("\033[%dC",(c))
#define gotold(d) printf("\033[%dB",(d))
void disp(int j,int i)
{
switch(j)
{
case 0:gotoc(i*15);
printf("#####\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("# #\n");gotoc(i*15);
printf("#####\n");gotol(8);;
break;
case 1:gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf("##\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\n");gotoc(i*15);
printf(" #\
Solution
You have a working program that does what you wanted, so that's an excellent start for a beginner! I have a couple of suggestions that may help you improve your program.
Consider using a data structure
Right now, the data for the digits and the code to print them is intertwined. Since the data for the large digits is just data, consider separating that data from the code that prints the digits. For example, I might start with this:
That way, it's easy to see how the digits will look and easy to modify them if we care to do so.
Consider how to print on a printer
Many years ago, the primary interface for a computer was more typically a printer (teletype) rather than a screen. For such devices, the ANSI escape sequences your code uses to position the cursor wouldn't work. (There are also command line interfaces today which do not support ANSI escape sequences.) One way to accommodate such a use would be to simply print each line of the string of digits. For example, one might use code like this:
See if you can write the
Eliminate unused variables
The variable
Avoid buffer overrun vulnerabilities
The code currently includes these two statements:
This is a potential problem because the
Now the maximum width of the string is set to 5 characters (per comment by @CoolGuy -- we need to reserve room for the terminating
Consider using a data structure
Right now, the data for the digits and the code to print them is intertwined. Since the data for the large digits is just data, consider separating that data from the code that prints the digits. For example, I might start with this:
#define DIGITHEIGHT 7
#define DIGITWIDTH 6
const char *digits[DIGITHEIGHT] = {
"##### # ##### ##### # # ##### ##### ##### ##### ##### ",
"# # # # # # # # # # # # # # ",
"# # # # # # # # # # # # # # ",
"# # # ##### ##### ##### ##### ##### # ##### ##### ",
"# # # # # # # # # # # # # ",
"# # # # # # # # # # # # # ",
"##### # ##### ##### # ##### ##### # ##### # "
};That way, it's easy to see how the digits will look and easy to modify them if we care to do so.
Consider how to print on a printer
Many years ago, the primary interface for a computer was more typically a printer (teletype) rather than a screen. For such devices, the ANSI escape sequences your code uses to position the cursor wouldn't work. (There are also command line interfaces today which do not support ANSI escape sequences.) One way to accommodate such a use would be to simply print each line of the string of digits. For example, one might use code like this:
for (int line = 0; line < DIGITHEIGHT; ++line) {
for (char *s = str; *s; ++s) {
printdigitrow(*s, line);
}
putchar('\n');
}See if you can write the
printdigitrow routine to use the digits array defined above to print one row of one digit.Eliminate unused variables
The variable
s in your code is defined but never used. Since unused variables are a sign of poor code quality, you should seek to eliminate them. Your compiler is probably smart enough to warn you about such things if you know how to ask it to do so.Avoid buffer overrun vulnerabilities
The code currently includes these two statements:
char str[6];
scanf("%s",str);This is a potential problem because the
scanf will read any size of string, but we've only allocated 6 bytes. That's the recipe for a buffer overflow vulnerability and must be eliminated. Fortunately, it's simple to do so:scanf("%5s",str);Now the maximum width of the string is set to 5 characters (per comment by @CoolGuy -- we need to reserve room for the terminating
'\0') and no buffer overflow will occur.Code Snippets
#define DIGITHEIGHT 7
#define DIGITWIDTH 6
const char *digits[DIGITHEIGHT] = {
"##### # ##### ##### # # ##### ##### ##### ##### ##### ",
"# # # # # # # # # # # # # # ",
"# # # # # # # # # # # # # # ",
"# # # ##### ##### ##### ##### ##### # ##### ##### ",
"# # # # # # # # # # # # # ",
"# # # # # # # # # # # # # ",
"##### # ##### ##### # ##### ##### # ##### # "
};for (int line = 0; line < DIGITHEIGHT; ++line) {
for (char *s = str; *s; ++s) {
printdigitrow(*s, line);
}
putchar('\n');
}char str[6];
scanf("%s",str);scanf("%5s",str);Context
StackExchange Code Review Q#93416, answer score: 12
Revisions (0)
No revisions yet.