6) Write the I/O flow, draw a flowchart, and write a C++ program to request 4 integers from a user. Your program should calculate and display the total and average for the numbers. Show the average value to 2 decimal places.

Example:
```
Enter 4 numbers: 2 3 2 3
Total: 10
Average: 2.50
```



Answer :

### Input/Output Flow

1. Input: User is prompted to enter four integers.
2. Processing:
- Read the four integers.
- Calculate the total of the four integers.
- Calculate the average of the four integers.
3. Output:
- Display the total sum of the four integers.
- Display the average of the four integers, rounded to 2 decimal places.

### Flowchart

Below is a textual representation of the flowchart. You can visualize it using any flowchart drawing tool:

- Start
- Prompt the user to input four integers
- Read the four integers (num1, num2, num3, num4)
- Calculate the total: total = num1 + num2 + num3 + num4
- Calculate the average: average = total / 4
- Display the total
- Display the average rounded to 2 decimal places
- End

### C++ Program

```cpp
#include
#include // For std::setprecision

int main() {
int num1, num2, num3, num4;
std::cout << "Enter 4 numbers: ";
std::cin >> num1 >> num2 >> num3 >> num4;

int total = num1 + num2 + num3 + num4;
double average = total / 4.0;

std::cout << "Total: " << total << std::endl;
std::cout << "Average: " << std::fixed << std::setprecision(2) << average << std::endl;

return 0;
}
```

### Explanation of the C++ Program

1. Include necessary headers:
- `#include ` is needed for input and output operations.
- `#include ` is used to control the precision of the floating-point output.

2. Main function:
- Declare four integer variables to store the user inputs.
- Prompt the user to enter four integers and read them using `std::cin`.
- Calculate the total of the four numbers.
- Calculate the average by dividing the total by 4.0 to ensure the division is performed in floating-point.
- Print the total sum of the numbers.
- Print the average, using `std::fixed` and `std::setprecision(2)` to format the output to display two decimal places.
- Return 0 to indicate the successful completion of the program.

This program follows the required input/output flow, computes the total and average as expected, and formats the average to two decimal places.

Other Questions