help with loops

This is a discussion on help with loops within the C++ Programming forums, part of the General Programming Boards category; I"m very lost with how i should write the code for the loop. I've been stumped for days. Any help ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    help with loops

    I"m very lost with how i should write the code for the loop. I've been stumped for days. Any help would be greatly appreciated.

    //-----------------------For_Loop----------------
    void Call_The_For_Loop(int Numa1, int Numb1, int &sum1)
    { //purpose: uses the for loop to find the sum of the numbers between( and including) Numa1 and Numb1
    sum1 = 0;
    Put in the code for the for loop here
    }// end For_Loop function

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Code:
    for(int i = Numa1; i<=Numb1; i++)
    {
    sum1 += i;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    pesudocode:
    Code:
       somevalue = num1
       somevalue2 = num2
       biggest = whateveris bigger, num1 or num2
        smallest = whatever is smaller, num1 or num2
    
    loop initialization:
        sum = 0, count = smallest
        count < biggest
        count++
    
    loop body:
        sum += count
    This is far to easy for me to actually provide the loop for you. Read the above, it details (exhaustivly) what you need to do.

    [edit]
    **** it. Never mind. Everyone does everyone elses homework for them anyway. What's the point. No one wants to actually learn anything.
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Ahhhhh, yes but your way was far better.

    I assumed that Numb1 was largest.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    2
    thank you for the psuedocode. it helped me understand a little.

    could you help exlpain to me how I would do the same thing using a while loop and do while loop.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the same as my pseudocode. Just change where you put the initializers, and where you put the check. A while loop has initializers before the loop itself, the check in the while( ) block, and the incrementors inside the loop. A do-while has the initializers before the loop, the incrementation inside the loop, and the check at the end of the cycle, in the while( ) block.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Code:
    initialise counter
    
    initialise sum
    
    set counter to low number
    
    do add counter to sum
    
    increment counter
    
    while counter is not greater than high number
    Code:
    initialise counter
    
    initialise sum
    
    set counter to low number
    
    while counter is not greater than high number
    
    add counter to sum
    
    increment counter
    I suck at pseudocode so hopefully this helps, if not then sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 11:09 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21