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
Is javascript based on c?
Explain how do you print only part of a string?
What is line in c preprocessor?
Can you define which header file to include at compile time?
Differentiate between a structure and a union.
What is cohesion and coupling in c?
if a is an integer variable, a=5/2; will return a value a) 2.5 b) 3 c) 2 d) 0
Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10
What is wrong with this declaration?
What is the incorrect operator form following list(== , <> , >= , <=) and what is the reason for the answer?
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
Why C language is a procedural language?
How can I insert or delete a line (or record) in the middle of a file?
Write a program to print all permutations of a given string.
What is hungarian notation? Is it worthwhile?