Thread: Troubleshoot Please

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Troubleshoot Please

    hi,

    can someone pls help? thx!

    i'm supposed to program this: user guesses the depth of water in a channel. the program will calculate the flow rate (Q). if Q is between 999 and 1001, the depth is deemed a correct guess. else will keep guessing till it falls within the range.

    the answer is ideally 6.92. however, input of 7, 8, 9, 10 all states that the guess is too low.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    double depth, width = 15, Q, N = 0.014, A, R, S = 0.0015;
    
    /*double hydraulic_radius_eqn(double R)
    {
    	R = (depth*width)/(2.0*depth + width);
    	return R;
    }*/
    
    double flow_eqn(double Q)
    {
    	Q = 1.486*A*pow((depth*width)/(2.0*depth + width),2/3)*pow(S,1/2)/N;
    	return Q;
    }
    
    main(void)
    {
    	printf("This program informs the user if the guessed depth of water us too high or low.\n");
    	printf("The channel of water has vertical walls 15 feet wide, 10 feet deep.\n");
    	printf("The slope is 0.0015 feet/foot, roughness coefficient of 0.014.\n");
    	printf("The flow rate is 1000 cubic feet per second.\n");
    	printf("You are to guess the depth of water until you are within 0.1 percent of the desired flow.\n\n");
    	printf("Guess the depth of water in the channel: ");
    	scanf("%lf", &depth);
    	Q = flow_eqn(depth);
    
    
    	while (Q>1001 || Q<999){
    		if (depth<=10){
    
    			if(Q>1001){
    				printf("Your guess is too high. Please guess a smaller depth.\nDepth: ");
    				scanf("%lf", &depth);
    				Q = flow_eqn(depth);
    			}
    			else{
    				printf("Your guess is too low. Please guess a larger depth.\nDepth: ");
    				scanf("%lf", &depth);
    				Q = flow_eqn(depth);
    			}
    		}
    		else {
    			printf("The channel is 10 feet tall. Please guess a water depth lesser than 10 feet.\nDepth: ");
    			scanf("%lf", &depth);
    		}
    	}
    	printf("You guessed it! The water depth is %lf feet!", &depth);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum!

    Sorry Xinglong, I read your equation incorrectly.
    Last edited by Adak; 03-03-2010 at 10:04 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    may i know which part of the eqn is wrong?

    from what i learnt, pow(a,b) means a^b.

    thanks!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    Q = 1.486*A*pow((depth*width)/(2.0*depth + width),2/3)*pow(S,1/2)/N;
    Integer division can be problematic (truncation) and cause unexpected results to those who don't know what to look for. You may want to try using floating point values (simply add ".0" to these values to make them floating point so the division produces expected values.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    thanks! it works already!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic troubleshoot true and false
    By joker_tony in forum C Programming
    Replies: 1
    Last Post: 03-08-2008, 04:53 PM