Chapter 9 Test Score#2

/*4. Test Scores #2
Modify the program of Programming Challenge 2 to allow the user to enter name-score pairs. For each student taking a test, the user types the students name followed by the students integer test score. Modify the sorting function so it takes an array holding the student  names  and  an  array  holding  the  student  test  scores.  When  the  sorted  list  of scores is displayed, each students name should be displayed along with his or her score. In stepping through the arrays, use pointers rather than array subscripts.*/

#include “stdafx.h”
#include<iostream>
#include<string>

using namespace std;
void sortScore(int *,int,string* );

int main()
{
int numberOfStudents;
cout<<“Enter the number of students: “;
cin>>numberOfStudents;

int* score=new int[numberOfStudents];
string* nameOfStudent=new string[numberOfStudents];

for(int i=0;i<numberOfStudents;i++)
{
cout<<“\nEnter the name of “<<i+1<<” student: “;
cin>>nameOfStudent[i];
cout<<“\nEnter the score of the student: “;
cin>>score[i];
}

sortScore(score, numberOfStudents,nameOfStudent);

cout<<“\nList of Scores in ascending order”;
for(int i=0;i<numberOfStudents;i++)
{
cout<<“\n”<<nameOfStudent[i]<<“\t\t”<<score[i];
}

cout<<endl;

delete [] score;
score=0;

system(“pause”);
return 0;
}

void sortScore(int *score,int size,string* name)
{
int startScan,minIndex;
int minValue;
string minName;

for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=score[startScan];

for(int index=startScan+1;index<size;index++)
{
if(score[index]<minValue)
{
minValue=score[index];
minIndex=index;
minName=name[index];
}
}
score[minIndex]=score[startScan];
score[startScan]=minValue;
name[minIndex]=name[startScan];
name[startScan]=minName;
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s