322,289 views
16 votes
16 votes
Part1) Given 3 integers, output their average and their product, using integer arithmetic.

Ex: If the input is:
10 20 5
the output is:
11 1000
Note: Integer division discards the fraction. Hence the average of 10 20 5 is output as 11, not 11.6667.
Note: The test cases include three very large input values whose product results in overflow. You do not need to do anything special, but just observe that the output does not represent the correct product (in fact, three positive numbers yield a negative output; wow).
Submit the above for grading. Your program will fail the last test cases (which is expected), until you complete part 2 below.
Part 2) Also output the average and product, using floating-point arithmetic.
Output each floating-point value with two digits after the decimal point, which can be achieved by executing
cout << fixed << setprecision(2); once before all other cout statements.
Ex: If the input is:
10 20 5
the output is:
11 1000
11.67 1000.00

User Derked
by
2.5k points

1 Answer

18 votes
18 votes

Answer:

The program is as follows:

#include <iostream>

#include <iomanip>

using namespace std;

int main(){

int num1, num2, num3;

cin>>num1>>num2>>num3;

cout << fixed << setprecision(2);

cout<<(num1 + num2 + num3)/3<<" ";

cout<<num1 * num2 * num3<<" ";

return 0;

}

Step-by-step explanation:

This declares three integer variables

int num1, num2, num3;

This gets input for the three integers

cin>>num1>>num2>>num3;

This is used to set the precision to 2

cout << fixed << setprecision(2);

This prints the average

cout<<(num1 + num2 + num3)/3<<" ";

This prints the product

cout<<num1 * num2 * num3<<" ";

User Ajushi
by
3.1k points