Write a program that dynamically allocates an array large enough to hold a user-
ID: 3621257 • Letter: W
Question
Write a program that dynamically allocates an array large enough to hold a user-definednumber of test scores. Once all the scores are entered, the array should be passed to a
function that calculates the average test score. Here is the function header:
double average(double* score, int numScores)
{
//code to calculate and return average
}
The program should then display the average test score from main().
Then the array should be passed to a function that calculates how many people got an A
on the test. An A is a test score that is > = 90. Here is the function header:
int howManyA(double* score, int numScores)
{
//code to calculate and return the number of A grades
}
The program should then display the number of A grades from main().
In the average function use pointer notation, and in the howManyA function use array
notation.
Release the dynamically allocated memory at the end of the program.
Input Validation: Do not accept non-positive numbers of test scores (e.g. how many test
scores are there?), or numbers outside the range of 0 - 100 for the actual test scores (e.g.
what is score on test 1? test 2?...etc).
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
double average(double* , int);
int howManyA(double* , int );
int main()
{double count;
int n,i;
double* score;
double avg;
cout<<"How many test scores are there? ";
cin>>n;
while(n<=0)
{cout<<"must be > 0 ";
cout<<"How many test scores are there? ";
cin>>n;
}
score=new double[n];
if(score==NULL)
cout<<"error allocationg memory ";
else
{cout<<"Enter scores ";
for(i=0;i<n;i++)
{cout<<i+1<<": ";
cin>>score[i];
while(score[i]<0||score[i]>100)
{cout<<"must be between 0 and 100, please re-enter ";
cout<<i+1<<": ";
cin>>score[i];
}
}
avg=average(score,n);
cout<<"The average test score is: "<<avg<<endl;
count=howManyA(score,n);
cout<<"There were "<<count<<" A's ";
delete []score;
}
system("pause");
return 0;
}
double average(double* score, int numScores)
{int i;
double tot;
for(i=0;i<numScores;i++)
tot+=*(score+i);
return tot/numScores;
}
int howManyA(double* score, int numScores)
{int i,n=0;
for(i=0;i<numScores;i++)
if(score[i]>=90)
n++;
return n;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.