hello everyone,
i need to modify these code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  	int numOfStars = 0;
    int count, counter;
	
	do
	{ 
      printf("N=");
	  scanf ("%d",&numOfStars);

for( ; counter<=numOfStars; counter++)
{
       for( ;numOfStars>0; numOfStars--)
       {
         if (numOfStars != 9999)
          {
            for (count = 0;count < numOfStars; count++)
             {
               printf("*");  
             }
            printf("\n");
         }               
       }
       printf(" ");
}
     }
     
     while (numOfStars != 9999);

  system("PAUSE");	
  return 0;
}

i already try this code but not getting the good result
i got

N=3
***
**
*
 N=

i need to get a result like this
when enter 3 i need to get this result with the way it had displayed

N=3
***
 **
  *

any help
thanks

Recommended Answers

All 8 Replies

Line 14 - you use counter with initialising it to anything

Line 16 - you modify numOfStars modifying the end condition of the loop in line 14

Line 18 - What??? Why do you think this is required?

Line 20 - 3rd Nested Loop? You are printing out a 2D grid of spaces and * only 2 loops are required one for each dimension.

Line 24 - there is exactly 1 return per line, this should not be inside a nested loop.

Line 27 - Some lines contain more than 1 space so this should be inside a nested loop somewhere.

Line 31 - The mysterious 9999 makes another appearance for no apparent reason. If numOfStars != 9999 this is an infinite loop since numOfStars is not modified. If numOfStars == 9999 this has no effect.

Line 14 - you use counter with initialising it to anything

Line 16 - you modify numOfStars modifying the end condition of the loop in line 14

Line 18 - What??? Why do you think this is required?

Line 20 - 3rd Nested Loop? You are printing out a 2D grid of spaces and * only 2 loops are required one for each dimension.

Line 24 - there is exactly 1 return per line, this should not be inside a nested loop.

Line 27 - Some lines contain more than 1 space so this should be inside a nested loop somewhere.

Line 31 - The mysterious 9999 makes another appearance for no apparent reason. If numOfStars != 9999 this is an infinite loop since numOfStars is not modified. If numOfStars == 9999 this has no effect.

hi thank you very much for your help
but am a bit lost can you show me in the code please??

hi thank you very much for your help
but am a bit lost can you show me in the code please??

Not unless you show that you have some understanding of what it is you are actually trying to achieve because then I would just be doing your work for you.

You might want to start by forgetting about coding and actually doing the exercise yourself. Choose N, 4 or 5 would be good numbers, then construct the triangle yourself from spaces and stars.

Note carefully how many spaces and stars are on each line and compare that to the line number. From that build up a method of constructing each line given the value of N. Once you have done that you may be ready to start writing a computer program to implement that algorithm.


I will say that what I should have said about line 14 is

Line 14 - you use counter without initialising it to anything

which I hope makes more sense.

Not unless you show that you have some understanding of what it is you are actually trying to achieve because then I would just be doing your work for you.

You might want to start by forgetting about coding and actually doing the exercise yourself. Choose N, 4 or 5 would be good numbers, then construct the triangle yourself from spaces and stars.

Note carefully how many spaces and stars are on each line and compare that to the line number. From that build up a method of constructing each line given the value of N. Once you have done that you may be ready to start writing a computer program to implement that algorithm.


I will say that what I should have said about line 14 is

Line 14 - you use counter without initialising it to anything

which I hope makes more sense.

hello,
my teacher told me that only one line must had to be changed in this code
here's my code:

#include <stdio.h>
#include <stdlib.h>

//procedure for star

void star_workout (void)
 {
    int numOfStars = 0;
    int count;
    
      printf("N=");
	  scanf ("%d",&numOfStars);

       for( ;numOfStars>0; numOfStars--)
        {
            for (count = 0;count < numOfStars; count++)
             {
                printf("*");
             }
             
            printf("\n");
        }
     }

//The main program
int main(int argc, char *argv[])
{
    
    star_workout ();
 
 system("PAUSE");
 return 0;
 }

only this line he told me

printf("*");

and i made a research and learn about that:

printf("%5s", "*");

but dont know how to put this in a loop

thanks

I dont think

printf("%5s", "*");

can be put in a loop.....

my teacher told me that only one line must had to be changed in this code
here's my code:

I can only say I don't think he has properly looked at this code or you have not understood his reply (assuming you paraphrased it).

only this line he told me

printf("*");

and i made a research and learn about that:

printf("%5s", "*");

I can not see how you would alter printf to output the correct number of spaces which is variable only on the first iteration of the loop especially since the loop at line 14 destroys the data needed to actually calculate what the correct number of spaces are.

Try this delete (or comment out) lines 16 - 19 and preplace them with this code line

printf("Line %d: %d spaces followed by %d stars", LineNumber, numberSpaces, numberStars);

You will need the following declaration int LineNumber, numberSpaces, numberStars; I leave you to put in the maths required to cacluate the correct values for each line.

Once you have worked out how to calculate the correct numbers for each line then you can think abouttrying to actually output the correct numbers of spaces and stars.

I can only say I don't think he has properly looked at this code or you have not understood his reply (assuming you paraphrased it).
I can not see how you would alter printf to output the correct number of spaces which is variable only on the first iteration of the loop especially since the loop at line 14 destroys the data needed to actually calculate what the correct number of spaces are.

Try this delete (or comment out) lines 16 - 19 and preplace them with this code line

printf("Line %d: %d spaces followed by %d stars", LineNumber, numberSpaces, numberStars);

You will need the following declaration int LineNumber, numberSpaces, numberStars; I leave you to put in the maths required to cacluate the correct values for each line.

Once you have worked out how to calculate the correct numbers for each line then you can think abouttrying to actually output the correct numbers of spaces and stars.

int numOfStars = 0;
    int count;
    printf("Enter a value of N \n\n");
      printf("N=");
	  scanf ("%d",&numOfStars);
	  int i, k=0;

       for( ;numOfStars>0; numOfStars--)
        {
        for(i=0;i<=k;i++) 
            {
            printf(" ");
            
            for (count = 0;count < numOfStars; count++)
             {
                printf("*");
             }
             }
             k++;
            printf("\n");
        }

i done somthing like this, but cant get the gud results

I have modified your code a little bit .. It should work now

int main()
{
    int numOfStars = 0;
    int count;
    printf("Enter a value of N \n\n");
      printf("N=");
	  scanf ("%d",&numOfStars);
	  int i, k=0;

       for( ;numOfStars>0; numOfStars--)
       {
             for(i=0;i<=k;i++) 
            {
                 printf(" ");
            }
            k++;
                 
            for (count = 0;count < numOfStars; count++)
            {
                    printf("*");
            }            
            printf("\n");
        }
}

You were very close. Your only bug was that you were terminating the while loop for printing the spaces way too late

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.