-2

In c++, I have to write a code to get the user's desired numbers in each line with "while" command and when the user enter number -1 at the end, I have to display the largest number, the largest number other than the previous number, and the number of row with the largest number entered. For example user's numbers is:

4
7
11
5
-1

and result is:

11(the biggest number)
7(the biggest number after 11)
3(the row that the user has entered the largest number)

This is my own code:

#include <iostream>

using namespace std;
int main()

{
int price;
int max=0;
cin>>price;
while(price!=-1)
{
    while(price>max)
    {   
    
    max=price;
    }

cin>>price;


}

cout<<max;




    return 0;   

}

I can find the largest number, but not the other two variables.

Evg
  • 25,259
  • 5
  • 41
  • 83
Ali
  • 1
  • 1
  • 5
    Please take the [tour] and read [ask]. Which of the other tasks is giving you problems? What have you tried that failed? How would you solve the task on paper? Please also format your code consistently. It make it more likely that people read it here and easier to understand and debug as well. – Ulrich Eckhardt Nov 08 '21 at 07:11
  • see [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569/995714) for why that last sentence won't help you – phuclv Nov 08 '21 at 07:20

1 Answers1

2

First, You should push every element to vector and then find max1 and max2. Moreover, You can find max element index like the following:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> vec;
    int a;
     
    do{
       
        cin>>a;
        vec.push_back(a);
    }while(a!=-1);
    
    int max1=vec[0],max2=vec[0];
    int max_index=0;
    
    for(int i=1;i<vec.size();i++){
        //max1=10&max2=8
        
        //vec[i]=8
        if(vec[i]>max2&&vec[i]<max1){
            max2=vec[i];
        }
        //vec[i]=11
        if(vec[i]>max1){
            max2=max1;
            max1=vec[i];
            max_index=i;
        }
    }
    
    cout<<"1st max:"<<max1<<"\n index:"<<max_index+1<<endl;
    cout<<"2nd max: "<<max2<<endl;
}

If you did not understand anything in this answer, feel free to write comments

  • You should see [why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – justANewb stands with Ukraine Nov 08 '21 at 07:42
  • Thank you for your reply. I have looked through the article. It was useful to me – Zokirjon Gofurboyev Nov 08 '21 at 07:51
  • Add extra variables to keep track of "current best" answer for: 1. the largest number, 2. the largest number other than the previous number, 3. and the number of row with the largest number entered Inside "while" loop if you detect that "1. largest" should change, also update rest of variables. Sometimes it requires also to keep track of "current" and/or "previous" value, etc. – wiktor.wandachowicz Nov 08 '21 at 12:29
  • 1
    I have done exactly like what you said. There are three variables:max1,max2 and max_index – Zokirjon Gofurboyev Nov 08 '21 at 17:04