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

Is javascript based on c?

595


Explain how do you print only part of a string?

652


What is line in c preprocessor?

618


Can you define which header file to include at compile time?

591


Differentiate between a structure and a union.

770






What is cohesion and coupling in c?

594


if a is an integer variable, a=5/2; will return a value a) 2.5 b) 3 c) 2 d) 0

1455


Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10

15056


What is wrong with this declaration?

615


What is the incorrect operator form following list(== , <> , >= , <=) and what is the reason for the answer?

944


The number of measuring units from an arbitarary starting point in a record,area,or control block to some other point a) recording pointer b) offset c) branching d) none

716


Why C language is a procedural language?

625


How can I insert or delete a line (or record) in the middle of a file?

578


Write a program to print all permutations of a given string.

647


What is hungarian notation? Is it worthwhile?

703