Question

I was given a task, it's nothing special but I did hit the wall here...

After getting arithmetical means I need to compare them and output the highest and the lowest ones.

The x is a student number, the vid[] is the arithmetical mean.

For example:

Student number x has arithmetical mean vid[i]

and the task wants me to output which student has the highest and which one has the lowest means.

The worst part that I can't use stuff like max() and min() because I don't know how many students are there in total. Plus they are all arrays which have the same variable name vid[].

Any help would be appreciated =)

int main()
{
    int mokSK=0, p1[25], p2[25], p3[25], x[25], vid[25], iv=0;
    ifstream inFile("inFile.in");
    ofstream outFile("outFile.out");


    inFile >> mokSK;

    for(int i=0;i<mokSK;i++)
    {
        inFile >> x[i] >> p1[i] >> p2[i] >> p3[i];
        vid[i]=(p1[i]+p2[i]+p3[i])/3;
        outFile<< x[i] <<" " << vid[i] << endl;
    }

    return 0;
}
Was it helpful?

Solution

If you want O(1) to access max and min graded students; from the beginning of reading, update your max and min graded student in each read pass.

to be more clear: keep track of min and max graded student from the very beginning of the execution and update max and min graded students in each student data reading pass if needed.

OTHER TIPS

int main()
{
  int mokSK=0, p1[25], p2[25], p3[25],x[25],vid[25],iv=0;
  int minmean = INT_MAX; int minstud= 0;// initialize minmean and max mean with first mean
  int maxmean = 0; int maxstud= 0;
  ifstream inFile("inFile.in");
  ofstream outFile("outFile.out");
  inFile >> mokSK;
  for(int i=0;i<mokSK;i++)
  {
    inFile >> x[i] >> p1[i] >> p2[i] >> p3[i];
    vid[i]=(p1[i]+p2[i]+p3[i])/3;
    if(vid[i]>maxmean){maxmean = vid[i]; maxstud = i;}
    if(vid[i]<minmean){minmean = vid[i]; minstud = i;}
    // not handled if multple students have maxmean or minmean
    outFile<< x[i] <<" " << vid[i] << endl; 
  }

 outFile << "Max mean: " << maxmean << ", student id: " << maxstud << endl;
 outFile << "Min mean: " << minmean << ", student id: " << minstud << endl;
 return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top