// Andres Guzman CSC5 Chapter 9, P. 538, #4
/******************************************************************************
*
* CALCULATE TEST SCORES #2
* ____________________________________________________________________________
* Program will take inputed student names and test scores, dynamically
* allocate memory, and sort the scores in ascending order while keeping each
* student's name matched with their corresponding test score.
*
* Computation:
* Uses selection sort to arrange test scores from lowest to highest while
* swapping the corresponding student names. Uses the formula "total / b"
* to calculate the average amongst the inputed test scores.
* ____________________________________________________________________________
*
* INPUT
*
* SIZE = Inputed Array Size
* names = Student Names
* test = Student Test Scores
*
* OUTPUT
*
* names = Student Names Sorted by Test Score
* test = Corresponding Sorted Test Scores
* averagescore = Average Test Score
*
******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
//
//Function Prototypes
void ascendingorder(string *a, int *b, int c); //Sorts numbers from low to high
int average(int *a, int b); //Calculates Average
int main()
{
int SIZE; //Array Size
cout << "Enter Amount of Test Scores: ";
cin >> SIZE;
while (SIZE <= 0) //Input Validation
{
cout << "Enter Positive Value" << endl;
cout << "Enter Amount of Test Scores: ";
cin >> SIZE;
}
string *names = new string[SIZE]; //Dynamically Allocates Memory
int *test = new int[SIZE]; //Dynamically Allocates Memory
//
//Allows input value for each subscipt
cin.ignore();
for(int count = 0; count < SIZE; count++)
{
cout << "\nEnter Student Name: ";
getline(cin, *(names + count));
cout << "\nEnter Test Score: ";
cin >> *(test + count);
while(*(test + count) < 0) //Input Validation
{
cout << "Please Enter Positive Value." << endl;
cout << "Test " << (count + 1) << ": ";
cin >> *(test + count);
}
cin.ignore();
}
cout << "\n\n"; //Spacing
//
//Function Call for sorting
ascendingorder(names, test, SIZE);
//
//For loop to step through array subscript and output test scores accordingly
for(int count = 0; count < SIZE; count++)
{
cout << *(names + count) << ": " << *(test + count) << endl;
}
cout << "\n"; //Spacing
//
//Function call for average score calculation
int averagescore = average(test, SIZE);
//
//Output results
cout << "Average Score: " << averagescore << endl;
delete [] names; //Deletes new memory
delete [] test; //Deletes new memory
return 0;
}
/*******************************************************************************
* Adding definition to function. The function will use selection sort to
* arrange the inputed test scores in ascending order while keeping the
* corresponding student names matched with their test scores.
******************************************************************************/
void ascendingorder(string *a, int *b, int c)
{
int startscan, minIndex; //Used to scan through the array
int minValue; //Holds lowest value found
string tempname;
//
//Initializing For Loop
for (startscan = 0; startscan < (c - 1); startscan++)
{
minIndex = startscan; //Assigns starting position as lowest
minValue = *(b + startscan); //Assigns starting number as lowest
//
//Searches for lowest value
for (int index = startscan + 1; index < c; index++)
{
if(*(b + index) < minValue)
{
minValue = *(b + index); //Updates lowest number value
minIndex = index; //stores position of lowest value
}
}
//
//Swaps Values
*(b + minIndex) = *(b + startscan);
*(b + startscan) = minValue;
tempname = *(a + minIndex);
*(a + minIndex) = *(a + startscan);
*(a + startscan) = tempname;
}
}
/*******************************************************************************
* Adding definition to function. The function will accumulate the total scores
* and calculate the average of the input values and return the value.
******************************************************************************/
int average(int *a, int b)
{
int total = 0; //Accumulator
int average; //Holder for formula
//
//For loop to step through subscript
for(int count = 0; count < b; count++)
{
total += *(a + count); //Adds Value to accumulator
}
average = total / b; //Calculates Average
return average; //Returns Value
}