Here is the code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//struct for scores and names
struct Scores
{
string name;
int score;
};
//function prototypes
void showArray (Scores *, int);
void avgScores (double *, int);
void sortScores (Scores *, int);
/***********Function Main***********/
int main ()
{
int numScores = 0;
double *ptr = NULL; //pointer for dynamic array size
//get number of test scores
cout << "How many test scores will you enter? ";
cin >> numScores;
cout << endl;
Scores *ptrScores; //creates a pointer to an array of structures
ptrScores = new Scores [numScores]; //creates an array of structers
for (int score = 0; score < numScores; score++)
{
cout << "Enter a student name: ";
cin >> ptrScores[score].name;
cout << "Enter " << ptrScores[score].name << "'s grade: ";
cin >> ptrScores[score].score;
if (ptrScores[score].score < 0)
{
cout << "Please only enter positive numbers.";
cout << "Please enter the grade again: ";
cin >> ptrScores[score].score;
}
}
sortScores(ptrScores, numScores);
showArray(ptrScores, numScores);
return 0;
}
/***************sortScores******************/
//This function sorts test scores in an array
void sortScores(Scores *array, int size)
{
int temp;
bool swap;
//sort array in ascending order
do
{ swap = false;
for (int count = 0; count < (size-1); count++)
{
if (array[count].score > array[count + 1].score)
{
temp = array[count].score;
array[count].score = array[count + 1].score;
array[count + 1].score = temp;
swap = true;
}
}
}while(swap);
}
/***************avgScores******************/
//This function averages test scores in an array
void avgScores(double *array, int size)
{
}
/***************showArray******************/
//This function prints the values in an array
void showArray (Scores *array, int size)
{
cout << "The test scores you entered are: \n";
//prints each array element
for (int index = 0; index < size; index++)
{
cout << array[index].name << " ";
cout << array[index].score << endl;
}
cout << endl;
}

New Topic/Question
Reply



MultiQuote



|