find second largest element in array w/o using sorting
techniques? use onle one for loop.

Answer Posted / alam cse-35 bangladesh univers

#include<iostream>
using namespace std;
int main()
{
int i,a[]={121,104,105,205,6,25,80,77,120},max=0,second_max=0;
for(i=0;i<=8;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
for(i=0;i<=8;i++)
{
if(a[i]!=max)
{
if(a[i]>second_max)
{
second_max=a[i];
}
}
}

cout<<"Second Highest Value:"<<second_max<<endl;
cout<<"Maximum Value:"<<max;
return 0;
}

Is This Answer Correct ?    4 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain what is operator promotion?

629


Explain what is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?

637


differentiate built-in functions and user – defined functions.

617


Write a program to print "hello world" without using a semicolon?

589


Why is c used in embedded systems?

606






What is the purpose of realloc()?

663


What is #include called?

565


How many bytes is a struct in c?

719


Why cant I open a file by its explicit path?

593


What is the difference between variable declaration and variable definition in c?

558


What is null character in c?

684


How can I implement a delay, or time a users response, with sub-second resolution?

615


What is a stream in c programming?

588


how to capitalise first letter of each word in a given string?

1424


Explain why c is faster than c++?

566