Can Anyone tell me what is wrong with this program?

This is a discussion on Can Anyone tell me what is wrong with this program? within the C++ Programming forums, part of the General Programming Boards category; Hi... I am new to the forums and new to C++. I have an assignment I have been working on ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    Question Can Anyone tell me what is wrong with this program?

    Hi... I am new to the forums and new to C++. I have an assignment I have been working on and can't find where my error is in the program. I was just wondering if someone could take a glance at it and tell me why my calculations are incorrect. I realize this is easy stuff but I am a beginner. Here is the assignment description:

    Engineers are designing a concrete channel which will bring water to the city reservoir from a near-by lake. The channel will have vertical walls and it will be 15 feet wide. It will have a roughness coefficient of 0.014. The water flow rate in the channel depends on the width, depth of the water, the roughness coefficient, and the slope of the channel. The relationship is given by Manning's equation:

    Q = (1.486 / N) * A * R^2/3 * S^1/2

    R = depth * width/ (2 * depth + width)

    Q is the flow rate in cubic feet per second
    R is the hydraulic radius
    A is the area
    S is the slope in feet/foot
    N is the roughness coefficient

    To help with the design of the channel, engineers want us to produce a table which shows the flow rate for each combination of slope 0.0014, 0.0015, 0.0016, 0.0017, and 0.0018 and depths 6, 8, 10, and 12 feet.

    Write a C++ program that produces this table. Use nested loops to provide the slope and depth values. For example, the index variable on the outer loop can be a double variable with initial value of 0.0014. Input the width and roughness coefficient. The table should start out similar to the one below.

    SLOPE DEPTH FLOW
    ________________________________________________

    0.0014 6.0 797.5963
    0.0014 8.0 1174.9401
    0.0014 10.0 1571.7912
    0.0015 6.0 825.5906
    0.0015 8.0 1216.1786
    0.0015 10.0 1626.9585


    Okay... that is what I got from my instructor. Remember, this is very basic C++... we are only in the second chapter so the functions and operations are very basic.

    Here is my code for this program:


    Code:
    # include <iostream.h>
    # include <iomanip.h>
    # include <math.h>
    
    int main (void)
    {
    
    //Variable declaration
    
    double Q;			// flow rate
    double R;			// hydralic radius
    double A;			// area
    double S;			// slope
    double N = 0.014;	                // roughness coefficient
    double D;			// depth
    double W = 15;		// width
    
    //Output Headings
    
    	cout<<"     SLOPE"<<setw(16)<<"DEPTH"<<setw(16)<<"FLOW"<<endl;
    	cout<<"__________________________________________"<<endl;
    
    //Processing
    
    for (S = 0.0014; S < 0.0019; S += 0.0001)
    {
    	for (D = 6.0; D <= 12; D += 2)
    	{
    	A = D * W;
    
    	R = D * W / (2 * (D + W));
    
    	Q = (1.486 / N) * A * pow(R, 2/3) * pow(S, 0.5);
                    }
    }
    	
    //Format Output
    		cout<<setw(10)<<setprecision(4)<<setiosflags(ios::fixed | ios::showpoint)<<S;
    		cout<<setw(16)<<setprecision(1)<<setiosflags(ios::fixed | ios::showpoint)<<D;
    		cout<<setw(16)<<setprecision(4)<<setiosflags(ios::fixed | ios::showpoint)<<Q<<endl;
    
    }
    
    return 0;
    }
    I am using Microsoft Visual C++ to write my program, only because it came with the book.

    Here is the printout I get when I execute it:

    SLOPE DEPTH FLOW
    __________________________________________
    0.0014 6.0 357.4352
    0.0014 8.0 476.5802
    0.0014 10.0 595.7253
    0.0014 12.0 714.8704
    0.0015 6.0 369.9806
    0.0015 8.0 493.3074
    0.0015 10.0 616.6343
    0.0015 12.0 739.9611
    0.0016 6.0 382.1143
    0.0016 8.0 509.4857
    0.0016 10.0 636.8571
    0.0016 12.0 764.2286
    0.0017 6.0 393.8744
    0.0017 8.0 525.1659
    0.0017 10.0 656.4573
    0.0017 12.0 787.7488
    0.0018 6.0 405.2934
    0.0018 8.0 540.3912
    0.0018 10.0 675.4890
    0.0018 12.0 810.5868
    Press any key to continue

    Well, it looks kinda messed up because everything is crunched together, but I think you can tell what its supposed to be.

    The numbers for the flow dont even come close to what I should be getting according to the example given on my handout. I am not asking anyone to write the program for me, only give any suggestions as to why my output is not correct. I am not sure if it is a problem with decimal's and rounding or what. I appreciate any help I can get

    Thank you in advance for your time!

    GrlNewB
    Last edited by GrlNewB; 03-06-2003 at 10:02 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    Looks like
    Code:
    R = D * W / (2 * (D + W));
    should be
    Code:
    R = D * (W / (2 * (D + W)));
    Division and multiplication have the same precedence so they are evaluated in order from left to right.

    (Don't forget to use code tags in your next posting...read this

    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    Thank you for the help! So making that one change with the parenthesis will change it so my output is correct? That's cool, I didnt know it would be so easy. Thank you!

    Oh, and I noticed the code tag thingy after I posted and made the changes to it, thanks for the reminder though!

    GrlNewB

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Actually, that parenthesis doesn't change anything, you'll get the same answer. It looks like you got the equation wrong, its 2*depth + width, NOT 2*(depth+width).

    So change it to:
    Code:
    R = D * W / (2 * D + W));

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    Thank you PJYelton... I will try that too. I noticed the parenthesis didn't change anything from the last post. Going to see if this works

    GrlNewB

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    aww bummer... that didnt do it either, got the same output as before. Thanks for the help though

    GrlNewB

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I know what your problem is

    In your code:
    Code:
    pow(R, 2/3)
    both 2 and 3 are integers, so 2/3 equates to zero. So you are ending up basically saying:
    Code:
    pow(R, 0)
    change it to either pow(R,double(2)/3) or pow(R,.66667)

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    Doh....(basic algebra), but don't forget that parens do make a difference for things like:
    Code:
        cout << 8 / 4 * 2 << endl;
        cout << 8 / (4 * 2) << endl;
    Also, I think your problem is "2/3", try "2.0/3.0". That should do it.

    gg

    (PJYelton is a quick one )

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    Awesome! It worked!! Thank you all for your help! I changed it to the way you mentioned

    Code:
    pow(R, double (2)/3)
    Cool! I tried them both,

    Code:
    pow(R, 2.0/3.0)
    They both worked! Thank you guys so much!!

    GrlNewB
    Last edited by GrlNewB; 03-06-2003 at 10:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 01:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 04:09 PM

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