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

Write a complete C++ program to read in, sort, and perform other statistical act

ID: 3829973 • Letter: W

Question

Write a complete C++ program to read in, sort, and perform other statistical actions on a set of data. The program will read from an input file and write all results to an output file.

- From an input file, the program will read a set of student's 3-digit id numbers and their SAT scores.

- The program will sort the id numbers into descending numerical order, being sure to carry along the corresponding SAT scores. The program will then print the sorted lists, giving both id numbers and SAT scores.

- The program will then sort the SAT scores into ascending numerical order, carrying along the corresponding id numbers. The program will print the sorted lists, giving both id numbers and scores.

- Then the program will call functions to find the max and min of the SAT scores and the mean of the SAT scores.

- Finally, the program will read in two scores and call a function search once for each value, to see if the score is found in the array scores.

Details:

The main program will declare two arrays (call these arrays ID and scores or something similar) and an integer variable n.

1. The main program will send the two arrays and the reference parameter n to the readdata function which will read all the data from a file; it will read in n and then read in n data values in parallel into the ID array and the scores array. The data set consists of groups of data, each of which contains a person's 3-digit id number and an SAT score (a number in the range 600 to 2400). Two sample sets of data might be 123 1850 and 456 1260. The arrays and n will have values upon return to main.

The main program will call printarrs to print the original set of data, in columns next to one another in the form of a neat table, with a heading at the top, column headings, etc.

2. Then the main program will send the array of id numbers, the array of scores, and the size n to a sorting function called lsort (that’s an "L", not a one). This function will sort the id numbers into descending numerical order, being sure to maintain the link-up of id numbers and scores (see warning below). This function will use a linear (selection) sort to sort the arrays.

When lsort finishes and returns control to the main program, the main program should call a printing function printarrs to print the two arrays in columns next to one another. Before printing the arrays, print an overall heading (like "Sorted by ID in Descending Order", plus headings for the id numbers and scores columns.

****Maintain the match-up of id numbers and scores. For example, 456 should always be associated with 960, no matter where 456 moves in numerical order; similarly, 123 should stay with 1450.

3. Next the main program will send the same three parameters to a second sorting function, bsort, which will sort the SAT scores into ascending numerical order, being sure to maintain the link-up of id numbers and scores. This function will use the bubble sort to sort the arrays.

When bsort finishes and returns control to the main program, the main program should call printarrs to print the two arrays. Before printing the arrays, print an overall heading, plus headings for the id numbers and scores columns.

4. Next the main program will call a function mean which will find the mean of the SAT scores. (The mean is just another name for the average, so this is not new.) The mean function receives two parameters, the array scores, and n, the number of values in the array. The function will return the mean, which should be a double. The main program will print the mean, together with a message.

5. Next the main program will call a function findlimits which will find the highest SAT score and the lowest SAT score and return both to the main program using reference parameters. The findlimits function receives four parameters: the array scores, the number of values in the array n, and reference parameters max and min. After returning to main, main should print the high and low scores together with messages.

** DO NOT loop through the entire array twice

6. Next the main program will read in a score value from the same input file and call a function search to find the value in the array. The main program will read in a second score value from the flle and also call search to find the value in the array. The function will receive three parameters: the array scores, the value n, and the value to find in the array. The function will return the position of the value in the array or -1 if the value is not found. After each call, the main program will print the score value and the matching ID or else the score value and the message "not found." One of the values you search for should be present in the array; the other should NOT be found in the array, so that you test all lines of code.

Explanation / Answer

#include<iostream>
#include<vector>
#include<fstream>
using namespace std;

void readdata(vector<unsigned int>& IDarray,
   vector<unsigned int>& scoresarry,
   unsigned int& n)
{
   cout << "Enter the number of records to be read : ";
   cin >> n;
   unsigned int ID;
   unsigned int score;
   ifstream fin("SATScores.txt");
   for (unsigned int i=0;i<n;i++)
   {
       fin >> ID >> score;
       IDarray.push_back(ID);
       scoresarry.push_back(score);
   }
}

void readscores(vector<unsigned int>& scoresarrayforsearch, unsigned int n)
{
   unsigned int ID;
   unsigned int score;
   ifstream fin("SATScores.txt");
   //Leaving the values which were already read i.e. n values
   while (n--)
   {
       fin >> ID >> score;
   }
   //Reading rest of the values in the file and storing the scores in
   //scoresarrayforsearch
   while (!fin.eof())
   {
       fin >> ID >> score;
       scoresarrayforsearch.push_back(score);
   }
}

//This is the linear sort function used to sord the ID array in descending order
void lsort(vector<unsigned int>& IDarray,
   vector<unsigned int>& scoresarray,
   const unsigned int& n)
{
   int temp;
   for (unsigned int i = 0; i<n; i++)
   {
       for (unsigned int j = i; j<n; j++)
       {
           if (IDarray[i] < IDarray[j])
           {
               temp = IDarray[i];
               IDarray[i] = IDarray[j];
               IDarray[j] = temp;
               temp = scoresarray[i];
               scoresarray[i] = scoresarray[j];
               scoresarray[j] = temp;
           }
       }
   }
}

//This is the bubble sort function used to sort the scores in ascending order
void bsort(vector<unsigned int>& IDarray,
   vector<unsigned int>& scoresarray,
   const unsigned int& n)
{
   for (unsigned int i = 0; i < n; ++i)
   {
       for (unsigned int j = 0; j < n - i - 1; ++j)
       {
           if (scoresarray[j] > scoresarray[j + 1])
           {
               int temp = scoresarray[j];
               scoresarray[j] = scoresarray[j + 1];
               scoresarray[j + 1] = temp;
               temp = IDarray[j];
               IDarray[j] = IDarray[j + 1];
               IDarray[j + 1] = temp;
           }
       }
   }
}

//Function to print the arrays
void printarrs(const vector<unsigned int>& IDarray,
   const vector<unsigned int>& scoresarray,
   const unsigned int& n)
{
   cout <<" "<<"ID" << " SAT Score" << endl;
   for (unsigned int i = 0; i < n; i++)
   {
       cout <<" "<< IDarray[i] << " " << scoresarray[i] << endl;
   }
}

//Function which return the mean
double mean(const vector<unsigned int>& scoresarray, const unsigned int& n)
{
   double sum = 0.0;
   for (unsigned int i = 0; i < n; i++)
   {
       sum += scoresarray[i];
   }
   return sum / n;
}


//function to return the min and max scores
void findlimits(const vector<unsigned int>& scoresarray, const unsigned int n,
   unsigned int& max, unsigned int& min)
{
   max = scoresarray[n - 1];
   min = scoresarray[0];
}

//Function to search the score and return the index if found else -1
int search(const vector<unsigned int>& scoresarray, const unsigned int n, unsigned int score)
{
   for (unsigned int i = 0; i < n; i++)
   {
       if (scoresarray[i] == score)
       {
           return i;
       }
   }
   return -1;
}

int main()
{
   vector<unsigned int> IDarray;
   vector<unsigned int> scoresarray;
   vector<unsigned int> searchscoresarray;
   unsigned int n=0;
   readdata(IDarray, scoresarray, n);
   cout << " Scores" << endl<<endl;
   printarrs(IDarray, scoresarray,n);
   cout << "######################################" << endl<<endl;
   lsort(IDarray, scoresarray,n);
   cout << " Sorted by ID in Descending Order" << endl<<endl;
   printarrs(IDarray, scoresarray,n);
   cout << "######################################" << endl<<endl;
   bsort(IDarray, scoresarray,n);
   cout << " Sorted by scores in Ascending Order" << endl<<endl;
   printarrs(IDarray, scoresarray, n);
   cout << "######################################" << endl<<endl;
   cout << "Mean Scores : " << mean(scoresarray, n)<<endl;
   unsigned int max = 0, min = 0;
   findlimits(scoresarray, n, max, min);
   cout << "Max Score : " << max << endl << "Min Score : " << min << endl;
   readscores(searchscoresarray, n);
   //Looping through all the values to be searched
   //Experiment with n by giving values 2,3,4
   for (unsigned int i = 0; i < searchscoresarray.size(); i++)
   {
       int searchindex = search(scoresarray, n, searchscoresarray[i]);
       if ( searchindex == -1)
       {
           cout << "Score value : " << searchscoresarray[i] << " Not found" << endl;
       }
       else
       {
           cout << "Score value : " << searchscoresarray[i] << " Matching ID " << IDarray[searchindex];
       }
   }
}


/* SATScore.txt file looks as follows
10 30
15 5
5 15
30 10
4 20
8 30
*/

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