#include <iostream>
using namespace std;

int main()
{
  cout << "Enter the number of students: ";
  int numberOfStudents;
  cin >> numberOfStudents;

  double score = -1;

  for (int i = 0; i < numberOfStudents; i++)
  {
    cout << "Enter a student score: ";
    int score1;
    cin >> score1;

    if (score < score1)
    {
      score = score1;
    }
  }

  cout << "Highest score is " << score;

    cin.clear();
    cin.ignore();
    getchar();
  return 0;
}

Now, i manage to find the highest score. But how if i want to find the highest score, second highest score and third highest score?

Thanks for advance ^^...

if u want need second highest score, 3rd highest n so on, the best thing is just sort the scores.

If u need only 1st, 2nd and 3rd u can keep two more variables like 'score'. Do it in similar way as u did for the highest score.

Hmm..i tried but can't.
Perhaps can u give some hint? LOL

Thanks

i am doing for the 2nd highest. Do for the 3rd highest on your own. U can do it in a similar way as follows.

int score;
double score1st = -1;
double score2nd = -1;
for (int i = 0; i < numberOfStudents; i++)
  {
    cout << "Enter a student score: ";
    cin >> score;
     if (score1st < score)
    {
      score1st = score;
    }
     else if(score2nd < score)
          score2nd = score;
 }

Edited 7 Years Ago by dkalita: n/a

Here is another problem..for the purpose to inverse an array.

void reverse(const int list[], int newlist[], int size)
{
for(int i = 0, j = size - 1; i<size; i++, j--)
{
newlist[j] = list
}
}

i am not understand this function especially the for loop..anyone can kindly explain?
Thanks for advance ^^.

One thing you need to bear in mind if you are dealing with three high scores...

If somebody gets a new high score, you need to shift the exisiting high scores down a place. Dkalita's post doesn't take this into consideration, so if you ran Dkalitas example in your debugger and you had the following scores in 1st and 2nd place:
score1st = 85
score2nd = 68

If you entered another score of 98, Dkalitas example would give you this:
score1st = 98
score2nd = 68

But what you'd expect to see is this:
score1st = 98
score2nd = 85

So the first thing you need to do is shift the current high score down a place and then put the users score in the top spot!

Here's a simple example to show you what I mean...

#include <iostream>
using namespace std;

int main()
{
	int score1st=0, score2nd=0, score3rd=0, studentScore, count=0;

	for(;;++count) // loop forever!
	{
		cout << "Enter a student score (or enter a negative score to quit):";
		cin >> studentScore;

		if(studentScore<0) // break out of loop if studentScore is negative
			break;
		else if(studentScore>score1st) // new high score
		{
			// shift the 2nd highest score into 3rd place
			score3rd = score2nd;

			// shift the current top score into 2nd place
			score2nd = score1st;

			// finally put the students score into 1st place
			score1st = studentScore;
		}
		else if(studentScore > score2nd) // new 2nd highest score
		{
			// shift the current 2nd place score into 3rd place
			score3rd = score2nd;

			// put the students score into 2nd place
			score2nd = studentScore;
		}
		else if(studentScore > score3rd) // new 3rd highest score
		{
			score3rd = studentScore;
		}

	}

	// output the top 3 recorded scores
	cout << "Out of " << count << " recorded scores, the top 3 are: " << endl;
	cout << "1st : " << score1st << endl;
	cout << "2nd : " << score2nd << endl;	
	cout << "3rd : " << score3rd << endl;

}

Hope that is of some help to you!

Cheers for now,
Jas.

i am doing for the 2nd highest. Do for the 3rd highest on your own. U can do it in a similar way as follows.

int score;
double score1st = -1;
double score2nd = -1;
for (int i = 0; i < numberOfStudents; i++)
  {
    cout << "Enter a student score: ";
    cin >> score;
     if (score1st < score)
    {
      score1st = score;
    }
     else if(score2nd < score)
          score2nd = score;
 }

hi
making some correction to the earlier code

int score;
double score1st = -1;
double score2nd;
for (int i = 0; i < numberOfStudents; i++)
  {
    cout << "Enter a student score: ";
    cin >> score;
     if (score1st < score)
    {
      score2nd = score1st;/*newly added*/
      score1st = score;
    }
     else if(score2nd < score)
          score2nd = score;
 }

previos code wold not give u correct answer all the time. Use this code for that.


******* thanks to JasonHippy ...........

Here is another problem..for the purpose to inverse an array.

void reverse(const int list[], int newlist[], int size)
{
for(int i = 0, j = size - 1; i<size; i++, j--)
{
newlist[j] = list
}
}

i am not understand this function especially the for loop..anyone can kindly explain?
Thanks for advance ^^.

thats so simple.
U are assigning the elements in the newlist from its end to start from the other array 'list' from first to last.

Here is another problem..for the purpose to inverse an array.

void reverse(const int list[], int newlist[], int size)
{
for(int i = 0, j = size - 1; i<size; i++, j--)
{
newlist[j] = list
}
}

i am not understand this function especially the for loop..anyone can kindly explain?
Thanks for advance ^^.

OK, well I'll step through it with you...
So you have two arrays, both of which must be of the same size and you're passing both arrays into the function along with the size.
And you want to know what's going on in the loop...

For this example we'll imagine we're passing two arrays into the function, both have three elements..so the size will be 3.
'list' contains the values 10,20,30, 'newlist' contains the values 0,0,0

Looking at the body of the loop:

{
    newlist[j] = list[i]
}

We can see that i and j are used to index the two arrays..i indexes the array 'list' and j indexes the 'newlist' array.

Looking at the initialisation section of the for loop:

for(i=0, j=size-1

so i is 0
j is set to size-1 (3-1=2)

In other words, i will initially index the front of the 'list' array (list[0]), whereas j initially indexes the back of the 'newlist' array (newlist[2]).

Looking at the break condition of the loop:

i<size;

We can see that the loop continues while the value of 'i' is less than size...and size is 3 in this hypothetical example, so the loop will continue while 'i' is less than 3.

OK, so back to the body of the for loop:

{
    newlist[j] = list[i]
}

The first time through the loop, the value in the first element of list (list[0]) is copied into the last element of the array newlist (newlist[2]).

The value at list[0] is 10, so newlist now contains this:
0,0,10

Next, looking at the incrementor part of the for loop:

i++, j--

i is incremented and j is decremented.
So at the end of the first iteration, i=1, j=1.

Because 'i' is less than 3, the loop reiterates and the body of the loop is executed again:
The value in list[1] is copied into newlist[1]. The value at list[1] is 20
So newlist contains this:
0,20,10

Then the incrementor is called:
i=2, j=0

Again, 'i' is still less than 3, so the loop reiterates and the body of the loop is executed:
The value in list[2] is copied into newlist[0] (in other words, the value of the last position in the array 'list' has been copied into the first position of 'newlist')
The value at list[2] is 30.
newlist now contains:
30,20,10

The incrementor is called:
i=3, j=-1

Because i is now equal to 3, the loop breaks out.

So after breaking out of the loop, the contents of the two arrays are:
list -> 10,20,30
newlist -> 30,20,10

So effectively what this function has done is copied the items from 'list' into 'newlist', but in reverse order.

Hope that clears things up for you!
Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: n/a

This article has been dead for over six months. Start a new discussion instead.