//Natalie Zarate CSC 5 Chapter 9, P. 538, #4
/*******************************************************************************
*
* DISPLAY SCORES
*
* This program will accept integer test scores and names for the user, then it
* will calculate the average score. After the average score is caluculated, the
* program will display the students scores in ascending order and the average
* score
*______________________________________________________________________________
* INPUT
* student_names[] - names of test-takers
* test_scores[] - student test scores
******************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*******************************************************************************
* Function GetStudentNames
*
* The function will get the names of the test-takers/students and store them in
* an array
* _____________________________________________________________________________
* INPUT
* num - number of students who took the test
*
* OUTPUT
* names - names of students/test-takers
* ****************************************************************************/
string *GetStudentNames(int num);
/*******************************************************************************
* Function GetTestScores
*
* This Function will prompt the user for the test scores for a previously
* defined number of tests, then store it in an array.
* _____________________________________________________________________________
* INPUT
* num - number of tests submitted
*
* OUTPUT
* scores - score for each student
* ****************************************************************************/
int *GetTestScores(int num);
/*******************************************************************************
* Function SortArray
*
* This function will sort test scores in ascending order.
* _____________________________________________________________________________
* INPUT
* names - names of students
* scores - students' scores
* num - number of tests submitted
* initial_ val - value of element that is being used as reference
* min_subscript - subscript of minimum value
* min_value - value of (current) minimum element
* bookmark - holder for name
*
* OUTPUT
* names - students' names
* scores - students' scores
* ****************************************************************************/
void SortArray(string *names, int *scores, int num);
/*******************************************************************************
* Function GetAverageScore
*
* This function will compute the average score of the scores provided.
* _____________________________________________________________________________
* INPUT
* scores - students' test scores
* num - number of tests
* score_sum - sum of students' scores
*
* OUTPUT
* average - average test score
* ****************************************************************************/
float GetAverageScore(int *scores, int num);
/*******************************************************************************
* Function OuputScores
*
* This function will display student names, test scores, and the average
* overall test score.
* _____________________________________________________________________________
* INPUT
* names - students' names
* scores - students' scores
* num - number of tests
* average - average test score
* ****************************************************************************/
void OutputScores(string *names, int *scores, int num, float average);
int main()
{
string *student_names; // INPUT - names of test takers
int *test_scores; // INPUT - student test scores
int tests; // INPUT - number of tests
float average_score; // INPUT - average test score
// Prompt user for number of test scores
cout << "Enter Number of Test Scores: " << endl;
cin >> tests;
// Call GetStudentNames
student_names = GetStudentNames(tests);
// Call GetTestScores
test_scores = GetTestScores(tests);
// Call SortArray
SortArray(student_names, test_scores, tests);
// Call GetAverageScore
average_score = GetAverageScore(test_scores, tests);
// Call OutputScores
OutputScores (student_names, test_scores, tests, average_score);
// Deallocate memory
delete [] student_names;
delete [] test_scores;
return 0;
}
/*******************************************************************************
* Function GetStudentNames
*
* The function will get the names of the test-takers/students and store them in
* an array
* _____________________________________________________________________________
* INPUT
* num - number of students who took the test
*
* OUTPUT
* names - names of students/test-takers
* ****************************************************************************/
string *GetStudentNames(int num)
{
string student; // INPUT - student name
string *names; // OUTPUT - names of students who took the test
// Dynamically allocate memeory
names = new string[num];
//Populate array with names
for (int i = 0; i < num; i++)
{
// Prompt for name
cout << "Enter Name of Student " << i + 1 << ":" << endl;
cin >> *(names + i);
}
//Return names
return names;
}
/*******************************************************************************
* Function GetTestScores
*
* This Function will prompt the user for the test scores for a previously
* defined number of tests, then store it in an array.
* _____________________________________________________________________________
* INPUT
* num - number of tests submitted
*
* OUTPUT
* scores - score for each student
* ****************************************************************************/
int *GetTestScores (int num)
{
int *scores; // OUTPUT - all student's scores
//Dynamically allocate memory
scores = new int[num];
for (int i = 0; i < num; i++)
{
// Prompt for test score
cout << "Enter Test Score for Student " << i + 1 << ": " << endl;
cin >> *(scores + i);
}
return scores;
}
/*******************************************************************************
* Function SortArray
*
* This function will sort test scores in ascending order.
* _____________________________________________________________________________
* INPUT
* names - names of students
* scores - students' scores
* num - number of tests submitted
* initial_ val - value of element that is being used as reference
* min_subscript - subscript of minimum value
* min_value - value of (current) minimum element
* bookmark - holder for name
*
* OUTPUT
* names - students' names
* scores - students' scores
* ****************************************************************************/
void SortArray(string *names, int *scores, int num)
{
int initial_val; // INPUT - reference value
int min_subscript; // INPUT - subscript of minimum value
int min_value; // INPUT - value of (current) minimum element
string bookmark; // INPUT - holder for name
for(initial_val = 0; initial_val < num; initial_val++ )
{
min_subscript = initial_val;
min_value = *(scores + initial_val);
bookmark = *(names + initial_val);
for(int i = initial_val + 1; i < num; i++)
{
if (*(scores + i) < min_value)
{
min_value = *(scores + i);
bookmark = *(names + i);
min_subscript = i;
}
}
*(scores + min_subscript) = *(scores + initial_val);
*(names + min_subscript) = *(names + initial_val);
*(scores + initial_val) = min_value;
*(names + initial_val) = bookmark;
}
}
/*******************************************************************************
* Function GetAverageScore
*
* This function will compute the average score of the scores provided.
* _____________________________________________________________________________
* INPUT
* scores - students' test scores
* num - number of tests
* score_sum - sum of students' scores
* OUTPUT
* average - average test score
* ****************************************************************************/
float GetAverageScore(int *scores, int num)
{
int score_sum = 0; // INPUT - sum of students' scores
float average; // OUTPUT - average test score
// Sum all values
for (int i = 0; i < num; i++)
{
score_sum += *(scores + i);
}
// Compute average
average = score_sum / num;
return average;
}
/*******************************************************************************
* Function OutputScores
*
* This function will display student names, test scores, and the average
* overall test score.
* _____________________________________________________________________________
* INPUT
* names - students' names
* scores - students' scores
* num - number of tests
* average - average test score
* ****************************************************************************/
void OutputScores(string *names, int * scores, int num, float average)
{
// Display Header
cout << right;
cout << "\n\nSTUDENT" << setw(15) << "SCORE" << endl;
cout << "----------------------" << endl;
for (int i = 0; i < num; i++)
{
cout << left << setw(17) << *(names + i);
cout << *(scores + i) << endl;
}
}