We will use this function to proceed and store the result
void find_two_largest(int a[], int n, int *largest, int *second_largest);
* When passed an array a of length n, the function will search a for its largest and second-largest elements, storing them in the variables pointed to by largest and second_largest respectively.
寫 find_two_largest() 函數。陣列做為函數參數 找出最大值跟第二大值。
Finish the code below to match the input and output. Pointer is required. 請使用指標。
#include <stdio.h>
void find_two_largest(int a[], int n, int *largest, int *second_largest);
int main(void)
{
int n, largest, second_largest;
scanf("%d", &n); //Numbers will be entered
int a[n];
//Enter n integers separated by spaces
for (int i = 0; i < n; i++)
scanf(" %d", &a[i]);
find_two_largest(a, n, &largest, &second_largest);
if (n == 0)
// Your code here
//Print No numbers were entered.
else if (n == 1)
// Your code here
//Print Only one number was entered. Largest:
else
// Your code here
//Print Largest: , Second Largest:
return 0;
}
void find_two_largest(int a[], int n, int *largest, int *second_largest)
{
// Your code here (Using pointer to finish)
}
Example input:
0
Example output:
No numbers were entered.
Example input:
1
67
Example output:
Only one number was entered. Largest: 67
Example input:
5
5 6 7 8 9
Example output:
Largest: 9, Second Largest: 8