How do i write a program that has only two nested do......while loops that produces:
+ 5 10 15 20 25 30
50 55 60 65 70 75 80
40 45 50 55 60 65 70
30 35 40 45 50 55 60
20 25 30 35 40 45 50
10 15 20 25 30 35 40
How do i write a program that has only two nested do......while loops that produces:
+ 5 10 15 20 25 30
50 55 60 65 70 75 80
40 45 50 55 60 65 70
30 35 40 45 50 55 60
20 25 30 35 40 45 50
10 15 20 25 30 35 40
Well can you produce a single while loop which can generate the first row of the output?
Show us what you know so far, and don't forget the [code][/code] tags when posting code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
this is what i have so far i can output the first line but ican find away to output the rest.Code:#include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); printf("+ "); i=5; do { printf("%4d"); i=i+1; }while(i<=30); }
Remove conio.h/clrscr() from your code, they don't do anything useful and aren't portable.
main needs to return int, not void.
You do "printf("%4d");", but where is the variable you are printing?
Why do you have i=i+1 when you are supposed to go in steps of 5?
Try to indent your code so it's more readable.
Suggest you re-post the code above so that it works, then we'll help with the "rest".
i=i+1 is for adding the value to the next value is added by 5.Code:int main() { int i,j; printf("+"); i=5; do { printf("%4d",i); i=i+5; }while(i<=30); }
ingnore i=i+1, it is suposed to be i=i+5
So now just wrap another loop (and another variable around that code
Replace the ? with whatever you want to achieve the result you're after.Code:int row = ?; do { do { } while ( i <= 30 ); } while ( row ? );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
could you please post the whole code am still having problems
isn't it easier to use 'for'?
Alastor: It's probably homework.
I think the most effective way to do what you want is:
That, in my opinion, is the most effective way to achieve the effect that you want. Notice that the first line is a heading and not part of the actual loop.Code:#include <stdio.h> int main() { printf(" + 5 10 15 20 25 30\n"); for(int i = 50; i >= 10; i-=10) { for(int j = 0; j <= 30; j+=5) { printf("%d ",i+j); } printf("\n"); } return 0; }
If you must you two do...while loops then just set i and j ahead of time and increment them within the loop.
Don't quote me on that... ...seriously
No, the most "effiecient" way to do it it:Meeting their criteria of 2 loops.Code:do { while(0); puts(" + 5 10 15 20 25 30\n" 50 55 60 65 70 75 80\n" 40 45 50 55 60 65 70\n" 30 35 40 45 50 55 60\n" 20 25 30 35 40\n45 50\n" 10 15 20 25 30 35 40"); } while(0);
PS: There's a reason we don't simply DO THE WHOLE GOD DAMN THING FOR THEM. It's so they actually TRY.
Quzah.
Hope is the first step on the road to disappointment.
quzah,
You are my role model.
Woop?
I didn't just give him the answer. I didn't use any do..while loops. He probably turned it in already any ways. I didn't think that it would be a completely new idea and he has probably already seen something like what I put up. I just figured he hadn't made the connection.
I try to show things that the reader will see and go "Oh I see now." even though it isn't exactly what they need. I'm sorry that I went to far on that one.
Don't quote me on that... ...seriously