FIRST AND SECOND ON THE RANKING LIST - SOLUTION
The solution is in the recursive repetition of the function where the current position , pointers of to two integer data that remember the currently largest and first next in size are passed as parameters. This function compares the currently largest member with the largest element in the rest of the array to the left of the current position. The latter is determined by a recursive call of the same function, but the current position which is passed, is 1 less than it was in the previous call.
If that function returns an element that is larger than the currently stored largest, then that value becomes the largest, and by then the largest becomes the second largest element of array.
In the case that the largest element in the subarray to the left of the current position is not larger than the currently largest, then it is checked that it may not be larger than the second largest. If so, then the new the second largest becomes just that value. Otherwise there is no change.
The initial values for the first and second largest are initially set to values that are certainly less than the elements of the array, and these are the values of -1 in this solution, because no negative values in the array are expected.
If that function returns an element that is larger than the currently stored largest, then that value becomes the largest, and by then the largest becomes the second largest element of array.
In the case that the largest element in the subarray to the left of the current position is not larger than the currently largest, then it is checked that it may not be larger than the second largest. If so, then the new the second largest becomes just that value. Otherwise there is no change.
The initial values for the first and second largest are initially set to values that are certainly less than the elements of the array, and these are the values of -1 in this solution, because no negative values in the array are expected.
It should also be noted that variables representing the first and second largest elements of an array (ft and sc) are passed to a function as a reference, not as values, because the changes of that variable within one of the function's recurrent calls are done be retained in other function calls and within the main function too. If the parameters were passed by value, then the changes would be valid only in the function or call in which they occurred. You can read more about passing variables to function by parameters and by references on the webpage:
Functions in C/C++
Functions in C/C++
#include < stdio.h>
/*A function that is called recursively and that determines the largest as well as the second largest value*/
/*Parameters: position of element in array, the array, pointers to the first and second largest element of array*/
void twoLargest(int n,int A[], int *ft,int *sc)
{
int main() {
using namespace std;
/*A function that is called recursively and that determines the largest as well as the second largest value*/
/*Parameters: position of element in array, the array, pointers to the first and second largest element of array*/
void twoLargest(int n,int A[], int *ft,int *sc)
{
if(n==0)
}
(*ft)=-1;//At the beginning, we initialize the first largest element to -1
(*sc)=-1;//At the beginning, we initialize the secondlargest element to -1
else(*sc)=-1;//At the beginning, we initialize the secondlargest element to -1
twoLargest(n-1,A,ft,sc);//Determines the first and second largest in the array to the left of the observed position
if(A[n-1] > (*ft))
{
else if(A[n-1] > (*d))
{
if(A[n-1] > (*ft))
{
(*sc) = (*ft);
(*ft) = A[n-1];
}(*ft) = A[n-1];
else if(A[n-1] > (*d))
{
(*sc)=A[n-1];
}int main() {
int n,Ft,Sc;
scanf("%d",&n);
int A[n];
for(int i = 0; i < n; i++)
{
twoLargest(n, A, &Ft, &Sc);
printf("%d\n",Ft);
printf("%d\n",Sc);
return 0;
}scanf("%d",&n);
int A[n];
for(int i = 0; i < n; i++)
{
scanf("%d",&A[i]);
}twoLargest(n, A, &Ft, &Sc);
printf("%d\n",Ft);
printf("%d\n",Sc);
return 0;