In: Computer Science
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
#include
int main()
{
int arr[15], size, i;
int max1, max2;
printf("Enter 15 integers in the array: ");
for(i=0; i<15; i++)
{
scanf("%d", &arr[i]);
}
max1 = max2 =arr[0];
for(i=0; i<15; i++)
{
if(arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if(arr[i] > max2 && arr[i] < max1)
{
max2 = arr[i];
}
}
printf("Second largest = %d", max2);
return 0;
}
OUTPUT:-
// If any doubt please comment