1

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;
}
Seçkin Savaşçı
  • 3,446
  • 2
  • 23
  • 39
RnD
  • 1,019
  • 5
  • 23
  • 49
  • 2
    Think about if you have a bunch of numbers and you already know their maximum, how could you find the maximum of that bunch after adding one more number to the collection? – Kerrek SB Nov 12 '11 at 13:07
  • You could use std::vector, and then get the min/max like this http://stackoverflow.com/questions/182957/position-in-vector-using-stl – jbat100 Nov 12 '11 at 13:28

2 Answers2

5

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.

Seçkin Savaşçı
  • 3,446
  • 2
  • 23
  • 39
  • Didn't really get what you mean, but isn't there some function to compare the arrays without knowing the ID's? – RnD Nov 12 '11 at 13:16
  • think you have n numbers, and you know the min and max element of these n number. if i give you another number, and ask for the min and max element of those n+1 number. How do you compute the solution for my question? – Seçkin Savaşçı Nov 12 '11 at 13:26
-1
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;
}
joy
  • 1,569
  • 8
  • 11
  • 1
    in these sort of things you should always assign the mix and max to be the **0th** element and walk the array from **1** to **size-1** – parapura rajkumar Nov 12 '11 at 14:46
  • I have written that as a coment in my last edit, second example. I've took his code in a touch enviroment and modified it. It is pretty hard. :) but that does not excuse the error. – joy Nov 12 '11 at 14:51