You are here:

C++/test scores

Advertisement


Question
Hi Rai:

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

Answer
Hi,
 Here is the skeleton for that. It would help you in sorting. For getting the average, you can add all the elements and divide the sum by the count. This you can do well before the sorting. Sorting can be done in the opposite way if you change the condition in the commented line in the code.:

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
  vector <int> Arr;
  char Opt = 1;
  while (Opt)
  {
     int x;
     cout << "enter the number" << endl;
     cin >> x;
     Arr.push_back(x);
     cout << "if you want to enter more number press 1 else 0" << endl;
     cin >> Opt;
     if (!Opt)
        break;
  }
  
  vector::iterator Iter=Arr.begin();
  vector::iterator temp;
  for (; Iter != Arr.end(); ++Iter)
     for (temp = Iter; temp != Arr.end; temp++ )
        // if *temp is > than *Iter, swap them
     
  return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.