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;
}
2
  • 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? Commented Nov 12, 2011 at 13:07
  • You could use std::vector, and then get the min/max like this stackoverflow.com/questions/182957/position-in-vector-using-stl Commented Nov 12, 2011 at 13:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't really get what you mean, but isn't there some function to compare the arrays without knowing the ID's?
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?
-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;
}

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.