College of Engineering and Computer Science Computer Science Department Computer Science 106 Computing in Engineering and Science Spring 2006 Number Instructor: Larry Caretto Solution to Midterm Exam 1. (30 points) The power output of a hydroelectric dam, P, is given by the formula P = m gh , where is is the mass flow the efficiency of the hydroelectric turbines expressed as a fraction, m rate of water through the turbine in kg/s, h is the height of the water about the turbines in meters and g = 9.808 m/s2 is the gravitational acceleration. With the units specified for the input data, P has units of watts. This number is divided by a conversion factor of 106 watts per MW to get the power output in megawatts, MW. Write the statements necessary to (1) define g as a symbolic constant, (2) prompt a user to enter the efficiency, mass flow rate, and water height in correct units, (3) get the input from the user, and (4) compute and print the power output in megawatts. (You do not have to write a complete C++ program, only the statements necessary to accomplish the stated tasks and to declare the variables that you use.) const double g = 9.808 double mDot, height, efficiency; cout << “Enter the mass flow rate in kg/s: “; cin >> mDot; cout << “Enter the efficiency as a fraction: “; cin >> efficiency; cout << “Enter the water height in meters: “ cin >> height; double power = efficiency * mDot * g * height * 1e-6; cout << “\nFor your input data on the hydroelectric turbine: “ << “\n Mass flow rate = “ << mDot << “kg/s” << “\n Efficiency = “ << efficiency” << “\n Height of water = “ << height << “m\n” << “\nThe power output is “ << power << “MW.” Jacaranda (Engineering) 3333 E-mail: lcaretto@csun.edu Mail Code 8348 Phone: 818.677.6448 Fax: 818.677.7062 Solution to midterm exam Comp 106, L. S. Caretto, Spring 2006 Page 2 2. (40 points) The table below shows program code on the left that reads data from a file. The contents of the data file are shown on the right. What output will the program produce when reading the data from the file shown? double a, b, c; ifstream inFile ( “input.dat” ); inFile >> a >> b >> c; do { double disc = b * b – 4 * a * c; if ( a == 0 ) { cout << “\nError, you entered a = 0.”; } else if ( disc < 0 ) { cout << “\nComplex roots\nReal part = “; << -b/(2*a) << “, Imaginary part = “ << sqrt( -disc ) / ( 2 * a ); } else { cout << “Two real roots: << (-b + sqrt(disc))/(2*a) << “ and “ << (-b - sqrt(disc))/(2*a); } inFile >> a >> b >> c; } while ( a != -9999 ); Contents of data file “input.dat” 1 2 1 1 -1 -6 1 0 4 -9999 -9999 0 12 6 2 1 0 5 -9999 The do-while loop in the program will continue to read data until a value of a = -9999 is read. The initial read outside the do loop will read a = 1, b = 2, c = 1. Then the calculations in the loop will compute: disc = b * b – 4 * a * c = 2 * 2 – 4 * 1 * 1 = 0 the if statement will give disc < 0 = false so the else block will execute this block will output x1 = -2/(2*1) = -1 and x2 = -2/(2*1) = -1 the program will then read a = 1, b = -1, c = -6 from the data file the condition a != -9999 will be true so the loop will continue with another set of calculations disc = b * b – 4 * a * c = (-1) * (-1) – 4 * 1 * (-6) = 25 the if statement will give disc < 0 = false so the else block will execute this block will output x1 = (1 + sqrt(25) )/(2*1) = 3 and x2 = (1 - sqrt(25) )/(2*1) = -2 the program will then read a = 1, b = 0, c = 4 from the data file the condition a != -9999 will be true so the loop will continue disc = b * b – 4 * a * c = 0 * 0 – 4 * 1 * 4 = -16 the if statement will give disc < 0 = true so the first block will execute this block will output Real part = 0/(2*1) = 0 and Imaginary part = sqrt(16) )/(2*1) = 2 the program will then read a = -9999, b = -9999, c = 0 from the data file the condition a != -9999 will be false so the loop will halt The program will give the following output, with spacing as shown Two real roots: -1 and -1 Two real roots: 3 and -2 Complex roots Real part = 0, Imaginary part = 2 Solution to midterm exam Comp 106, L. S. Caretto, Spring 2006 Page 3 3. (40 points) Write a complete C++ program that evaluates b x n dx for user inputs of n, a, and b, using the a b n 1 a n 1 n 1 n 1 b b n a x dx ln a n 1 and ab 0 undefined if n 1 and ab 0 formula to the right. The program should allow the user to continue entering data until the user enters -999.999 for n to indicate that the user wants to stop entering data. Your program should read the user inputs of a, b, and n and print the result computed from the formula shown here. If the user enters n = -1 and values of a and b such that the product ab ≤ 0, you should print a message stating that the integral is undefined. #include <iostream> #include <cmath> using namespace std; int main() { cout << “This program evaluates the integral from a to b of “ << “ x to the power n.\n” << “It will operate repeatedly until you enter a value of “ << “-999.999 for n\n” << “Enter your first set of values for n, a, and b: “; double n, a, b; cin >> n >> a >> b; while ( n != -999.999 ) { cout << “\nFor your inputs a = “ << a << “, b = “ << b << “, and n = “ << n < “, the integral is “ ; if ( n == -1 && ab > 0 ) cout << log( b / a ); else if ( n == -1 ) cout << “not defined.”; else cout << ( pow( b, n – 1 ) – pow( a, n – 1 ) ) / ( n – 1 ); cout << “Enter another value of n or enter -999.999 to exit: “; cin >> n; if ( n != -999.999 ) { cout << “Enter the new values of a and b: “; cin >> a >> b; } } return EXIT_SUCCESS; }