Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Predict the outcome of the following code segments. Lets assume all the proper header files are already included. (20%) (1) double a, b; a

1. Predict the outcome of the following code segments. Lets assume all the proper header files are already included. (20%)

(1)

double a, b;

a = 3 / 4 * 3.14 * pow(3, 2);

b = 3.14 * pow(3, 2) * 3 / 4;

cout << a << endl << b << endl;

0

21.195

(2)

double e1 = 1.234, e2 = 5.678;

cout << setprecision(2) << e1 << endl;

cout << setprecision(2) << fixed << e2 << endl;

2. Given the code below, write an if structure to display the maximum number among x, y, and z. (20%)

double x, y, z;

cin >> x >> y >> z;

3. Write a program to ask the user to enter a number and display its square root. If the user enters a negative number, your program should ask the user to re-enter the number again repeatedly until the user finally enters a zero or a positive number. Then your program shall display its square root. You may assume that the user only enters a number, not letters or symbols. Therefore there is no input failure. (30%)

Hint: (1) Use a while loop or a do-while loop for the repetition.

(2) The square root function sqrt() is defined in cmath.

4. Write a program of two functions. The main function asks the user to enter 5 numbers and stores them in an array. Then the main function should pass the array to the second function calcAvg(). The function calcAvg() will calculate the average of the array and return the result to the main function. Finally, the main function will display the average on the screen. (30%)

You may assume that the user only enters numbers, not letters or symbols. Therefore there is no input failure.

Hint: (1) The calcAvg() function should take 2 parameters, one is for the array, the other is for the size of the array.

(2) Use a for loop to compute the sum, and divide it by the size to get the average.

If you prefer, you can read the 5 numbers from a file, and write the average to another file.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

Students also viewed these Databases questions