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? :(