COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
checkpoint3.9.cpp
Go to the documentation of this file.
1/* Please write a program that reads user input repeatedly until -1 is entered,
2and display the average of the inputted values.
3# Input specification
4A sequence of double values until user input -1.
5You can assume that users will always input double values.
6# Output specification
7If the user enters -1, the program will print out the average of the values input
8(excluding the -1 in the calculation of average value) and the program ends
9(without ending newline at the end). You can assume that users will input at least one value
10so there will always be an average value computed by your program.
11# Example
12Suppose user input the following:
132.5 2.5 3.5 1.5 -1
14The program output the following:
152.5
16*/
17
18#include <iostream>
19using namespace std;
20
21int main(){
22 double values, inputs; int count = 0;
23 while (true){
24 cin >> inputs;
25 if (inputs == -1){
26 break;
27 }
28 else {
29 values += inputs;
30 count += 1;
31 }
32 }
33 if (count){
34 cout << values/count;
35 }
36 return 0;
37}
int main()