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
#include<iostream>
#include<iomanip>
usingnamespace 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 = newdouble[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:
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.