Loops

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");
}
Last edited on
Theres a couple of things here that has to change...

The second while-loop isint doing anything. You can just move the if statements inside of the do-while loop.

Also. Dont forget to add to countv everytime x is bigger than 0.

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
do {
		cout << "Please enter the reading: ";
		cin >> x;

		if (x < 0)
		{
			countiv++;
		}

		if (x > 0)
		{
			
			countv++;
			sum += x;
		}

	}while (x >= 0);

	

		
	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;


Edit: hmm Im trying to understand the instructions. Could you give a written example of what it should do exactly?
Last edited on
Okayy ,, And what about the termination thing ?
Unfortunately, I dont have any output examples :/
Do you want the user to be able to enter positive and negative numbers, and then when you write 1111 it should exit the loop and display how many times each was entered?
Yes, exactly .. display how many times and the average value of the valid (positive) numbers
Then you would want the loop to run until the input is 1111 wouldnt you?

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
int x = 0;
	float avg;
	float sum = 0;
	int countv = 0;  //Validcounts
	int countiv = 0;   //Invalidcounts

	//Input and Processing

	do {

		if (x < 0)
		{
			countiv++;
		}

		if (x > 0)
		{

			countv++;
			sum += x;
		}

		cout << "Please enter the reading: ";
		cin >> x;

		

	}while (x != 1111);

	
	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;
Thankk you, It worked ;)
Glad I could help, a big improvement from your last posts :D
Topic archived. No new replies allowed.