Write a program that takes as input an array of 5 numbers and then displays the
ID: 642101 • Letter: W
Question
Write a program that takes as input an array of 5 numbers and then displays the minimum, maximum, mean, standard deviation and sorted array of the numbers. You are expected to write a class (.cpp) and header (.h) file called Numbers whose member functions implement each of the above mentioned functionality. You should also implement a driver.cpp which tests each of this different functionality.
I wrote the code for this project, but now I need to change it to object oriented. I need to change the code to a class with the 3 different files.
#include <iostream>
#include <iomanip>
using namespace std;
double mean(double[], int, double&);
double standardd(double[], int, double);
double calc(int, double);
char letter(double);
int main()
{
double x[10];
int i = 0, j,h;
double average, sd, sum = 0;
cout << "How many scores will you enter: ";
cin >> h;
for (int k = 0; k < h; k++)
{
cout << "Enter score " << (i + 1) << ": ";
cin >> x[i];
i++;
}
average = mean(x, i, sum); // calls the mean function to set the average score.
sd = standardd(x, i, average); // calls the standardd function to set the standard diviation
cout << " Average: " << average << " ";
cout << "Standard Deviation: " << sd << " ";
cout << setprecision(1) << fixed;
cout << "Letter Grades: " << endl << endl;
for (j = 0; j < i; j++)
cout << x[j] << " --> " << letter(x[j]) << endl; // outputs the letter grades
return 0;
}
char letter(double x) // sets letter grades
{
if (x >= 90)
return 'A';
else if (x >= 80)
return 'B';
else if (x >= 70)
return 'C';
else if (x >= 60)
return 'D';
else
return 'F';
}
double mean(double x[], int n, double& sum) // Calculates the mean adding the scoresand dividing the sum by the number of scores.
{ // returns the meant to the average variable.
int i;
for (i = 0; i < n; i++)
sum += x[i];
return (sum / n);
}
double calc(double x, double avg) // calculates the squre of (score - average) for each score.
{
double temp;
temp = x - avg;
return temp*temp;
}
double standardd(double x[], int n, double average) // calculates the starndard diviation
{ // calls calc function to get the square of (score - average) for each score.
double mean, sum = 0; // adds the values of the squares and divides them by the number of scores.
int i; // sets that value into mean variable
for (i = 0; i<n; i++)
sum += calc(x[i], average);
mean = sum / n; // calculates the square root of the mean
return sqrt(mean);
}
Explanation / Answer
#include
#include
#include
#include
using namespace std;
double Numbers::mean(double x[], int n, double& sum)
{ // returns the mean to the average variable.
int i;
for (i = 0; i < n; i++)
sum += x[i];
return (sum / n);
}
double Numbers::calc(double x, double avg) // calculates the squre of (score - average) for each score.
{
double temp;
temp = x - avg;
return temp*temp;
}
double Numbers ::standardd(double x[], int n, double average) // calculates the starndard diviation
{ // calls calc function to get the square of (score - average) for each score.
double mean, sum = 0; // adds the values of the squares and divides them by the number of scores.
int i; // sets that value into mean variable
for (i = 0; i sum += calc(x[i], average);
mean = sum / n; // calculates the square root of the mean
return sqrt(mean);
}
char Numbers::letter(double x) // sets letter grades
{
if (x >= 90)
return 'A';
else if (x >= 80)
return 'B';
else if (x >= 70)
return 'C';
else if (x >= 60)
return 'D';
else
return 'F';
}
int main()
{ Numbers n;
double x[10];
int i = 0, j,h;
double average, sd, sum = 0;
cout << "How many scores will you enter: ";
cin >> h;
for (int k = 0; k < h; k++)
{
cout << "Enter score " << (i + 1) << ": ";
cin >> x[i];
i++;
}
average =n.mean(x, i, sum); // calls the mean function to set the average score.
sd = n.standardd(x, i, average); // calls the standardd function to set the standard diviation
cout << " Average: " << average << " ";
cout << "Standard Deviation: " << sd << " ";
cout << setprecision(1) << fixed;
cout << "Letter Grades: " << endl << endl;
for (j = 0; j < i; j++)
cout << x[j] << " --> " << n.letter(x[j]) << endl; // outputs the letter grades
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.