In a laboratory experiment, readings of certain measurement are entered instantaneously to a computer
one after the other for processing. Write a C++ program that accepts these readings and computes their
average value at the end of the experiment. The program readss one reading value every execution of an
input operation (cin) but accepts only positive values to compute the average value and discards all
negative values. At the end of the experiment, the sentinel value 1111 is entered as an input to the
program to stop processing and display the results. The sentinel value should not be considered as one of
the valid or invalid readings. The displayed results should include the average value of all valid
readings, the number of all valid readings, and the number of invalid readings. Use proper
formats to display the results.
There is something wrong with my code .. I cant fit the (1111) termination thing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
//Declaring Variables
int x;
float avg;
float sum = 0;
int countv = 0; //Validcounts
int countiv = 0; //Invalidcounts
//Input and Processing
do {
cout << "Please enter the reading: ";
cin >> x;
} while (x >= 0);
while (x > 0)
{
if (x < 0)
{
countiv++;
}
if (x > 0)
{
countv;
sum += x;
}
}
avg = sum / countv;
cout << "The Average value of Valid Readings = " << avg << endl;
cout << "The number of Valid Readings= " << countv << endl;
cout << "The number of Invalid Readings= " << countiv << endl;
system("pause");
}
|