943,513 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3218
  • C++ RSS
Aug 25th, 2004
0

nesting loops

Expand Post »
I am having a problem regarding nesting loops.for example
for(int x=1;x<10;x++){
for(int y=1;y<x;y++)

i want to know what this statement means. I posted a question to the forum but the moderator has unfortunately taken it off. I need some help regarding the concepts of the program given below.just give me a rough idea in how to do it,


******
*****
****
***
**
*
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
kalinga is offline Offline
7 posts
since Aug 2004
Aug 25th, 2004
0

Re: nesting loops

Kalinga,

The reason I deleted your other threads is because your question is obviously a homework question and you posted no code of your own for us to critique.

I'll leave this thread open on one condition: Post code that you've written, including the part that you're hung up on. Ask specifically what you're not getting, and someone will try to help. Even if the code's completely wrong, someone will try and point you in the right direction.

We're trying to take a tough stance on people just asking us to do their homework. We don't want the reputation that we'll do people's homework-- we're a help site, not a cheat site.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Aug 26th, 2004
0

Re: nesting loops

that means for every value of x u will have x values of y.
the loop inside is execcted x times more than the outsideloop.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
let us c is offline Offline
17 posts
since Aug 2004
Aug 26th, 2004
0

Re: nesting loops

i will post the code for the above question.but it does not work

#include<iostream.h>
int main()
{
for(int x=1;x<10;x++){
for(int y=1;y<10;y++){
cout<<"*";
cout<<"\n";
}
for(int z=y-1;z>=y;z--){
cout<<" ";
cout<<"\n";
}

for(x=z;x>z;x++){
cout<<"*";
}
cout<<endl;
return 0;
}


please check the coding and let me know whats wrong with it.thanks..

kalinga.............
Reputation Points: 11
Solved Threads: 0
Newbie Poster
kalinga is offline Offline
7 posts
since Aug 2004
Aug 26th, 2004
0

nesting loops!!!!somethings wrong!!!help

there is a problem regarding the following program..i mean my coding does not work with it..i will post the program and the coding for it.can anyone of you tell me whats wrong with it.thanks.....

**********
*********
********
*******
******
*****
****
***
**
*
---------------------------------------------
coding
--------------------------------------------
i will post the code for the above question.but it does not work

#include<iostream.h>
int main()
{
for(int x=1;x<10;x++){
for(int y=1;y<10;y++){
cout<<"*";
cout<<"\n";
}
for(int z=y-1;z>=y;z--){
cout<<" ";
cout<<"\n";
}

for(x=z;x>z;x++){
cout<<"*";
}
cout<<endl;
return 0;
}

posted by--(kalinga)
------------------------------------------------
Reputation Points: 11
Solved Threads: 0
Newbie Poster
kalinga is offline Offline
7 posts
since Aug 2004
Aug 26th, 2004
0

Re: nesting loops!!!!somethings wrong!!!help

Thanks for the code! That helps!

So your code is saying:
C++ Syntax (Toggle Plain Text)
  1. Nine times,
  2. Nine times,
  3. print a '*' followed by a new line
So you get 81 lines with one asterisk.

From what you want to print out, it looks like you want something more like this:
C++ Syntax (Toggle Plain Text)
  1. Ten times,
  2. print 10,9,8,7,... '*', followed by a new line
So your code is pretty close, but here are a few ideas:

First, This line:
for (int x = 1; x < 10; x++)

does the following code 9 times, not 10. Why? because the FIRST time is 1 and it stops when i is LESS THAN 10, so it does 1..9.

Mostly folks in c/c++ do loops starting with 0, but you could also just say '<=', so either of these will give you 10 iterations:

for (int x = 0; x < 10; x++) // 0..9
for (int x = 1; x <= 10; x++) // 1..10

Second, the second loop should probably do one fewer iterations each time. A simple way to do that here would be to loop one less time than x:
for (int y = x; y < 10; y++) // will do 10 the first time, 9 the second, etc

Third, you want to print '*' for each y iteration, and THEN print the new line:
C++ Syntax (Toggle Plain Text)
  1. for x
  2. {
  3. for y
  4. {
  5. }
  6. print new line here
  7. }

I'm not sure what the rest of the code is trying to accomplish, but maybe this will get you going in the right direction.

Good luck!
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 26th, 2004
0

Re: merged:nesting loops

I went ahead and merged this thread, just for the sake of continuity. Thanks for your cooperation!
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: incorrect output in Linux environment
Next Thread in C++ Forum Timeline: Input a "password" string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC