I asked this question before, but the answer I got did not work. I have tried to
ID: 3627304 • Letter: I
Question
I asked this question before, but the answer I got did not work.I have tried to start this but am unsure how to use an array as an argument in function Calls.
Also I have no idea how to a: keep count of # of grades entered & b: at the end prompt the user to exit or enter grades for another class.
Please help! ---- Will MOST DEFINITELY "score" any answer-ers.
Objectives:
1. Write a program that finds the highest, lowest, mean, and median values of a set of numerical data
2. Use Arrays to store data and sort
3. Use an array as argument in function Calls
Problem statement:
Write a program that will
a) Prompt the user to enter numerical grades of students in a test, one at a time, and store it in an array. This is accomplished in the main module. The user enters a negative number to indicate all the grades are entered. **The program will keep the count of the number of grades entered.**
b) Display all the grades in a tabular form.
c) Call function to calculate the average grade of the class, the median, and the lowest and highest grades.
d) Finally display the class average, highest grade, the lowest grade, and the median at the bottom. **At the end the user is prompted to exit or enter grades for another class.**
Procedure or Details:
The array is passed to the function.
All calculations are done in the function.
The values are displayed in the main module.
The following is an example of the output:
Number Grade
---------- --------
1 65
2 90
3 72
4 86
5 89
6 62
7 82
8 92
9 95
10 68
The class average: 80.1
Highest Grade is: 95
Lowest Grade: 62
Median Grade: 84
**This requires sorting of the array.
Please, if you use an identifier that could be confusing, make a note, and also please make sure there are no errors, for if there are errors, it makes it even more difficult for me to find the right way to do it. I'm using Microsoft visual C++ express, so using the same compiler helps.
The program I had before would not go beyond a negative number, so I could not get it to run the call functions. Also, the program I had before is a HUGE mess, I'll continue trying to fix it, but stating fresh would really help!
I would also like to avoid using the "void" function.
A grade of A+ for an answer that has no errors and is coherent! Thanks!
Explanation / Answer
//I didn't do the median one, but if you get it sorted you just take the value at the middle or average of the middle and that's your median.
#include
using namespace std;
void printGrades(int *a, int len)
{
if(len == 0) cout << " No students entered";
else
{
cout << " Number Grade";
cout << " -------- ----- ";
for (int n=0; ncout << n+1 << " " << a[n] << endl;
}
}
float findMean(int *a, int len)
{
float total = 0;
for (int n=0; ntotal += a[n];
return(total / len);
}
int isHighest(int *a, int len)
{
int high = a[0];
for (int n=0; n{
if(n > 0)
if(high < a[n])
high = a[n];
}
return high;
}
int isLowest(int *a, int len)
{
int low = a[0];
for (int n=0; n{
if(n > 0)
if(low > a[n])
low = a[n];
}
return low;
}
int main()
{
int *grades = NULL; //array to hold grades
int input, n = 0;
while(1) //Builds dynamic array until the user enters a negative
{
cout << "Enter student " << n + 1 << "'s grade (negative to stop): ";
cin >> input;
if (input < 0) break;
grades = (int *) realloc (grades, (n+1) * sizeof(int));
grades[n++] = input;
}
printGrades(grades, n);
cout << " Class mean is " << findMean(grades, n); //output of methods
cout << " Highest grade is " << isHighest(grades, n);
cout << " Lowest grade is " << isLowest(grades, n);
char ans; //ending dialog
cout << " Do you want to enter another class' scores? (y/n) ";
cin >> ans;
if(ans == 'y' || ans == 'Y')
{
main();
cout << " ";
}
free(grades);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.