You are here:

- Home
- Computing/Technology
- C/C++
- C++
- TEST SCORES

Advertisement

hi Sandra:

how do can you write a program to read an unknown number of interger test scores from the keyboard(assume at most 150 scores). print the orgininal list of scores, the scores are sorted from low to high, the scores sorted from high to low, the highest score, the lowest score, and the average score.

This program is a collection of small, simple tasks. You can break the problem down into distinct parts:

1) Read at most 150 numbers from the keyboard

2) Make a copy of the array in ascending sorted order

3) Make a copy of the array in descending sorted order

4) Print out the three arrays

5) Print the highest score

6) Print the lowest score

7) Print the average

Because you have arrays in both ascending and descending order, you can use ascend[0] for the lowest score and descend[0] for the highest score. The code to do this assuming you have sort functions and an average function is trivial:

int main()

{

int scores[150];

int ascend[150];

int descend[150];

int size = 0;

int grade;

while ( size < 150 && cin>> grade )

scores[size++] = grade;

sort_ascending ( ascend, scores, size );

sort_descending ( descend, scores, size );

cout<<"Scores:\n";

for ( int i = 0; i < size; i++ )

cout<< scores[i] <<'\t'<< ascend[i] <<'\t'<< descend[i] <<'\n';

cout<<"High score: "<< descend[0] <<'\n';

cout<<"Low score: "<< ascend[0] <<'\n';

cout<<"Average score: "<< average_score ( scores, size ) <<endl;

}

Now you just need to write the sort and average functions. Calculating an average is simple since you have the items and the size already taken care of:

int average_score ( int *src, int size )

{

int sum = 0;

for ( int i = 0; i < size; i++ )

sum += src[i];

return sum / size;

}

Sorting is the most difficult part. You should have already been introduced to bubble sort, so I won't use a more sophisticated method. The only difference between ascending (low to high) and descending (high to low) sorting is in the comparison:

void sort_ascending ( int *dst, int *src, int size )

{

for ( int i = 0; i < size; i++ )

dst[i] = src[i];

for ( int i = size - 1; i > 0; i-- ) {

for ( int j = 0; j < i; j++ ) {

if ( dst[j] > dst[j + 1] ) {

int save = dst[j];

dst[j] = dst[j + 1];

dst[j + 1] = save;

}

}

}

}

void sort_descending ( int *dst, int *src, int size )

{

for ( int i = 0; i < size; i++ )

dst[i] = src[i];

for ( int i = size - 1; i > 0; i-- ) {

for ( int j = 0; j < i; j++ ) {

if ( dst[j] < dst[j + 1] ) {

int save = dst[j];

dst[j] = dst[j + 1];

dst[j + 1] = save;

}

}

}

}

These two functions are exactly alike except sort_ascending uses > to test the items and sort_descending uses <. You'll notice that each part is not difficult, it just seems that they are when viewed as a whole.

-Sandra

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | You are the angel. Thank you so much. I have never met any other expert who is so smart as you. I tried so hard for this problem to work. What I was trying to do was convert char array that had integer into integer variable but it never worked. Once again Thank you very much. |

I can help with any questions concerning ISO standard C++, object oriented programming and design as well as algorithms and data structures.

I have nine years of programming experience with C++.