Finding the average of an array

I am attempting to write a program that takes a number(picked by the user) of test scores in an array then calculates the average and outputs the number of passed and failed tests.

On a side note I am attempting to use array notation in the main function and pointer notation in the other functions

Any help would be greatly appreciated

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
 #include<iostream>
#include<iomanip>
using namespace std;

int numScores;
int counter = 0;
double *scores;

//Function Prototypes
double average(double*, int);
void passOrFail(double*, int, int &, int &);

//I'm supposed to prevent the user from inputting scores that are negative or greater than 0 but I'm not sure how to do that.
//also the for loop does not work passed the first input
int main(){
	cout << "How many test scores? "; cin >> numScores; cout << endl;
	scores[numScores];
	for(counter = 0; counter < numScores; counter++){
		cout << "Please enter score " << counter + 1 <<": "; 
		cin >> scores[0]; cout << endl;
		scores++;
		}
	
	//cout << "The average score is: " << average(scores[numScores], numScores);
	return 0;
	system("Pause");
}

//tried to do this recursively but I don't know if there is a better way
double average(double *scores, int numScores){
	if(numScores <= 0){
		return 0;
	}
	else{
		return (*scores + average(scores+1, numScores - 1))/numScores;
	}
	
	//Not sure what to do for this function
	//void passOrFail(double *scores, int numScores, int & pass, int & fail){
	
	//}
}
Umm the comment on line 13, I'm guessing you mean you want to prevent the user from entering values that are negative or less than zero?

line 17 should be scores = new double[numScores];
line 20 should be cin >> scores[counter]; cout << endl;
Or if you want to use pointers to get the values (which is what I think you are attempting on line 21), you can do cin >> *scores; cout << endl;.


For your average function, there are a number of ways of doing it recursively. I will show you the easier way:

1
2
3
if(*score == NULL)
   return 0;
return ((*scores/numScores) + average(++scores, numScores));


Haven't tested it yet, but it seems like it should work


For passOrFail, you should have a way of checking if the value the user entered is within a certain range and act accordingly

#define isBetween(A, B, C) ( ( (A <= B) && (B <= C) ) )

I found that this works.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>

using namespace std;

int NumberOfScores;
int counter = 0;
double scores[128];
double tmp = 0;
double average;

bool negative = false;

void TryAgain();

int main()
{
	cout << "Enter the number of scores you are inputing: ";
        cin >>NumberOfScores;
	
	do
	{
				cout << "Enter the " << counter+1 << " score: ";
                        	cin >> scores[counter];
				
				if(*scores < 0)
				{
					cout << "Cannot enter negative numbers! Try again!" << endl;
					negative = true;
					TryAgain();
					counter++;
					tmp = tmp + *scores;
				}
				else
				{
					counter++;
					tmp = tmp + *scores;
				}
	}
	while(counter < NumberOfScores);

	average = tmp/counter;

	cout << "The average is: " << average << endl;

	system("pause");
	return 0;
}

void TryAgain()
{
	while(negative)
	{
		cout << "Enter the " << counter+1 << " score: ";	cin >> scores[counter];
		if(*scores > 0)
		{
			negative = 0;
			return;
		}
	}
}


I hope I helped! :)
Last edited on
Thank you both for the help, I really do appreciate it!

Oh and when I said greater than 0 I meant greater than 100 because a score could not exceed 100% but I can figure that out based on what you told me, thanks again for all the help.
Last edited on
Topic archived. No new replies allowed.