Write a C++ program that handles any number of grades and performs the data anal
ID: 3673284 • Letter: W
Question
Write a C++ program that handles any number of grades and performs the data analysis operations outlined above. Consider the following requirements. a. Read the input data from user. The program must not assume a fixed length of data; it should determine the length dynamically. b. Display the result on a standard display monitor. c. Use functions for each specific operations; for instance, double getAverage(int *grades, int size) double getMedian(int *grades, int size) int getMode(int *grades, int size) double getSd(int *grades, int size) d. If the grades have no mode, the getMode function should return -1. If the grades have more than one mode, the function should return anyone of them. e. Do not accept negative numbers for input.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
double getAverage(vector<int> &grade, int size)
{
int sum = 0;
// Sum all values in our grade
for (int i=0; i< size; i++)
{
sum = sum + grade[i];
}
// Calculate mean
double mean = ((double) sum) / ((double) size);
return mean;
}
double getMedian(vector<int> &grade, int size)
{
sort (grade.begin() , grade.end()); // sorting the vector to find the median
double median = 0;
// If our grade length is even, then we need to find the mean of the two centered values
if (size % 2 == 0)
{
int indexA = (size - 1) / 2;
int indexB = size / 2;
median = ((double) (grade[indexA] + grade[indexB])) / 2;
}
// Else if our grade length is odd, then we simply find the value at the center index
else
{
int index = (size - 1) / 2;
median = grade[ index ];
}
return median;
}
int getMode(vector<int> &grade, int size)
{
int modeCount = 0; // The count of the mode value
int mode = 0; // The value of the mode
int currCount = 0;
int currElement;
// Iterate through all values in our grade and consider it as a possible mode
for (int i = 0 ; i< size ;i++ )
{
// Reset the number of times we have seen the current value
currCount = 0;
// Iterate through the grade counting the number of times we see the current candidate mode
for (int element = 0 ;element < size ; element++)
{
// If they match, increment the current count
if (i == element )
{
currCount++;
}
}
// We only save this candidate mode, if its count is greater than the current mode
// we have stored in the "mode" variable
if (currCount > modeCount)
{
modeCount = currCount;
mode = i;
}
}
if(mode > -1)
return grade[mode];
else return -1;
}
double getSd(vector<int> &grade, int size ,int mean)
{
double sum =0;
for (int i = 0; i < size; ++i)
{
sum = sum + (grade[i]-mean)*(grade[i]-mean);
// applying formula for standard deviation
}
mean = mean/size;
mean = sqrt(mean);
return mean;
}
int main()
{
vector<int> grade; // using vector to dynamically store the numbers
int tempGrade = 0;
bool loop = true;
cout << "Please enter after each of the grades (0-100) you would like to input." << endl;
cout << "Type -1 if you finished entering your grades." << endl;
// since the format of input is not given
// I have assumnet it be a stream of numbers
// enter -1 when you are done with the numbers
while (loop)
{
cin >> tempGrade;
if (tempGrade == -1)
{
loop = false;
}
else if (tempGrade <= 0)
{
cout << "You cannot input any negative numbers. Please input between 0-100." << endl;
cout << "Type -1 if you finished entering your grades." << endl;
}
else if ((tempGrade > 0) && (tempGrade <= 100))
{
grade.push_back(tempGrade);
cout << "enter another grade : ";
cout << "Type -1 if you finished entering your grades." << endl;
}
else
{
cout << "That number is too high! Please input between 0-100." << endl;
}
}
cout << "Your numbers are ";
for (int i = 0; i < grade.size() ; i++)
{
cout << grade[i] << endl;
}
double mean = getAverage(grade, grade.size());
double median = getMedian(grade, grade.size());
int mode = getMode(grade, grade.size());
double sd = getSd(grade,grade.size(),mean);
cout << "mean is: " << mean << endl;
cout << "meadian is: " << median << endl;
cout << "mode is: " << mode << endl;
cout << "standard deviation is: "<<sd<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.