-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecondLargestUnsorted.c
More file actions
48 lines (40 loc) · 1.06 KB
/
secondLargestUnsorted.c
File metadata and controls
48 lines (40 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
//Function for array size input
int arraySize(int *n){
printf("Input the array size: ");
scanf("%d", n);
}
//Function for array input
void arrayInput(int a[], int s){
printf("\nInput the array: ");
for(int i = 0; i < s; i++){
scanf("%d", a+i);
}
}
//Finding the second largest element of an unsorted array
int secondLargestUnsorted(int a[], int s){
int m1 = 0, m2 = 0;
//Finding the largest element of the unsorted array
for(int j = 0; j < s; j++){
if(*(a+m1) < *(a+j)){
m1 = j;
}
}
//Finding the second largest element of the unsorted array
for(int j = 0; j < s; j++){
if(*(a+m2) < *(a+j) && j != m1){
m2 = j;
}
}
return *(a+m2);
}
int main(){
//Taking input
int s = 0;
arraySize(&s);
int a[s];
arrayInput(a, s);
//Finding the second largest element in the inputed unsorted array and print it
printf("\nThe second largest element: %d.\n", secondLargestUnsorted(a, s));
return 0;
}