Stack Overflow Asked by Aditya Sadhukhan on February 4, 2021
PLEASE HELP
Where did I make mistake in the following program?
It is not giving the expected output. I have found some solutions to this problem but they are using different logic. I want to know what is the problem with the following logic.
I did a dry ran according to this logic, I didn’t find any problem there. I am really very confused.
# include <stdio.h>
int main()
{
char zero[5][5] = {
"#####",
"# #",
"# #",
"# #",
"#####"
};
char one[5][5] = {
" #",
" #",
" #",
" #",
" #"
};
char two[5][5] = {
"#####",
" #",
"#####",
"# ",
"#####"
};
char three[5][5] = {
"#####",
" #",
"#####",
" #",
"#####"
};
char four[5][5] = {
"# #",
"# #",
"#####",
" #",
" #"
};
char five[5][5] = {
"#####",
"# ",
"#####",
" #",
"#####"
};
char six[5][5] = {
"#####",
"# ",
"#####",
"# #",
"#####"
};
char seven[5][5] = {
"#####",
" #",
" #",
" #",
" #"
};
char eight[5][5] = {
"#####",
"# #",
"#####",
"# #",
"#####"
};
char nine[5][5] = {
"#####",
"# #",
"#####",
" #",
"#####"
};
char userInput[3] = "356";
int n = 3,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<n;j++)
{
switch(userInput[j])
{
case '0':
printf("%s ",zero[i]);
break;
case '1':
printf("%s ",one[i]);
break;
case '2':
printf("%s ",two[i]);
break;
case '3':
printf("%s ",three[i]);
break;
case '4':
printf("%s ",four[i]);
break;
case '5':
printf("%s ",five[i]);
break;
case '6':
printf("%s ",six[i]);
break;
case '7':
printf("%s ",seven[i]);
break;
case '8':
printf("%s ",eight[i]);
break;
case '9':
printf("%s ",nine[i]);
break;
}
}
printf("n");
}
}
int main()
{
char zero[5][6] = {
"#####",
"# #",
"# #",
"# #",
"#####"
};
char one[5][6] = {
" #",
" #",
" #",
" #",
" #"
};
char two[5][6] = {
"#####",
" #",
"#####",
"# ",
"#####"
};
char three[5][6] = {
"#####",
" #",
"#####",
" #",
"#####"
};
char four[5][6] = {
"# #",
"# #",
"#####",
" #",
" #"
};
char five[5][6] = {
"#####",
"# ",
"#####",
" #",
"#####"
};
char six[5][6] = {
"#####",
"# ",
"#####",
"# #",
"#####"
};
char seven[5][6] = {
"#####",
" #",
" #",
" #",
" #"
};
char eight[5][6] = {
"#####",
"# #",
"#####",
"# #",
"#####"
};
char nine[5][6] = {
"#####",
"# #",
"#####",
" #",
"#####"
};
char userInput[3] = "356";
int n = 3,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<n;j++)
{
switch(userInput[j])
{
case '0':
printf("%s ",zero[i]);
break;
case '1':
printf("%s ",one[i]);
break;
case '2':
printf("%s ",two[i]);
break;
case '3':
printf("%s ",three[i]);
break;
case '4':
printf("%s ",four[i]);
break;
case '5':
printf("%s ",five[i]);
break;
case '6':
printf("%s ",six[i]);
break;
case '7':
printf("%s ",seven[i]);
break;
case '8':
printf("%s ",eight[i]);
break;
case '9':
printf("%s ",nine[i]);
break;
}
}
printf("n");
}
}
I have changed very little from your code. When you use %s to print a string it searches for ' ' so you must leave one bit blank for ' '. What I mean by this is. If your array size is 5. Then you can store 4 characters
in it and last one will be used to store .
Since you provided all 5 bits of character your print wont find at the end of every line.
Here is the output:
##### ##### #####
# # #
##### ##### #####
# # # #
##### ##### #####
Answered by Sagun Devkota on February 4, 2021
Here you have a function which allows scaling
# include <stdio.h>
char *numbers[10][5] = {{
"#####",
"# #",
"# #",
"# #",
"#####",
},
{
" #",
" #",
" #",
" #",
" #",
}
,
{
"#####",
" #",
"#####",
"# ",
"#####"
},
{
"#####",
" #",
"#####",
" #",
"#####"
}
,{
"# #",
"# #",
"#####",
" #",
" #"
}
,{
"#####",
"# ",
"#####",
" #",
"#####"
},{
"#####",
"# ",
"#####",
"# #",
"#####"
},{
"#####",
" #",
" #",
" #",
" #"
},{
"#####",
"# #",
"#####",
"# #",
"#####"
},{
"#####",
"# #",
"#####",
" #",
"#####"
}};
void print(const char *str, int scale)
{
const char *wrk;
for(int row = 0; row < 5; row++)
{
for(int rowrep = 0; rowrep < scale; rowrep++)
{
wrk = str;
while(*wrk)
{
char *digitrow = numbers[*wrk - '0'][row];
while(*digitrow)
{
for(int charrep = 0; charrep < scale; charrep++)
{
printf("%c", *digitrow);
}
digitrow++;
}
for(int charrep = 0; charrep < scale; charrep++) printf(" ");
wrk++;
}
printf("n");
}
}
}
int main(void)
{
print("0136", 1);
printf("n");
print("0136", 2);
printf("n");
print("0136", 3);
printf("n");
print("0136", 6);
}
and the result:
##### # ##### #####
# # # # #
# # # ##### #####
# # # # # #
##### # ##### #####
########## ## ########## ##########
########## ## ########## ##########
## ## ## ## ##
## ## ## ## ##
## ## ## ########## ##########
## ## ## ########## ##########
## ## ## ## ## ##
## ## ## ## ## ##
########## ## ########## ##########
########## ## ########## ##########
############### ### ############### ###############
############### ### ############### ###############
############### ### ############### ###############
### ### ### ### ###
### ### ### ### ###
### ### ### ### ###
### ### ### ############### ###############
### ### ### ############### ###############
### ### ### ############### ###############
### ### ### ### ### ###
### ### ### ### ### ###
### ### ### ### ### ###
############### ### ############### ###############
############### ### ############### ###############
############### ### ############### ###############
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
###### ###### ###### ###### ######
###### ###### ###### ###### ######
###### ###### ###### ###### ######
###### ###### ###### ###### ######
###### ###### ###### ###### ######
###### ###### ###### ###### ######
###### ###### ###### ############################## ##############################
###### ###### ###### ############################## ##############################
###### ###### ###### ############################## ##############################
###### ###### ###### ############################## ##############################
###### ###### ###### ############################## ##############################
###### ###### ###### ############################## ##############################
###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ######
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
############################## ###### ############################## ##############################
Add some parameter checking: if the string contains only digirs, if the scale is > 0 etc stc.
In this code character width does not have to be constant. You can create not fixed-width fonts like in this example (look at the digit 1
)
Answered by 0___________ on February 4, 2021
Edit: I took John Bodes comment to form an answer.
# include <stdio.h>
int main()
{
char zero[][6] = {
"#####",
"# #",
"# #",
"# #",
"#####"
};
char one[][6] = {
" #",
" #",
" #",
" #",
" #"
};
char two[][6] = {
"#####",
" #",
"#####",
"# ",
"#####"
};
char three[][6] = {
"#####",
" #",
"#####",
" #",
"#####"
};
char four[][6] = {
"# #",
"# #",
"#####",
" #",
" #"
};
char five[][6] = {
"#####",
"# ",
"#####",
" #",
"#####"
};
char six[][6] = {
"#####",
"# ",
"#####",
"# #",
"#####"
};
char seven[][6] = {
"#####",
" #",
" #",
" #",
" #"
};
char eight[][6] = {
"#####",
"# #",
"#####",
"# #",
"#####"
};
char nine[][6] = {
"#####",
"# #",
"#####",
" #",
"#####"
};
//found another spot that's dangerous
char userInput[] = "356";
int n = 3,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<n;j++)
{
switch(userInput[j])
{
case '0':
printf("%s ",zero[i]);
break;
case '1':
printf("%s ",one[i]);
break;
case '2':
printf("%s ",two[i]);
break;
case '3':
printf("%s ",three[i]);
break;
case '4':
printf("%s ",four[i]);
break;
case '5':
printf("%s ",five[i]);
break;
case '6':
printf("%s ",six[i]);
break;
case '7':
printf("%s ",seven[i]);
break;
case '8':
printf("%s ",eight[i]);
break;
case '9':
printf("%s ",nine[i]);
break;
}
}
printf("n");
}
}
Answered by questioner on February 4, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP