Hi, I am trying to find 2nd largest number that given input of numbers. The question is

actually consider that you enter 5 number : 60, 70, 30, 40, 55 randomly -the order does not matter- my function should find 2nd largest number and return it. I am trying but I did not manage to finish my code and actually I can not a way to find true algorithm for this question. The rule is -I know already how I can find max or min number in array, it is simple- you do not use any type of array / pointer / dynamic memory allocation

Here is my code in fact I couldn't write anything related to heart of this question :(

#include <iostream>
using namespace std;

int secondLargestNumber()
{
    int nofStudent, grade;

    int max;
    int secondMax;
    cout <<"Please enter the number of students that have taken this exam: "<<endl;
    do
    {
        cin>>nofStudent;

        if (nofStudent < 0 || nofStudent > 50)
        {
            cout<< "Please re-enter the class size!"<<endl;

        }


    }while (nofStudent < 0 || nofStudent > 50);

    cout << "Now, we passed just do-while"<<endl;
    cout<<"********************************"<<endl;
    cout<< "Enter the grades of these students: "<<endl;


    for (int i = 0; i < nofStudent; ++i)
    {
        cin>>grade;


        while (grade > 100 || grade < 0)
        {
            cout<<"Please enter valid grades for each of these students: "<<endl;
            cin>>grade;
        }
        

    }



        /*
        cout<<endl;
        cout<<"Keep is : "<<keep<<endl;
        cout<<endl;
        cout<<"Grade is : "<<grade<<endl;
        cout<<endl; */



    cout<<"******************************"<<endl;
    cout <<"Max : "<<&max<< " and 2. Max : " <<&secondMax<<endl<<grade<<endl;

    return 6;
}




int main ()
{

    cout<<endl;
    cout<<"TYPE"<<endl;
    cout<<endl;
    secondLargestNumber();

    return 0;
}

It is not my homework, it was our quiz question yesterday and I did not solve but I am curious on how to set up true thinking or algorithm, thanks...

Recommended Answers

All 8 Replies

I focused on an idea that create new int named keep that keep the grade integer but whenever I enter the grade old grade value is normally losing and I can not keep old grade values and so on I can not compare these grade value, I need a list but I am not allowed.

If you want to find the second max you need to have 2 max variables. the first will hold the actual max and the second will hold the number that is greatest after that.

int max = 0, max2 = 0, grade;
cout << "Please enter your numbers (-1 to quit): ";
while (cin >> grade)  // loop for input
{
    if (grade == -1)  // ends input
        break
    if (grade > max)  // check for maximum value
        max = grade;
    if (grade > max2 && max != grade) // only do this if grade isn't max
        max2 = grade;
}
commented: not hold second no .it shows zero +0

Thanks for posting now I am trying I did not think outer while loop

while (cin>>grade)

Here is question is solved, just you need to write one line segment

if (grade > max) // check for maximum value 
{
  secondMax = max; /* in random inputting if it is not written sometimes it says last
element is 2. max number why I do not exactly know just try with compiler */

  max = grade;
}

again thanks...

This program can not find seconds largest number.plz give me right program.

#include<stdio.h>
int main()
 {
 int temp,m,max=0,max2=0;  
   scanf("%d",&m);
    max=m;

    scanf("%d",&m);
    max2=m;

   if(max2>max){
      temp=max;
      max=max2;
      max2=temp;
   }

   while(scanf("%d",&m))
   {
       if(m==-1)
        break;

       if(m>max){
            max2=max;
            max=m;
       }
       else if(m>max2)
         max2=m;
       }
printf("%d",max2);
}
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.