hy i have a problem in a Result programe of C++. I want to find 1st,2nd and 3rd position from any number of students. I already have a code to find first three largest element of array but problem is that 2 or 3 students marks can be equal but in this situation my programe is not working please help me.

Recommended Answers

All 9 Replies

can't help you without the code you wrote.

this is my code

#include<iostream>
#include<conio.h>
//#include<math.h>
using namespace std;
int main()
{
 int a[10],i;
 for(i=0;i<10;i++)
 {
       cout<<"enter value = ";
       cin>>a[i];
 }
 int largest_1 = 0, largest_2 = 0, largest_3 = 0;

 for(int i=0;i<10;i++)
 {
  if(a[i] > largest_3 && a[i] < largest_2)
  {
   largest_3 = a[i];
  }
  else if(a[i] > largest_2 && a[i] < largest_1)
  {
   largest_3 = largest_2;
   largest_2 = a[i];
  }
  else if(a[i] > largest_1)
  {
   largest_3 = largest_2;
   largest_2 = largest_1;
   largest_1 = a[i];
  }
 }
 cout << "largest :" << largest_1 << endl;
 cout << "2nd largest :" << largest_2 << endl;
 cout << "3rd largest :" << largest_3 << endl;
 getch();
    return 0;
}

delete line 2, conio.h is not needed.

replace line 36 with cin.get().

what data did you enter? Your program worked correctly for me

#include<iostream>
using namespace std;
int main()
{
    int a[10] = { 5, 2, 10, 25, 4, 9, 7, 6, 8, 3 };
    int largest_1 = 0, largest_2 = 0, largest_3 = 0;

    for (int i = 0; i<10; i++)
    {
        if (a[i] > largest_3 && a[i] < largest_2)
        {
            largest_3 = a[i];
        }
        else if (a[i] > largest_2 && a[i] < largest_1)
        {
            largest_3 = largest_2;
            largest_2 = a[i];
        }
        else if (a[i] > largest_1)
        {
            largest_3 = largest_2;
            largest_2 = largest_1;
            largest_1 = a[i];
        }
    }
    cout << "largest :" << largest_1 << endl;
    cout << "2nd largest :" << largest_2 << endl;
    cout << "3rd largest :" << largest_3 << endl;
    cin.get();
    return 0;
}

my logic is when we want to find 1st 2nd and 3rd position in student array it will equal to find first 3 largest number in array 1st large number is 1st position 2nd large number is 2nd number similarly 3rd large number is 3rd position now problem is that when 1st large and 2nd large or 3rd large number is equal then it is not showing correct result. i hope you understand.

So if you have 3 numbers all the same you want the 1st, 2nd and 3d largest to be that same number?

I just tried it -- use <= and >= instead of < and > in each of the if statements.

when topers marks are equal then this code show only 1st large. actually my english is very poor therefore try to understand my problem.

Yes, I think I do understand the problem, maybe you don't understand the solution.

#include<iostream>
using namespace std;
int main()
{
    int a[10] = { 5, 2, 10, 25, 4, 9, 7, 25, 8, 3 };
    int largest_1 = 0, largest_2 = 0, largest_3 = 0;

    for (int i = 0; i<10; i++)
    {
        if (a[i] >= largest_3 && a[i] <= largest_2)
        {
            largest_3 = a[i];
        }
        else if (a[i] >= largest_2 && a[i] <= largest_1)
        {
            largest_3 = largest_2;
            largest_2 = a[i];
        }
        else if (a[i] >= largest_1)
        {
            largest_3 = largest_2;
            largest_2 = largest_1;
            largest_1 = a[i];
        }
    }
    cout << "largest :" << largest_1 << endl;
    cout << "2nd largest :" << largest_2 << endl;
    cout << "3rd largest :" << largest_3 << endl;
    cin.get();
    return 0;
}

And the output is this:

largest :25
2nd largest :25
3rd largest :10

thank you very very very much you are amazing and difference is only <= and >= now problem is if marks is if marks are 90,90,80,80,70,70 now what is result??????

should be 90, 90 and 80. What did you get when you ran the program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.