Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need C++ program that calculates statistics on a set of test scores (test scores

ID: 3662813 • Letter: N

Question

Need C++ program that calculates statistics on a set of test scores (test scores are filed in a folder).

The scores should be stored in a dynamically allocated array.

The program should:

- Open a file that has a set of test scores. The first value is the number of scores in the file. (NEED TO BRING IN THE FILE FOR EXAMPLE NAMED scores).

-Allocate an array to hold the scores. Only dynamic allocation will be accepted.

- Should read the test scores into the array. Have a separate function to read the scores.

- should Sort the scores in ascending order using quicksort method.

-Print the sorted scores from least to greatest ( Have a separate function to print the scores).

-Calculate and print the average, median and mode of the scores (seperate functions for all three of them).

Suggestion: build a frequency array to aid in the mode calculation.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

int partition(int* l,int low,int high){
int pivot = l[high];
int index = low;
for (int j = low+1; j < high; j++){
if (l[j] <= pivot){
int temp = l[index];
l[index] = l[j];
l[j] = temp;
index++;
}
}
int temp = l[index];
l[index] = pivot;
l[high] = temp;
return index;
}
void quick_sort(int* l,int low,int high){
if (high > low){
int pos = partition(l,low,high);
quick_sort(l,low,pos-1);
quick_sort(l,pos+1,high);
}
}

void print(int* l,int n){
cout << "------- SORTED ARRAY ----- " << endl;
for (int i = 0; i < n; i++)
cout << l[i] << " ";
cout << endl;
}

void read(int* l,int n){
ifstream infile;
infile.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
string str;
try {
cout << "Enter the file name : ";
cin >> str;
infile.open(str.c_str());
infile >> n;
l = new int[n];
for (int i = 0; i < n; i++)
infile >> l[i];
}
catch (std::ifstream::failure e) {
std::cerr << "The File ---- input.txt ---- is not Here ";
}
}

void average(int* l,int n){
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += l[i];
cout << "Average is : " << (sum/n) << endl;
}

void median(int* l,int n){
cout << "Median is : " << l[n/2] << endl;   
}

int maximum(int* l,int n){
int mx = INT_MIN;
for (int i = 0; i < n; i++)
mx = max(l[i],mx);
return mx;
}

void mode(int* l,int n){
int* temp = new int[l[n-1] + 1];
for (int i = 0; i < n; i++)
temp[l[i]] += 1;
cout << "Mode is : " << maximum(temp,l[n-1] - 1) << endl;
}
  
int main(){
int* l;
int n;
read(l,n);
quick_sort(l,0,n-1);
print(l,n);
average(l,n);
median(l,n);
mode(l,n);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote