Nested Loops

This is a discussion on Nested Loops within the C++ Programming forums, part of the General Programming Boards category; I am in dire need of figuring out how to run this program using nested loops. It's a Van der ...

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Nested Loops

    I am in dire need of figuring out how to run this program using nested loops. It's a Van der Waal equation assignment, but I need to use nested loops for incremental increases. Here's what I have so far.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdio.h>
    using namespace std;
              
    // Define constants.
    
    #define a 3.952
    #define b 0.0427
    #define R 0.08206
    #define mL_L .001
    
    int main ()
    {
        float moles;            //IN: Number of moles of CO2 (n).
        float temp;                //IN: Temperature (K).
        float initial;            //IN: Initial volume (mL).    
        float final;            //IN: Final volume (mL).
        float increment;        //IN: Increment volume (mL) b/w lines of the table.
        float pressure;            //OUT: Pressure (atm).
        float loopVol;            //Variable assignment for loop function.
        float volume;            //Variable assignment for calculations.
        
        //Request user input.
        
        cout << "\nEnter the quantity of Carbon Dioxide (moles): ";
        cin >> moles;
        cout << "\nEnter the Temperature (K): ";
        cin >> temp;
        cout << "\nEnter the Initial Volume (mL): ";
        cin >> initial;
        cout << "\nEnter the Final Volume (mL): ";
        cin >> final;
        cout << "\nEnter the Volume Increment (mL): ";
        cin >> increment;
        
        //Set for table.
        
        cout << "\n\nVolume (mL)                    Pressure (atm)" << endl;
        
        //Begin calculations.
        
            for(loopVol = initial; loopVol <= final; loopVol += increment)
                {
                printf("  %.2f", loopVol);
                volume = loopVol * mL_L;
                pressure = (moles * R * temp)/(volume - b * moles) - (a * moles * moles)/(volume * volume);
            
                printf("                             %.4f",pressure);        
                } while (loopVol <= final);
            
            return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    Yes, I posted that, too. But, as you'd notice, they weren't able to help :/.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    There is no nested loop in that code. There are two separate loops, a for loop and a while loop. They are not nested.

    This is a rewrite of lines 43-50 of your code, with indenting and comments changed to reflect what the code DOES.

    Code:
    for(loopVol = initial; loopVol <= final; loopVol += increment)
    {
          printf("  %.2f", loopVol);
          volume = loopVol * mL_L;
          pressure = (moles * R * temp)/(volume - b * moles) - (a * moles * moles)/(volume * volume);
         
          printf("                             %.4f",pressure);       
    }    // for loop ends here
    
    // and a new while loop here
    while (loopVol <= final);
    The while loop will do nothing, since loopVal will be equal to final+1 after completion of the preceding for loop.



    Nobody can help you more than that. Van der Waal's equation does not involve loops, and you have been very unclear on what you're trying to achieve, let alone why you think nested loops will achieve it.

    If you believe there is a nested loop in your code, you're in trouble. You need to go back to your textbook, and read the section on loops, in order to understand what a nested loop actually is.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with nested loops?? Please help!
    By GoBlue13 in forum C++ Programming
    Replies: 12
    Last Post: 02-28-2013, 06:27 AM
  2. Nested For loops
    By Dagda in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2012, 12:45 PM
  3. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  4. nested for loops/
    By o0o in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 09:19 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 10:44 AM

Tags for this Thread


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