Chapter 9 Test Score#1

/*2. Test Scores #1
Write  a  program  that  dynamically  allocates  an  array  large  enough  to  hold  a  user-defined  number  of  test  scores.  Once  all  the  scores are  entered,  the  array  should  be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.*/

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

using namespace std;
void sortScore(int *,int );
double getAverage(int *,int);

int main()
{
int numberOfScore;
cout<<“Enter the number of test scores: “;
cin>>numberOfScore;

int* score;
score= new int [numberOfScore];

for(int i=0;i<numberOfScore;i++)
{
cout<<“\nEnter the score for “<<i+1<<” test: “;
cin>>score[i];
}

sortScore(score, numberOfScore);

cout<<“\nList of Scores in ascending order”;
for(int i=0;i<numberOfScore;i++)
{
cout<<“\n”<<score[i];
}
double average=getAverage(score,numberOfScore);

cout<<“\nThe average of scores is “<<average<<endl;

delete [] score;
score=0;

system(“pause”);
return 0;
}

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

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;
}
}
score[minIndex]=score[startScan];
score[startScan]=minValue;
}

}

double getAverage(int * score,int size)
{
double sum=0.0,avg;
for(int i=0;i<size;i++)
{
sum+=score[i];
}
return avg=sum/size;
}

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