GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Nov-2004, 15:59
sammacs sammacs is offline
New Member
 
Join Date: Nov 2004
Posts: 12
sammacs is on a distinguished road

messy loop help please


Hi, how are you all?
I'm a C++ novice and am having trouble with this fairly messy loop structure I've created. At the moment it's an infinite loop and I'm not sure why. The goto part should exit the whole loop and the break part should go back to the main, start while loop. 'c' is an integer the user enters (3 for example) which determines how many values are in the array. The loop is meant to go through the array and find the highest entry (also input by the user). I hope you can understand what it's trying to do.
I'd appreciate any help on getting it to work.
Thanks
Sam



CPP / C++ / C Code:
    n = 0;
    g = 0;
    
    while (n <= (c-1))
    {
        g = (n + 1);
        while (g <= (c-1))
        {
            if (parray[n] > parray[g])
            {
                g++;
                if (g >= c)
                {
                    goto end;
                }    
            }    
            else
            {
                n = g;
                break;
            }
        }  
    }
    
    end: cout << "\n" << narray[n] << " has the largest population" << endl;
  #2  
Old 25-Nov-2004, 17:00
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
I don't feel extremely comfortable w/ C++ yet (only been using for <1 year), but I have learned a few things.

First off, don't use goto. It's not used in structured programming and usually it's use means the programmer didn't design the program well. You should never have to use a goto statement because there are better ways to accomplish the same idea. In this code, just replace the "goto end" with your cout statement; there's no value-added by using "goto". If written correctly, the loop will exit normally with no problem.

Additionally, I'm careful about using breaks. Usually I only use them with switch statements or nested if/else statements. Loops should be written so the controlling argument stops the loop gracefully, e.g. when the loop condition becomes false.

However, I don't see where 'n' is ever incremented. If 'n' remains equal to '0', then you will always be in a loop because
CPP / C++ / C Code:
n <= (c-1)
will always be true; i.e. 'n' will never become greater than c-1.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 25-Nov-2004, 17:08
sammacs sammacs is offline
New Member
 
Join Date: Nov 2004
Posts: 12
sammacs is on a distinguished road
Thanks for the input. I don't know of any other way to get out of multiple loops other than goto though. Replacing it with the cout stuff would still mean the outer loops would be executed more times. n is incremented by being equal to g after g is incremented ( the line 'n = g' ).
Any other ideas?
thanks
Sam
  #4  
Old 25-Nov-2004, 20:07
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
While loops will automatically "break" and return control to the outer loop when the stopping condition is reached (if it's only one loop then it returns control to main() ), so if your loop is coded correctly you shouldn't require a break or goto statement. (Hope that makes sense.)

You can use a forever loop instead of a while loop; it's a new feature of C++. The format is
CPP / C++ / C Code:
for(;;)
{
statements;
//stopping condition
if(i=0)
    cout << "blah, blah, blah";
break;
//more statements if needed
}
This may be more intuitive for you.

Another thing I noticed is that your conditional for the goto line may be wrong.
CPP / C++ / C Code:
if (g >= c)
 {
goto end;
} 
If you want to ensure the loop doesn't continue, you need to make sure the conditional guarantees a stop. What I mean is, maybe it should be (g=c) instead of (g>=c). Your way implies that the loop will continue and run the goto line as long as g keeps getting incremented.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #5  
Old 25-Nov-2004, 21:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
Additionally, I'm careful about using breaks. Usually I only use them with switch statements or nested if/else statements.
breaks do nothing in if/else statements. They exit loops only and cause errors if not in a valid structure (loop or switch).


Quote:
Originally Posted by crystalattice
You can use a forever loop instead of a while loop; it's a new feature of C++. The format is
CPP / C++ / C Code:
for(;;)
{
statements;
//stopping condition
if(i=0)
    cout << "blah, blah, blah";
break;
//more statements if needed
}
This may be more intuitive for you.
It's an old feature of C, therefore it's always been in C++. Another way to do the 'forever loop' (and the format I personally like) is
CPP / C++ / C Code:
while(1)
instead of the for() format.

Also, you might find the forever loop works better like this:
CPP / C++ / C Code:
for(;;)
{
    /* statements */
    if (i == 0)  // breakout condition
    {
        break;
    }
    /* more statements if needed */
}
This will break only on the exit condition, not first time thru the loop as originally posted. Also, watch your indenting. All statements within the for loop should be indented.


sammacs, is your task simply to find the index of the largest value in the array? If so, you are making it much more complicated than it needs to be.

If n is to be the index of the largest value,
  • Set n to 0
  • start a for() loop from 1 to < c
  • check the parray values and if n points to a lesser value, change it.
  • when the loop is finished, n points to the largest value.
You don't need nested loops for this, no compound ifs, and no gotos nor breaks
__________________

Age is unimportant -- except in cheese
  #6  
Old 26-Nov-2004, 10:26
sammacs sammacs is offline
New Member
 
Join Date: Nov 2004
Posts: 12
sammacs is on a distinguished road
Hi,
I've managed to get it to work. Thanks for all your help. I used a while loop in the end but used a similar format to what WaltP suggested. It turned out very simple in the end.
Thanks again,
Sam
  #7  
Old 26-Nov-2004, 15:18
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Hey Walt, thanks for the catches. I was thinking of the fact that switch can be used in placed of if/else and forgot that if/else doesn't require a break.

I didn't know that forever loops were part of C from the beginning. My C textbook never mentioned it and my C++ book dedicated a whole section to them, making it sound as if forever loops were a new feature of C++.

Guess I need more practice. <shrug>
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 13:02
[Header]Classifying the FOR loop mithunjacob C++ Forum 0 20-Jun-2004 16:39
Program using a for loop tommy69 C Programming Language 3 10-Mar-2004 22:16
coding a sentinel controlled loop tommy69 C++ Forum 2 10-Mar-2004 14:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.