I need to create a program that gives this output
0000000000
999999999
88888888
7777777
666666
55555
4444
333
22
1
I need help on how to go about with this. THanks
I need to create a program that gives this output
0000000000
999999999
88888888
7777777
666666
55555
4444
333
22
1
I need help on how to go about with this. THanks
Any help is appreciated...
What is your idea and what have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I honestly don't know where to start or I wouldn't be asking this question
Okay. Let's change the question to:
create a program that gives this output
0000000000
Now, what idea do you have to write the program? Refer to what you have learned.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In order to do that all i would need is a single printf statement
printf("0000000000");
How exactly does this help me
Good. That's a valid solution. However, you also wrote in the title to this thread "using for loops". Incorporate that requirement into your solution to this simplified problem in a reasonable way.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Could i start my program off with just that printf statement and then use for loops for the rest of the pyramid?
You certainly could do that if you wanted to. However, that misses the point as you could just have a single printf statement:Originally Posted by g3tb0mbed
If that is an acceptable solution, go for it. But you mentioned "for loops", so the point of my simplified exercise is to get you started on a for loop. If you want to do a printf then use for loops, go ahead, but then if you still ask how to do "the rest of the pyramid", I'll just present you with another simplified problem that amounts to the same thing.Code:printf("0000000000\n" "999999999\n" "88888888\n" "7777777\n" "666666\n" "55555\n" "4444\n" "333\n" "22\n" "1\n");
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No that definitely wont be acceptable. Sorry its my fault im really new to this and missed the first two weeks of class due to being in the hospital. Ill go through the textbook again and see if i can learn anything. Thanks for the help
im trying this question topic
and i get a little question
and this will produce output:Code:#include<stdio.h> int main() { int i = 10, n = 1; int j; while(i >= n) { j = 1; printf("\n"); while(j <= i) { if(i == 10) printf("0"); else printf("%d",i); j++; } i--; } return 0; }
0000000000
999999999
88888888
7777777
666666
55555
4444
333
22
1
like g3tbombed expected
but
if i using global variables outside the loop like this
and this will produce output:Code:#include<stdio.h> int main() { int i = 10, n = 1; int j = 1; while(i >= n) { // j = 1; /*Erase This and Use Global Variables Outside Loop*/ printf("\n"); while(j <= i) { if(i == 10) printf("0"); else printf("%d",i); j++; } i--; } return 0; }
0000000000
and escape sequence '\n' until the outer loop finished
why does this happened? its variable on loop are sensitive case that only use on local variables on loop.
PS: I just want to try, no building him the assignment for him, sorry if i posting this because this will answering the assignment for him.
Thank you in advance.
Because every-time you were entering the while in line 9, j was getting the value 1. Now you are erased it and placed it before while loop, thus it gets the value 1 only once.
Terminology hazard: j is not a global variable.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
i see, so simply said, its only used once on inner loop because J didnt get loop from outer loop ?
i mean repeated j = 1 in outer loop.
Yes, j will get the value of 1, every time we enter the outer loop.
(in the first code)
However, in the second, j will get value 1, only once, before entering the outer loop.
You see the difference is that every line of code that is in the loop, gets executed every-time the loop is been executed.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson