Help with Sentinel Value!

I have a couple of issues that i need to fix.This program finds the average of a number desired by the user.For example if they put in 3 and then 1,2,3 then the average is 2.
First of all my program is supposed to stop if zero is inputed. so if i put in 3 as the number of integers that will be averaged, and then i type 1,2,0 it should stop and not calculate the average.
Also, i need to account for negative numbers.

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
#include <iostream>
using namespace std;
double avgVal(int, int);
int main()
{
	int amountOfCases;
	cin >> amountOfCases;
	int * numbers = new int[amountOfCases];
	int sum = 0;
	while(numbers !=0)
	{
	for (int i = 0; i<amountOfCases; i++)
	{
		cin >> numbers[i];
		sum = sum + numbers[i];
	}


	cout<<avgVal(sum, amountOfCases)<<endl;
	delete[] numbers;
	}
	system("pause");
	return 0;
}

double avgVal(int sum, int amountOfCases)
{
	return sum / (double)amountOfCases;
}
Last edited on
closed account (48T7M4Gy)
line 6: int ??? maybe amountOfCases perhaps
UPDATE: sorry this is what i need help with What i need it to do is ask the user for a number of cases. The user will input numbers and the program should add the inputs until zero or a negative number is entered and then out put the average of those inputs. The amount of cases is pretty much how many times an average will be done. so if the amount of cases is 4. and the inputs are 1,3,(1+3/2)0 then it should output 2. and that would be ONE case, next inputs are 5,6,4,0(5+6+4/3) the output is 5 and that is case two. etc. Here is my code:


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
#include <iostream>
using namespace std;
double avgVal(int, int);
int main()
{
int amountOfCases;
cin >> amountOfCases;
int* numbers = new int[amountOfCases];
int input=0;
int sum = 0;
for (int i = 0; i<amountOfCases; i++)
{
cin >> numbers[i];
input++; 
if(numbers[i]<0)
cout<<avgVal(sum,input)<<endl;
sum = sum + numbers[i];
}

cout<<avgVal(sum,input )<<endl;


delete[] numbers;
system("pause");
return 0;
}
//This function has the parameters sum, which is the total of all inputs, and 
// amountOfCases which is the number of cases by the user. It will return the average.
double avgVal(int sum, int input)
{
return sum / (double)input;
}
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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int cases;
    int number;
    int sum = 0;
    int count = 0;

    cout<<"Enter number of cases: ";
    cin>>cases;

    while(cases--)
    {
        cout<<"\n\nEnter numbers to find average( Enter 0 to stop input)\n";
        do
        {
            cin>>number;
            sum+=number;
            count++;
        }while(number != 0);

        cout<<"\nThe aveage is "<<sum/(count - 1); // dont include last 0 in the count
        count = 0;  //refresh count for next case;
        sum = 0;    //refresh sum
        cout<<endl;
    }
    system("pause");
    return 0;
}
Topic archived. No new replies allowed.