Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q3B. Write a function, findSecondLargest , that takes the number of elements in

ID: 3819579 • Letter: Q

Question

Q3B. Write a function, findSecondLargest, that takes the number of elements in an integer array and the array. It returns false if the number of element is less than two. Otherwise, it returns true, and “returns” the value of the second largest element in the array. If you choose to sort the array, you will not get full marks, as sorting is not required and the array must not be changed by the function. You may assume that the array does not contain any repeated values e.g.

    For the array [1, 2, 4, 3, 5, 6, -1, 0, -2, -8, 7} à the second largest is 6 ( the largest is 7)

Explanation / Answer

C PROGRAM :

#include <stdio.h>

int func(int len, int array[]){
   if(len<2){
       return -1;
   }
   int max = array[0];
   for(int i=1;i<len;i++){
       if(array[i]>max){
           max = array[i];
       }
   }
   int fans = -99999999;
   for(int i=0;i<len;i++){
       if(array[i]>fans && array[i]!=max){
           fans = array[i];
       }
   }
   return fans;
}
int main(void) {
   // your code goes here
   int len = 11;
   int arr[] = {1, 2, 4, 3, 5, 6, -1, 0, -2, -8, 7};
   int ans = func(len,arr);
   printf("Ans is %d ",ans);
   return 0;
}

OUTPUT :