1. int numDash=(5 + 8) / 2;
for (i=0; i<numDash; i++)
cout<<"-";
How many dashes will be printed out from the above code?
2. Calculate a test average in a variable named avg and then print it out. The variable avg when printed should have two numbers to the right of the decimal point. (example - 98.56)
The average is calculated by using integer division. Divide an integer variable named earned by an integer variable named max, and then multiply 100. Assume that all of the variables (avg, earned, and max) have been properly declared. (Your answer should be two statements of code).
3. Write the C++ code to get a radius (r) from the user and calculates the area of a circle in a variable called area. You may assume a symbolic constant called PI has been defined. You do not have to print the area, just calculate it after receiving the radius from the user.
The formula is:
Area = Πr2 <-- this formula is (PI * r squared )
Greetings!!!!!
Please find the answers as below.
1. 6 dashes will be printed by given code; Because 8+5 is computed first which gives 13. 13/2 will give 6 in integer form, as result of operation between two integers will be an integer. So value of numDash variable will be computed as 6. For loop will run for value of i=0 to i < 6 (which is 6 times as for values of i=0,1,2,3,4,5 ) print statement will be executed. Screen shot of sample run is as below.
2) Two statement code for the question number 2 is as below. It will never produce output of avg as float value. because we are performing entire calculation ((earned/max)*100) among integers, which will finally return an integer value.Two statement code for the question number 2 is as below.
avg=(earned/max)*100;
cout<<avg;
Screen shot of the above explanation is with sample run is as below.
3) C++ code for the third question is as below.
#include<iostream>
#define PI 3.14159
int main()
{
double area,r;
cout<<"Enter radius (r): ";
cin>>r;
area= PI*r*r;
}
Sample run screen shot of the above code is as follows.
Thank You
Get Answers For Free
Most questions answered within 1 hours.