Code example:
#include <> // Function is used to find the maximum and submaximum values in an arrayvoid LargestTow(int a[], int n, int *pfirst, int *psecond) { *pfirst = a[0]; *psecond = a[1]; if (*psecond > *pfirst) { // If the initial submaximum value is greater than the maximum value, swap them int temp = *pfirst; *pfirst = *psecond; *psecond = temp; } for (int i = 2; i < n; i++) { if (a[i] > *pfirst) { // If the current element is greater than the maximum value, the update value is the original maximum value, and the maximum value is updated to the current element *psecond = *pfirst; *pfirst = a[i]; } else if (a[i] > *psecond) { // If the current element is greater than the minimum value but less than the maximum value, the update value is the current element *psecond = a[i]; } } } int main() { int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int max, second_max; LargestTow(a, n, &max, &second_max); printf("%d %d\n", max, second_max); return 0; }
The following is the idea of traversing the array first to find the maximum value, and then traversing the array again to find the minimum value. Use the code written in C language to solve the above problem (find out)n
The maximum and submaximum values of integers):
#include <> // Function is used to find the maximum and submaximum values in an arrayvoid LargestTow(int a[], int n, int *pfirst, int *psecond) { int max_value = a[0]; int max_index = 0; // traverse the array for the first time to find the maximum value and its index for (int i = 1; i < n; i++) { if (a[i] > max_value) { max_value = a[i]; max_index = i; } } *pfirst = max_value; // Set the element at the maximum value to a very small value to avoid it from interfering with finding the maximum value a[max_index] = -99999999; int second_max_value = a[0]; // traverse the array for the second time and find the sub-maximum value for (int i = 1; i < n; i++) { if (a[i] > second_max_value) { second_max_value = a[i]; } } *psecond = second_max_value; } int main() { int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int max, second_max; LargestTow(a, n, &max, &second_max); printf("%d %d\n", max, second_max); return 0; }
Question description
Find the maximum and submaximum values among n integers. It is required to define a function LargestTow(), find the maximum value and the secondary maximum value of array a, and store the storage units referred to by the formal parameter pointers pfirst and psecond respectively. The function prototype is as follows:
void LargestTow(int a[],int n,int *pfirst,int *psecond) { /*Array a has n elements, and the maximum value in the array is stored in the memory unit pointed to by the parameter pointer pfirst, and the second largest value in the array is stored in the memory unit pointed to by the parameter pointer psecond. */ }
Enter a description
There are two lines in the input. The first line is an integer n, 1<n<=1000; the second line is n integers, separated by spaces.
Output description
Enter two integers to represent the two largest values in the array. The output takes up one line.
Sample input
5
6 3 4 9 8
Sample output
9 8
Summarize
This is the article about using pointer functions to find the maximum and submaximum values in an array in C language. For more related content in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!