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,
2
and display the average of the inputted values.
3
# Input specification
4
A sequence of double values until user input -1.
5
You can assume that users will always input double values.
6
# Output specification
7
If 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
10
so there will always be an average value computed by your program.
11
# Example
12
Suppose user input the following:
13
2.5 2.5 3.5 1.5 -1
14
The program output the following:
15
2.5
16
*/
17
18
#include <iostream>
19
using namespace
std;
20
21
int
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
}
main
int main()
Definition
checkpoint3.9.cpp:21
Module3
checkpoint3.9.cpp
Generated on Sun Sep 29 2024 16:39:53 for COMP2113 by
1.12.0