Finding the second maximum value in an array..

Jun 14, 2010 at 1:03am
Hello,
The output of the required program :
*****************************************************************
Enter the size of the array(max=100): 10
Enter 10 elements: 7 7 1 2 3 4 5 6 4 3
Your array is:
7....7....1....2....3....4....5....6....4....3
The second maximum value is: 6
GoodBye..!!!!
Press any key to continue
*****************************************************************

My program:

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
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
	int a[100],n,i,secmax,j,hold;
	cout<<"Enter the size of the array(max=100): ";
	cin>>n;
	cout<<"Enter "<<n<<" elements: ";
	for (i=0 ; i<n ; i++)
		cin>>a[i];
	cout<<"Your array is:\n";
	for (i=0 ; i<n ; i++)
		cout<<setfill('.')<<setw(5)<<fixed<<a[i];
	cout<<endl;
	for (i=0 ; i<n-1 ; i++)
	{
		for (j=0 ; j<n-1 ; j++)
			if (a[i]>a[i+1])
			{
				hold=a[i];
				a[i]=a[i+1];
				a[i+1]=hold;
			}
	}
	secmax=a[1];
	if (a[0]==a[1]) // this line means the maximum valuy is repeated
	{
		for (i=1 ; a[i] != a[i+1] ; i++); // this loop will stop when find a different value 
		secmax=a[i];

	}
	cout<<"The second maximum value is: "<<secmax<<endl;
	cout<<"GoodBye..!!!!\n";
	return 0;
}


My output:
*****************************************************************
Enter the size of the array(max=100): 10
Enter 10 elements: 7 7 1 2 3 4 5 6 4 3
Your array is:
7....7....1....2....3....4....5....6....4....3
The second maximum value is: 1
GoodBye..!!!!
Press any key to continue
*****************************************************************

Whats wrong? :(
Jun 14, 2010 at 2:04am
Line 19 is wrong. (and therefore also 21, 22, and 23)
Jun 14, 2010 at 6:00am
cout<<"Second maximum value is "<<a[n-2];
Last edited on Jun 14, 2010 at 6:00am
Jun 14, 2010 at 11:02am
The basic idea of the posted code is to sort the array and then pick the second item as the second maximum value.

Line 29 and 30 are also wrong.

First, the sorting is incorrect, Second, it is incorrect to halt the loop at unequal values.. it will keep running till it finds equal values... which should not be the case.

Ask if you still dont get it and need the corrected code.

@ qtpan
cout<<"Second maximum value is "<<a[n-2];

There are human beings out there (who may enter different values at different times)...
Last edited on Jun 14, 2010 at 11:03am
Jun 14, 2010 at 12:41pm
The program you want must be like this ...
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
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
	int a[100],n,i,secmax,j,hold;
	cout<<"Enter the size of the array(max=100): ";
	cin>>n;
	cout<<"Enter "<<n<<" elements: ";
	for (i=0 ; i<n ; i++)
		cin>>a[i];
	cout<<"Your array is:\n";
	for (i=0 ; i<n ; i++)
		cout<<setfill('.')<<setw(5)<<fixed<<a[i];
	cout<<endl;
	for (i=0 ; i<n-1 ; i++)
	{
		for (j=i+1 ; j<n ; j++){
			if (a[i]>a[j])
			{
				hold=a[i];
				a[i]=a[j];
				a[j]=hold;
			}
            }
	}
	secmax = a[n-2];
	for(i=n-2;i>=0;--i){
                        if(a[i] > a[n-2]) secmax = a[i];
                        }
	
	cout<<"The second maximum value is: "<< secmax << endl;
	cout<<"GoodBye..!!!!\n";
	
	cin.ignore(2);
	return 0;
}

If you have any question ,you can ask me...
Jun 14, 2010 at 1:06pm
@systempause
This would not work as at line 29, a[n-2] is same as a[i] and secmax does not change.
In the further executions of the loop, there is no value found which is greater than a[n-2].

Did you run the code?

Anyways, the real help of the asker is to enable him to find out the bugs himself, not provide him the homework solution by posting the code...
Last edited on Jun 14, 2010 at 1:07pm
Jun 14, 2010 at 1:17pm
Yes i run the code and there ware no errors and you are right about what you say "In the further executions of the loop, there is no value found which is greater than a[n-2]." I was going to write
if(a[i] != a[n-2]) secmax = a[i]; because there may be same numbers ...
Jun 14, 2010 at 1:28pm
There's a possibly simpler way to do it, requiring a single loop:

1
2
3
4
5
6
7
8
9
10
11
12
int max = secmax = a[0];

for(int i = 0; i < n; ++i)
{
	if((secmax == max || secmax < a[i]) && max > a[i]) {
		secmax = a[i];
	}
	if(max < a[i]) {
		secmax = max;
		max = a[i];
	}
}
Last edited on Jun 14, 2010 at 1:29pm
Jun 14, 2010 at 1:44pm
I would sort the array and take
the second value off the top.
but i am lazy
Jun 14, 2010 at 2:17pm
bigearsbilly wrote:
I would sort the array and take the second value off the top.

That won't work if two elements hold the maximum value.
Jun 14, 2010 at 2:31pm
well, there is that I suppose.

stupid duplicates
Jun 14, 2010 at 9:31pm
Thanks all.
Is there anyway to do it without using the idea of sorting..?!!
Jun 14, 2010 at 9:36pm
Sort-of. (Pardon).

You first need to know the maximum value. Easy, just do a few comparisons. Then, find a value that's larger than all the other maximum values but smaller than the maximum. Does that help?

-Albatross
Jun 14, 2010 at 9:38pm
Maybe just iterate through the array keeping a note of the largest number so far and its previous value, every time you change it.
Jun 15, 2010 at 1:38am
+1 Albatross, if this is homework.

Otherwise convert the array to a std::set, assert on size() >= 2, and access the 2nd element.
Jun 15, 2010 at 5:18am
@General Yes,you can find the second maximum without sorting it. Like albatros said find the highest first.


For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
highest=a[0];
for(i=0;i<n;i++)
    {
          if(highest<a[i])
              {
                    highest=a[i]; 
              }
    }
secmax=0;

for(i=0;i<n;i++)
    {
          if((secmax<a[i])&&(secmax<highest))
              {
                    secmax=a[i]; 
              }
    }


I have no compiler right now but i guess it goes ike this without using the bubble sort.
Jun 15, 2010 at 5:25pm
You could do it in one loop as well if you think about it. You just have to iterate over every value and have two variables max and max2. using std::numeric_limits you can initialize both to minimum values for an int or whatever the type is. Within the single loop you simply have to have a couple of different tests to figure out whether each number should go into max or max2 or neither. For now I guess we'll have to file this under "solution waiting for a problem". I'm not really sure what the application of it would be other than to simply solve a homework assignment.
Jun 16, 2010 at 4:42am
Thanks for all!
and this is not for my homework
I finished the semester.
Topic archived. No new replies allowed.