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

Answer Posted / anand

Initializing second_largest to max negative number will ensure for all (+ve and -ve range of values).

int secondLargestNumber(int arr[],int numberOfValues)
{
int largest=arr[0];
int second_largest= -(2^(sizeof(int)*8 -1));
int i;

for(i=1;i<numberOfValues;i++)
{
if(a[i]>largest)
{
secondLargest=largest;
largest=a[i];
}

if(a[i]>secondLargest && a[i]<largest)
secondLargest=a[i];
}

return secondLargest;
}

Is This Answer Correct ?    4 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does 2n 4c mean?

707


What is wrong with this program statement?

603


What is volatile, register definition in C

680


Explain the difference between null pointer and void pointer.

661


What is pre-emptive data structure and explain it with example?

3203






What does d mean?

575


What is the difference between text and binary modes?

637


What is cohesion and coupling in c?

587


What is the value of uninitialized variable in c?

566


What is the right way to use errno?

617


Why & is used in c?

709


WHICH TYPE OF JOBS WE GET BY WRITING GROUPS .WHEN THE EXAMS CONDUCTED IS THIS EXAMS ARE CONDUCTED EVERY YEAR OR NOT.PLS TELL ME THE ANSWER

1455


What is the use of sizeof () in c?

553


What does a pointer variable always consist of?

656


What is pivot in c?

562