please help on urgent basis

Oct 10, 2011 at 5:06pm

Please check the following program why it's giving the 2nd largest value wrong.If I enter 5 values 140,125,150,170 n 160 respectively it shows the largest value :170
the smallest value:125
the 2nd Largest value: 140

2nd largest value shud be 160.


#include <iostream.h>


int main (){
int NumofVariables;
cout<<"Enter number of varible to check: ";
cin>>NumofVariables;
int counter=0;
int userinput=0;
int largest = 0;
int smallest=0;
int secondLargest=0;

do{
cout <<"Enter value for variable:\n";
cin >>userinput;
if(counter==0){
smallest=userinput;
largest=userinput;
secondLargest=userinput;

}
++counter;


if(largest<userinput){
largest=userinput;
}


if (smallest>userinput){
smallest= userinput;
}
if (userinput<largest && userinput>secondLargest ){
secondLargest= userinput;
}

//counter = counter+1;
}
while(counter<NumofVariables);

cout<< "Largest values is :";
cout<<largest;
cout<< "\n smallest values is :";
cout<<smallest;
cout<< "\n 2nd largest values is :";

cout<<secondLargest;

int pause;

cin>>pause;
}
Last edited on Oct 10, 2011 at 5:07pm
Oct 10, 2011 at 6:01pm
closed account (D80DSL3A)
That's odd. Your code worked fine for me. Output:

Enter number of varible to check: 5
Enter value for variable:
140
Enter value for variable:
125
Enter value for variable:
150
Enter value for variable:
170
Enter value for variable:
160
Largest values is :170
 smallest values is :125
 2nd largest values is :160

I think that your method will fail though if the largest value is entered after the 2nd largest value. ie. The results will depend on the order in which values are entered.
Oct 11, 2011 at 1:52am
But it result sould not depend on the order as every time loop runs n check the both conditions.

if (userinput<largest && userinput>secondLargest ){
secondLargest= userinput;

In my point of it's dealing all the conditions so y still result will depend on the order of the values entered..???
Oct 11, 2011 at 2:40am
Update this code segment:
1
2
3
if(largest<userinput){
largest=userinput;
}


to this:
1
2
3
4
if(largest<userinput){
secondLargest = largest;
largest=userinput;
}


Oct 11, 2011 at 1:11pm
Problem is still same. :(
Oct 11, 2011 at 2:24pm
@Thanz

I used kalgorithmist's idea, but deleted
1
2
3
if (userinput<largest && userinput>secondLargest ){
secondLargest= userinput;
}
check, and everything worked beautifully, since secondLargest is already being adjusted.
Oct 11, 2011 at 4:38pm
closed account (D80DSL3A)
I found that both code fragments are needed. kalgorithmist code handles the case where the largest value is entered after the 2nd largest, Your original code (given by whitenite1) handles the other case.
Are you sure that you're running the newer code? It should be working.
Oct 11, 2011 at 5:38pm
@fun2code
I checked the program with your suggestion, and found, yes indeed, it worked flawlessly. Here is the program, with corrections:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// High-lowest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main (){
	int NumofVariables;
	cout<<"Enter number of varible to check: ";
	cin>>NumofVariables;
	int counter=0;
	int userinput=0;
	int largest = 0;
	int smallest=0;
	int secondLargest=0;

	do{
		counter++;
		cout <<"Enter value for variable # " << counter << ": ";
		cin >>userinput;
		if(counter==1)
		{
			smallest=userinput;
			largest=userinput;
			secondLargest=userinput;
		}

		if(largest<userinput)
		{
			secondLargest = largest;
			largest=userinput;
		}

		if (smallest>userinput)
		{
			smallest= userinput;
		}

		if (userinput<largest && userinput>secondLargest )
		{
			secondLargest= userinput;
		}
	}
	while(counter<NumofVariables);

	cout << endl <<endl;
	cout << "The largest value is :\t" <<largest << endl;
	cout << "2nd largest value is :\t" << secondLargest << endl;
	cout << "   Smallest value is :\t" <<smallest << endl;
	cout <<endl;
	int pause;
	cin>>pause;
}
Oct 14, 2011 at 7:59am

whitenite1 This Program is working well now tyx.
Topic archived. No new replies allowed.