Jump to content

C Programming representing array values with stars

Go to solution Solved by Sauron,

I tried adding for (star = 0; star < lottery; star++) inside the first loop but that didn't work. I'm not actually sure what i'd need to write in this second for loop.

int a;for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  ", i+1, lottery[i]);	             for(a = 0; a < lottery[i]; a++) printf("*");                     printf("\n");		}

Right so I have this program that nearly works, the only thing that isn't working is printing out stars. What the program is supposed to do is match the amount of stars printed with each value in the array. Currently I have this code: 

#include <stdio.h>int main(){        char star = '*';		int i;		//int s;        int lottery[49] = { 23,16,18,19,26,13,22,    /*  1 ..  7 */                            20,14,22,18,21,15,17,    /*  8 .. 14 */                            24,15,18,20,13,14,20,    /* 15 .. 21 */                            18,22,20,16,19,11,20,    /* 22 .. 28 */                            16,28,22,20,15,17,17,    /* 29 .. 35 */                            21,21,19,20,14,22,25,    /* 36 .. 42 */                            19,17,26,18,20,23,12 };  /* 43 .. 49 */											for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  %c\n", i+1, lottery[i], star);			 		}	    						return 0;}

The program is supposed to do this in the (picture 1 below)

But the output is currently this (picture 2 below)

 

post-225550-0-26045100-1453462639_thumb.

post-225550-0-99077200-1453462755_thumb.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

well, that would be because you didn't tell it to print the star more than once. Simply add a second loop inside the first one that prints a star <lottery> times.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

you need another for loop that loops lottery times and has printf(star) or whatever

Link to comment
Share on other sites

Link to post
Share on other sites

well, that would be because you didn't tell it to print the star more than once. Simply add a second loop inside the first one that prints a star <lottery> times.

I tried adding for (star = 0; star < lottery; star++) inside the first loop but that didn't work. I'm not actually sure what i'd need to write in this second for loop.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

I tried adding for (star = 0; star < lottery; star++) inside the first loop but that didn't work. I'm not actually sure what i'd need to write in this second for loop.

int a;for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  ", i+1, lottery[i]);	             for(a = 0; a < lottery[i]; a++) printf("*");                     printf("\n");		}

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

You have an initial for loop that iterates through the array. Now you need a second one to iterate how many stars.i+1, lottery[i]

Try:

//loops through the array.for (i = 0; i < 49; i++){       printf("%2d (%d) |", i+1, lottery[i]);        //loops to print stars.       for(int j = 0; j < lottery[i]; j++)       {                 printf(" *");       }              //Changes line between star printing       printf("\n"); //Sorry, I forgot this. Good work Sauron for beating me to the point.            }
Link to comment
Share on other sites

Link to post
Share on other sites

 

You have an initial for loop that iterates through the array. Now you need a second one to iterate how many stars.i+1, lottery[i]

Try:

//loops through the array.for (i = 0; i < 49; i++){       printf("%2d (%d) |", i+1, lottery[i]);        //loops to print stars.       for(int j = 0; j < lottery[i]; j++)       {                 printf(" *");       }              //Changes line between star printing       printf("\n"); //Sorry, I forgot this. Good work Sauron for beating me to the point.            }

 

 

int a;for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  ", i+1, lottery[i]);	             for(a = 0; a < lottery[i]; a++) printf("*");                     printf("\n");		}

Well the program works, but now it's just a case of fomatting it correctly. It does this now which i'm trying to figure out: 205d3a4c1adc9ae83fa3bd57718a9436.png

https://gyazo.com/205d3a4c1adc9ae83fa3bd57718a9436

#include <stdio.h>int main(){        //char star = '*';		int i;		int s;        int lottery[49] = { 23,16,18,19,26,13,22,    /*  1 ..  7 */                            20,14,22,18,21,15,17,    /*  8 .. 14 */                            24,15,18,20,13,14,20,    /* 15 .. 21 */                            18,22,20,16,19,11,20,    /* 22 .. 28 */                            16,28,22,20,15,17,17,    /* 29 .. 35 */                            21,21,19,20,14,22,25,    /* 36 .. 42 */                            19,17,26,18,20,23,12 };  /* 43 .. 49 */											for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  ", i+1, lottery[i]);			 			 for (s = 0; s < lottery[i]; s++)			{				printf("*");										printf("\n");			}			 		}	    						return 0;}
   
   
Link to comment
Share on other sites

Link to post
Share on other sites

 

Well the program works, but now it's just a case of fomatting it correctly. It does this now which i'm trying to figure out: 205d3a4c1adc9ae83fa3bd57718a9436.png

https://gyazo.com/205d3a4c1adc9ae83fa3bd57718a9436

#include <stdio.h>int main(){        //char star = '*';		int i;		int s;        int lottery[49] = { 23,16,18,19,26,13,22,    /*  1 ..  7 */                            20,14,22,18,21,15,17,    /*  8 .. 14 */                            24,15,18,20,13,14,20,    /* 15 .. 21 */                            18,22,20,16,19,11,20,    /* 22 .. 28 */                            16,28,22,20,15,17,17,    /* 29 .. 35 */                            21,21,19,20,14,22,25,    /* 36 .. 42 */                            19,17,26,18,20,23,12 };  /* 43 .. 49 */											for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  ", i+1, lottery[i]);			 			 for (s = 0; s < lottery[i]; s++)			{				printf("*");										printf("\n");			}			 		}	    						return 0;}

 

well of course, you print a newline every time you print a star. Put it outside the second loop like I did.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

 

Well the program works, but now it's just a case of fomatting it correctly. It does this now which i'm trying to figure out: 205d3a4c1adc9ae83fa3bd57718a9436.png

https://gyazo.com/205d3a4c1adc9ae83fa3bd57718a9436

#include <stdio.h>int main(){        //char star = '*';		int i;		int s;        int lottery[49] = { 23,16,18,19,26,13,22,    /*  1 ..  7 */                            20,14,22,18,21,15,17,    /*  8 .. 14 */                            24,15,18,20,13,14,20,    /* 15 .. 21 */                            18,22,20,16,19,11,20,    /* 22 .. 28 */                            16,28,22,20,15,17,17,    /* 29 .. 35 */                            21,21,19,20,14,22,25,    /* 36 .. 42 */                            19,17,26,18,20,23,12 };  /* 43 .. 49 */											for (i = 0; i < 49; i++)		{		     printf(" %2d  (%d)  |  ", i+1, lottery[i]);			 			 for (s = 0; s < lottery[i]; s++)			{				printf("*");										printf("\n");			}			 		}	    						return 0;}

Move the "printf("\n");" outside of the for loop that it's nested in. Just put the brace in the line above it and remove the one directly below it and fix spacing.

Link to comment
Share on other sites

Link to post
Share on other sites

well of course, you print a newline every time you print a star. Put it outside the second loop like I did.

Oh yeah I didn't spot that, sorry. Thanks for your help.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

 

You have an initial for loop that iterates through the array. Now you need a second one to iterate how many stars.i+1, lottery[i]

Try:

//loops through the array.for (i = 0; i < 49; i++){       printf("%2d (%d) |", i+1, lottery[i]);        //loops to print stars.       for(int j = 0; j < lottery[i]; j++)       {                 printf(" *");       }              //Changes line between star printing       printf("\n"); //Sorry, I forgot this. Good work Sauron for beating me to the point.            }

Thanks for your help as well.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for your help as well.

Thanks! I do what I can but the guys who frequent the forums are a lot quicker than me!  :D

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×