c++ ignoring the duplicate

EXAMPLE :
If the numbers are 4, 43, 6.7, 34, 6, -9, 0 , 22
The second largest value is 34.
Example: If the numbers are 4, 43, 6.7, 34, 6, -9, 0 ,43, 22
The second largest value, ignoring the duplicate 43, is 34
If the numbers input are 1 1 1 1 1 1
the second largest value is 1
What if the user enters only one number?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int main()

{
	char r;
	double number,largest,second_largest;
	cout << "enter the numbers : " ;
	cin >> largest;
	cout <<" Do you want to continue (Y) or (N)" ;
	cin >> r;
	while(r=='y')
	{
	cin>> number;
	if (number >= largest)
   {
	 second_largest = largest;
    largest = number;}
else if (number >= second_largest)
    {
	second_largest = number;}

cout << "Do you want to continue (Y) or (N) " ;
cin >> r;
}

cout <<"secondlargest"<<second_largest;

return 0;

}



ı wrote something but ı can't find the numbers because they repeat
Last edited on
Change number >= largest to number > largest and number >= second_largest to number > second_largest

And to handle the case where all numbers are the same, keep some bool variable to keep track of whether second_largest was ever assigned a value. So you can write an if-statement such that if there wasn't a second largest number, you would display the largest as the second largest, otherwise you would display the second largest that was found by the program.
I'm new starting to write c++ so I dont know much . could you write code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;
int main()

{
    bool is_second_largest_exist = false;
    char r;
    double number, largest, second_largest;
    cout << "enter the numbers : ";
    cin >> largest;
    cout << " Do you want to continue (Y) or (N)";
    cin >> r;
    while (r == 'y')
    {
        cin >> number;
        if (number > largest)
        {
            second_largest = largest;
            largest = number;
            is_second_largest_exist = true;
        }
        else if (number > second_largest)
        {
            second_largest = number;
            is_second_largest_exist = true;
        }

        cout << "Do you want to continue (Y) or (N) ";
        cin >> r;
    }

    if (is_second_largest_exist) {
        cout << "secondlargest" << second_largest;
    }
    else {
        cout << "secondlargest" << largest;
    }

    return 0;
}
this code still give 43. should not count repeaters and answer we should 34
I think Grime's technique would work if there is an additional else if (number == largest) between his lines 21 and 22 which would simply discard the duplicate.
i tried but didn't work
In @Grime's code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        if (number > largest)
        {
            second_largest = largest;
            largest = number;
            is_second_largest_exist = true;
        }
        else if (number == largest)
        {
            // do nothing
        }
        else if (!is_second_largest_exist || number > second_largest)  // second_largest wasn't initialised
        {
            second_largest = number;
            is_second_largest_exist = true;
        }

is there anyone else who can help?
@sahbaz
I made the minor changes to the if blocks in @Grime's code as indicated in my previous post and here, verbatim, is the output.

The answer is ... 34.

I'm not sure what else you need.

enter the numbers : 4
 Do you want to continue (Y) or (N)y
43
Do you want to continue (Y) or (N) y
6.7
Do you want to continue (Y) or (N) y
34
Do you want to continue (Y) or (N) y
6
Do you want to continue (Y) or (N) y
-9
Do you want to continue (Y) or (N) y
0
Do you want to continue (Y) or (N) y
43
Do you want to continue (Y) or (N) y
22
Do you want to continue (Y) or (N) n
secondlargest34 
upss thank you , I asked if I have different ways
Sahbaz wrote:
I asked if I have different ways
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
#include <string>
#include <set>
using namespace std;

int main()
{
   set<double> S;
   string line;
   cout << "Enter numbers on one line: ";    getline( cin, line );
   stringstream ss( line );
   for ( double x; ss >> x; ) S.insert( x );
   cout << "Largest: " << *S.rbegin();
   if ( S.size() > 1 ) cout << "\nSecond largest: " << *( ++S.rbegin() );
}


Enter numbers on one line: 4 43 6.7 34 6 -9 0 43 22
Largest: 43
Second largest: 34 

Thanks for your help.
Last edited on
Topic archived. No new replies allowed.