User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,974 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,754 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2342 | Replies: 6
Reply
Join Date: Aug 2004
Posts: 7
Reputation: kalinga is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kalinga kalinga is offline Offline
Newbie Poster

nesting loops

  #1  
Aug 25th, 2004
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,


******
*****
****
***
**
*
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: nesting loops

  #2  
Aug 25th, 2004
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.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Aug 2004
Location: india,AP
Posts: 17
Reputation: let us c is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
let us c's Avatar
let us c let us c is offline Offline
Newbie Poster

Re: nesting loops

  #3  
Aug 26th, 2004
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.
Reply With Quote  
Join Date: Aug 2004
Posts: 7
Reputation: kalinga is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kalinga kalinga is offline Offline
Newbie Poster

Help Re: nesting loops

  #4  
Aug 26th, 2004
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.............
Reply With Quote  
Join Date: Aug 2004
Posts: 7
Reputation: kalinga is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kalinga kalinga is offline Offline
Newbie Poster

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

  #5  
Aug 26th, 2004
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)
------------------------------------------------
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

  #6  
Aug 26th, 2004
Thanks for the code! That helps!

So your code is saying:
Nine times,
    Nine times,
        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:
Ten times,
    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:
for x
{
    for y
    {
    }
    print new line here
}

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!
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: merged:nesting loops

  #7  
Aug 26th, 2004
I went ahead and merged this thread, just for the sake of continuity. Thanks for your cooperation!
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC