This probably is a stupid question hey we all got to start programming somewhere.
I got to use for loops to generate two patterns as follows.
*****
****
***
**
*
and
*
***
*****
*******
*****
***
*
Any Ideas.
This probably is a stupid question hey we all got to start programming somewhere.
I got to use for loops to generate two patterns as follows.
*****
****
***
**
*
and
*
***
*****
*******
*****
***
*
Any Ideas.
well
is the same asCode:for(i = 0; i < 10; ++i) { }
write some code if u haven't already and we'll help u out from there.Code:i = 0; while(i < 10) { ++i; }
Try expirmenting with nested for loops.
For instance there are five lines to suggestion which implies a loop counting to five. Second there is an inner loop which print a number of stars based on the current line.
Code:numberofstars = maxstars; // maxstars=5 for (int lines=0; lines<5;++lines) { while (numberofstar>0) { cout << '*'; numberofstar =numberofstars-1; } maxstars--; // decrement by 1 numberstars=maxstars; }
I've absolutley no idea how to write a for loop. Does this website explain them well. I've written while loops if that is any help.
loop are explained here
http://www.cprogramming.com/tutorial/lesson3.html
>I've absolutley no idea how to write a for loop.
I had problems with for loops when I started. It helps to build a for loop from the equivalent while loop. You construct it like so:
And you're done! Just remember that only the first and last steps of this process result in valid C code.Code:/* Start with a while loop */ i = 0; while ( i < 10 ) { i++; } /* Put semicolons around the condition */ i = 0; while ( ; i < 10 ; ) { i++; } /* Put the initialization of the condition variable before the condition */ while ( i = 0 ; i < 10 ; ) { i++; } /* Put the increment of the condition variable after the condition */ while ( i = 0 ; i < 10 ; i++ ) { } /* Change the keyword while to for */ for ( i = 0 ; i < 10 ; i++ ) { }
My best code is written with the delete key.
why doesn't this code work?
Code:// starts declining # include <stdio.h> int main() { int stars; for(stars=5; stars>=1;stars--) // also for stars in range(6,0-1) printf("*\n"*stars); system("pause"); return 0; }
Originally posted by apoc632
I've absolutley no idea how to write a for loop. Does this website explain them well. I've written while loops if that is any help.
LEARN FOR / WHILE LOOPS HERE
>why doesn't this code work?
Because it has syntax errors and undefined behavior. If you clean it up so that it works, you get this:
Code:// starts declining # include <stdio.h> int main() { int stars; for(stars=5; stars>=1;stars--) // also for stars in range(6,0-1) printf("*\n"); return 0; }
My best code is written with the delete key.
Your code doesn't work;Originally posted by Prelude
[B]>why doesn't this code work?
Because it has syntax errors and undefined behavior. If you clean it up so that it works, you get this:
I assume you didn't understand my question. The code should output what apoc wanted it to.
that is whyI believe the problem is in that statement.Code:printf("*\n" * stars);
>Your code doesn't work;
It works just fine, you just weren't expecting what you got. But let's play on your assumptions a bit if you don't mind:
Even if this were to print "*\n" stars times, you would still only have one * per line, so it wouldn't work the way you want even if it were legal.Code:printf("*\n" * stars);
>I assume you didn't understand my question.
Actually, I did. But to answer it would be to solve the problem for you, and that takes all of the fun out of it.
>The code should output what apoc wanted it to.
While it's possible to only use one loop (I've posted code that does so on this forum before), such a solution isn't meant for beginners and is primarily a curiosity for experienced C users to while away the time. A beginner's solution will always use nested loops.
My best code is written with the delete key.