Week 6 Programming Questions

Filter:
Sort:

Exercise 1

Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array then print the array. After printing the array your program should also prompt the user enter startValue, and endValue. The program should print the number of values that are in between the value startValue and endValue inclusive where startValue < endValue.

Example:

Enter the number of elements (up to 50): 5
Enter 5 integers:
10 5 20 8 15
Array elements: 10 5 20 8 15 

Enter startValue and endValue: 5 12
Count of values between 5 and 12: 3
Solution
#include <stdio.h>

int main() {
    int arr[50];
    int n, startValue, endValue, count = 0;

    printf("Enter the number of elements (up to 50): ");
    scanf("%d", &n);

    printf("Enter %d integers:\n", n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Array elements: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    printf("Enter startValue and endValue: ");
    scanf("%d %d", &startValue, &endValue);

    for(int i = 0; i < n; i++) {
        if(arr[i] >= startValue && arr[i] <= endValue) {
            count++;
        }
    }

    printf("Count of values between %d and %d: %d\n", startValue, endValue, count);

    return 0;
}
                        
#include <iostream>
using namespace std;

int main() {
    int arr[50];
    int n, startValue, endValue, count = 0;

    cout << "Enter the number of elements (up to 50): ";
    cin >> n;

    cout << "Enter " << n << " integers:\n";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    cout << "Array elements: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << "\n";

    cout << "Enter startValue and endValue: ";
    cin >> startValue >> endValue;

    for (int i = 0; i < n; i++) {
        if (arr[i] >= startValue && arr[i] <= endValue) {
            count++;
        }
    }

    cout << "Count of values between " << startValue << " and " << endValue << ": " << count << "\n";

    return 0;
}

                        

Exercise 2

Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array with grades then print the array. Then compares the sum of even numbers in the array with the sum of odd numbers in the array.

  • If the sum of even numbers is larger, the program prints 1.
  • If the sum of odd numbers is larger, the program prints -1.
  • If both sums are equal, the program prints 0.

Example:

Enter the number of elements (up to 50): 4
Enter 4 grades:
2 3 4 1
Array elements: 2 3 4 1 
1
Solution
#include <stdio.h>

int main() {
    int arr[50];
    int n, sumEven = 0, sumOdd = 0;

    printf("Enter the number of elements (up to 50): ");
    scanf("%d", &n);

    printf("Enter %d grades:\n", n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
        if(arr[i] % 2 == 0) {
            sumEven += arr[i];
        } else {
            sumOdd += arr[i];
        }
    }

    printf("Array elements: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    if(sumEven > sumOdd) {
        printf("1\n");
    } else if(sumOdd > sumEven) {
        printf("-1\n");
    } else {
        printf("0\n");
    }

    return 0;
}
                        
#include <iostream>
using namespace std;

int main() {
    int arr[50];
    int n, sumEven = 0, sumOdd = 0;

    cout << "Enter the number of elements (up to 50): ";
    cin >> n;

    cout << "Enter " << n << " grades:\n";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        if (arr[i] % 2 == 0) {
            sumEven += arr[i];
        } else {
            sumOdd += arr[i];
        }
    }

    cout << "Array elements: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << "\n";

    if (sumEven > sumOdd) {
        cout << "1\n";
    } else if (sumOdd > sumEven) {
        cout << "-1\n";
    } else {
        cout << "0\n";
    }

    return 0;
}

                        

Exercise 3

Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array with grades then print the array. Then program then prints the number of the elements in the array that are multiples of five.

Example:

Enter the number of elements (up to 50): 7
Enter 7 grades:
15 17 413 1005 3 70 2
Array elements: 15 17 413 1005 3 70 2 
Multiples of five: 15 1005 70 
Total count: 3
Solution
#include <stdio.h>

int main() {
    int arr[50];
    int n, count = 0;

    printf("Enter the number of elements (up to 50): ");
    scanf("%d", &n);

    printf("Enter %d grades:\n", n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Array elements: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    printf("Multiples of five: ");
    for(int i = 0; i < n; i++) {
        if(arr[i] % 5 == 0) {
            printf("%d ", arr[i]);
            count++;
        }
    }
    printf("\nTotal count: %d\n", count);

    return 0;
}
                        
#include <iostream>
using namespace std;

int main() {
    int arr[50];
    int n, count = 0;

    cout << "Enter the number of elements (up to 50): ";
    cin >> n;

    cout << "Enter " << n << " grades:\n";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    cout << "Array elements: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << "\n";

    cout << "Multiples of five: ";
    for (int i = 0; i < n; i++) {
        if (arr[i] % 5 == 0) {
            cout << arr[i] << " ";
            count++;
        }
    }
    cout << "\nTotal count: " << count << "\n";

    return 0;
}

                        

Exercise 4

Write a complete CC++ program that defines two integer arrays each of size 50, the prompts the user to fill the arrays with integers. Then program prompts the user to enter the value of an integer variable named Append. The program should combine the arrays A & B and store the final results in C as follows:

  • If Append is equal to 1 the program appends A to B.
  • If Append is equal to 0 the function appends B to A.

Example:

Enter number of elements for A: 4
Enter elements for A:
10 20 30 2
Enter number of elements for B: 3
Enter elements for B:
2 34 8
Enter Append value (0 or 1): 1
Result Array C: 2 34 8 10 20 30 2 
Solution
#include <stdio.h>

int main() {
    int A[50], B[50], C[100];
    int nA, nB, nC = 0, appendChoice;

    printf("Enter number of elements for A: ");
    scanf("%d", &nA);
    printf("Enter elements for A:\n");
    for(int i = 0; i < nA; i++) scanf("%d", &A[i]);

    printf("Enter number of elements for B: ");
    scanf("%d", &nB);
    printf("Enter elements for B:\n");
    for(int i = 0; i < nB; i++) scanf("%d", &B[i]);

    printf("Enter Append value (0 or 1): ");
    scanf("%d", &appendChoice);

    if(appendChoice == 1) {
        for(int i = 0; i < nB; i++) {
            C[nC] = B[i];
            nC++;
        }
        for(int i = 0; i < nA; i++) {
            C[nC] = A[i];
            nC++;
        }
    } else if (appendChoice == 0) {
        for(int i = 0; i < nA; i++) {
            C[nC] = A[i];
            nC++;
        }
        for(int i = 0; i < nB; i++) {
            C[nC] = B[i];
            nC++;
        }
    }

    printf("Result Array C: ");
    for(int i = 0; i < nC; i++) {
        printf("%d ", C[i]);
    }
    printf("\n");

    return 0;
}
                        
#include <iostream>
using namespace std;

int main() {
    int A[50], B[50], C[100];
    int nA, nB, nC = 0, appendChoice;

    cout << "Enter number of elements for A: ";
    cin >> nA;
    cout << "Enter elements for A:\n";
    for (int i = 0; i < nA; i++) cin >> A[i];

    cout << "Enter number of elements for B: ";
    cin >> nB;
    cout << "Enter elements for B:\n";
    for (int i = 0; i < nB; i++) cin >> B[i];

    cout << "Enter Append value (0 or 1): ";
    cin >> appendChoice;

    if (appendChoice == 1) {
        for (int i = 0; i < nB; i++) C[nC++] = B[i];
        for (int i = 0; i < nA; i++) C[nC++] = A[i];
    } else if (appendChoice == 0) {
        for (int i = 0; i < nA; i++) C[nC++] = A[i];
        for (int i = 0; i < nB; i++) C[nC++] = B[i];
    }

    cout << "Result Array C: ";
    for (int i = 0; i < nC; i++) {
        cout << C[i] << " ";
    }
    cout << "\n";

    return 0;
}

                        

Exercise 5

Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array with quiz grades (between 0 and 10 inclusive). Then the program computes the frequency of each value in the array and prints the most frequent element.

Example:

Enter the number of elements (up to 50): 10
Enter 10 quiz grades (0-10):
5 8 5 9 5 8 10 5 7 5
Most frequent element is: 5 (appeared 5 times)
Solution
#include <stdio.h>

int main() {
    int arr[50];
    int freq[11] = {0}; 
    int n;

    printf("Enter the number of elements (up to 50): ");
    scanf("%d", &n);

    printf("Enter %d quiz grades (0-10):\n", n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
        if(arr[i] >= 0 && arr[i] <= 10) {
            freq[arr[i]]++;
        }
    }

    int maxFreq = -1;
    int mostFrequentVal = -1;

    for(int i = 0; i <= 10; i++) {
        if(freq[i] > maxFreq) {
            maxFreq = freq[i];
            mostFrequentVal = i;
        }
    }

    printf("Most frequent element is: %d (appeared %d times)\n", mostFrequentVal, maxFreq);

    return 0;
}
                        
#include <iostream>
using namespace std;

int main() {
    int arr[50];
    int freq[11] = {0};
    int n;

    cout << "Enter the number of elements (up to 50): ";
    cin >> n;

    cout << "Enter " << n << " quiz grades (0-10):\n";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        if (arr[i] >= 0 && arr[i] <= 10) {
            freq[arr[i]]++;
        }
    }

    int maxFreq = -1;
    int mostFrequentVal = -1;

    for (int i = 0; i <= 10; i++) {
        if (freq[i] > maxFreq) {
            maxFreq = freq[i];
            mostFrequentVal = i;
        }
    }

    cout << "Most frequent element is: " << mostFrequentVal << " (appeared " << maxFreq << " times)\n";

    return 0;
}

                        

Exercise 6

Write a complete CC++ program including a main and three functions named Max, CountAbove, and IndexOf.

  • Max() takes an array of integer (grades) and its size as integer as input parameters and returns the maximum grade in the array as integer type.
  • SecondMax() takes an array of integer (grades) and its size as integer as input parameters and returns the second maximum grade in the array as integer type.
  • CountAbove() takes an array of integers, its size as integer, and a score value as integer input parameters and returns the number of grades in the array that are more or equal to the input score.
  • IndexOf() takes an array of integers, its size as integer, and a score value as integer input parameters and returns the location of the first occurrence of the input score. Location means index. If the value is not found, it should return -1.
  • MostAppeared() takes an array of integers, its size as integer input parameters and returns the value that is most appeared (occurred) within array elements.
  • FillNoDuplicate() takes an array of integers, its size, and low and high as integer input parameters. This function should fill the array with random numbers from low to high with no duplications.
  • PrintGrade() takes an array of integers, its size as integer input parameters. This function prints grade values.

Main Function Logic: The main() defines an integer array of size 70 and read the grades values from the user. The main function then:

  1. Calls Max to find the maximum value.
  2. Calls SecondMax to find the second maximum.
  3. Calls CountAbove to compute the number of Passing grades (grades >= 50).
  4. Calls IndexOf to find out the location of a grade entered by the user.
  5. Calls PrintGrade to display the grades values.
  6. Calls MostAppeared to see which element appeared most.

Finally, define another array of size 70 and try to fill its values by using FillNoDuplicate, then call PrintGrade in order to see grades for this array.

Example Run:

Enter number of students (up to 70): 5
Enter grades:
50 85 40 90 85
Max Grade: 90
Second Max Grade: 85
Number of Passing Grades (>=50): 4
Enter a grade to find its index: 85
Index of 85: 1
Most Appeared Grade: 85
Grades entered: 50 85 40 90 85 

Filling new array with unique random numbers (0-100)...
12 5 23 8 19 
Solution
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int Max(int arr[], int size) {
    int maxVal = arr[0];
    for(int i = 1; i < size; i++) {
        if(arr[i] > maxVal) maxVal = arr[i];
    }
    return maxVal;
}

int SecondMax(int arr[], int size) {
    int maxVal = Max(arr, size);
    int secMax = -2147483648; 
    
    for(int i = 0; i < size; i++) {
        if(arr[i] != maxVal && arr[i] > secMax) {
            secMax = arr[i];
        }
    }
    return secMax;
}

int CountAbove(int arr[], int size, int score) {
    int count = 0;
    for(int i = 0; i < size; i++) {
        if(arr[i] >= score) count++;
    }
    return count;
}

int IndexOf(int arr[], int size, int score) {
    for(int i = 0; i < size; i++) {
        if(arr[i] == score) return i;
    }
    return -1;
}

int MostAppeared(int arr[], int size) {
    int maxCount = 0;
    int mostElement = arr[0];
    
    for(int i = 0; i < size; i++) {
        int count = 0;
        for(int j = 0; j < size; j++) {
            if(arr[j] == arr[i]) count++;
        }
        if(count > maxCount) {
            maxCount = count;
            mostElement = arr[i];
        }
    }
    return mostElement;
}

void FillNoDuplicate(int arr[], int size, int low, int high) {
    int count = 0;
    while(count < size) {
        int num = (rand() % (high - low + 1)) + low;
        int found = 0;
        for(int i = 0; i < count; i++) {
            if(arr[i] == num) {
                found = 1;
                break;
            }
        }
        if(!found) {
            arr[count] = num;
            count++;
        }
    }
}

void PrintGrade(int arr[], int size) {
    for(int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    srand(time(0)); 
    int grades[70];
    int n, searchGrade;

    printf("Enter number of students (up to 70): ");
    scanf("%d", &n);

    printf("Enter grades:\n");
    for(int i = 0; i < n; i++) {
        scanf("%d", &grades[i]);
    }

    printf("Max Grade: %d\n", Max(grades, n));
    printf("Second Max Grade: %d\n", SecondMax(grades, n));
    printf("Number of Passing Grades (>=50): %d\n", CountAbove(grades, n, 50));

    printf("Enter a grade to find its index: ");
    scanf("%d", &searchGrade);
    printf("Index of %d: %d\n", searchGrade, IndexOf(grades, n, searchGrade));
    
    printf("Most Appeared Grade: %d\n", MostAppeared(grades, n));
    
    printf("Grades entered: ");
    PrintGrade(grades, n);

    int randomGrades[70];
    printf("\nFilling new array with unique random numbers (0-100)...\n");
    FillNoDuplicate(randomGrades, n, 0, 100);
    PrintGrade(randomGrades, n);

    return 0;
}
                        
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Max(int arr[], int size) {
    int maxVal = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > maxVal) maxVal = arr[i];
    }
    return maxVal;
}

int SecondMax(int arr[], int size) {
    int maxVal = Max(arr, size);
    int secMax = -2147483648;
    for (int i = 0; i < size; i++) {
        if (arr[i] != maxVal && arr[i] > secMax) {
            secMax = arr[i];
        }
    }
    return secMax;
}

int CountAbove(int arr[], int size, int score) {
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i] >= score) count++;
    }
    return count;
}

int IndexOf(int arr[], int size, int score) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == score) return i;
    }
    return -1;
}

int MostAppeared(int arr[], int size) {
    int maxCount = 0;
    int mostElement = arr[0];
    for (int i = 0; i < size; i++) {
        int count = 0;
        for (int j = 0; j < size; j++) {
            if (arr[j] == arr[i]) count++;
        }
        if (count > maxCount) {
            maxCount = count;
            mostElement = arr[i];
        }
    }
    return mostElement;
}

void FillNoDuplicate(int arr[], int size, int low, int high) {
    int count = 0;
    while (count < size) {
        int num = (rand() % (high - low + 1)) + low;
        int found = 0;
        for (int i = 0; i < count; i++) {
            if (arr[i] == num) { found = 1; break; }
        }
        if (!found) arr[count++] = num;
    }
}

void PrintGrade(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << "\n";
}

int main() {
    srand(time(0));
    int grades[70];
    int n, searchGrade;

    cout << "Enter number of students (up to 70): ";
    cin >> n;

    cout << "Enter grades:\n";
    for (int i = 0; i < n; i++) {
        cin >> grades[i];
    }

    cout << "Max Grade: " << Max(grades, n) << "\n";
    cout << "Second Max Grade: " << SecondMax(grades, n) << "\n";
    cout << "Number of Passing Grades (>=50): " << CountAbove(grades, n, 50) << "\n";

    cout << "Enter a grade to find its index: ";
    cin >> searchGrade;
    cout << "Index of " << searchGrade << ": " << IndexOf(grades, n, searchGrade) << "\n";

    cout << "Most Appeared Grade: " << MostAppeared(grades, n) << "\n";

    cout << "Grades entered: ";
    PrintGrade(grades, n);

    int randomGrades[70];
    cout << "\nFilling new array with unique random numbers (0-100)...\n";
    FillNoDuplicate(randomGrades, n, 0, 100);
    PrintGrade(randomGrades, n);

    return 0;
}

                        

Exercise 7

Write a function RemoveDuplicates() that receives an array of integers and its size as input parameters. This function should modify the array in-place to remove any duplicate elements, shifting all subsequent unique elements to the left. The function should return the new logical size of the array.

In main(), prompt the user to input the number of elements and fill the array. Call the function to remove duplicates, then print the unique array elements along with the new size.

Example:

Enter the number of elements: 7
Enter 7 elements: 10 20 10 30 20 10 40
Array after removing duplicates: 10 20 30 40 
New size: 4
Solution
#include <stdio.h>

int RemoveDuplicates(int arr[], int size);

int main() {
    int size;
    int arr[50];

    printf("Enter the number of elements: ");
    scanf("%d", &size);

    printf("Enter %d elements: ", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    int newSize = RemoveDuplicates(arr, size);

    printf("Array after removing duplicates: ");
    for (int i = 0; i < newSize; i++) {
        printf("%d ", arr[i]);
    }
    printf("\nNew size: %d\n", newSize);

    return 0;
}

int RemoveDuplicates(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                for (int k = j; k < size - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                size--;
                j--;
            }
        }
    }
    return size;
}

                        
#include <iostream>
using namespace std;

int RemoveDuplicates(int arr[], int size);

int main() {
    int size;
    int arr[50];

    cout << "Enter the number of elements: ";
    cin >> size;

    cout << "Enter " << size << " elements: ";
    for (int i = 0; i < size; i++) {
        cin >> arr[i];
    }

    int newSize = RemoveDuplicates(arr, size);

    cout << "Array after removing duplicates: ";
    for (int i = 0; i < newSize; i++) {
        cout << arr[i] << " ";
    }
    cout << "\nNew size: " << newSize << endl;

    return 0;
}

int RemoveDuplicates(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                for (int k = j; k < size - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                size--;
                j--;
            }
        }
    }
    return size;
}

                        

Exercise 8

Write a function MergeSorted() that receives two pre-sorted integer arrays $A$ and $B$, their sizes, and a third array $C$ of size $sizeA + sizeB$. The function should merge the sorted elements of $A$ and $B$ into $C$ such that $C$ remains sorted.

In main(), read two sorted arrays from the user, call the function to merge them, and print the merged array $C$.

Example:

Enter size of A: 3
Enter 3 sorted elements for A: 1 5 8
Enter size of B: 4
Enter 4 sorted elements for B: 2 4 9 10
Merged array C: 1 2 4 5 8 9 10 
Solution
#include <stdio.h>

void MergeSorted(int A[], int sizeA, int B[], int sizeB, int C[]);

int main() {
    int sizeA, sizeB;
    int A[50], B[50], C[100];

    printf("Enter size of A: ");
    scanf("%d", &sizeA);
    printf("Enter %d sorted elements for A: ", sizeA);
    for (int i = 0; i < sizeA; i++) {
        scanf("%d", &A[i]);
    }

    printf("Enter size of B: ");
    scanf("%d", &sizeB);
    printf("Enter %d sorted elements for B: ", sizeB);
    for (int i = 0; i < sizeB; i++) {
        scanf("%d", &B[i]);
    }

    MergeSorted(A, sizeA, B, sizeB, C);

    printf("Merged array C: ");
    for (int i = 0; i < sizeA + sizeB; i++) {
        printf("%d ", C[i]);
    }
    printf("\n");

    return 0;
}

void MergeSorted(int A[], int sizeA, int B[], int sizeB, int C[]) {
    int i = 0, j = 0, k = 0;

    while (i < sizeA && j < sizeB) {
        if (A[i] < B[j]) {
            C[k++] = A[i++];
        } else {
            C[k++] = B[j++];
        }
    }

    while (i < sizeA) {
        C[k++] = A[i++];
    }

    // Copy remaining items of B if any
    while (j < sizeB) {
        C[k++] = B[j++];
    }
}

                        
#include <iostream>
using namespace std;

void MergeSorted(int A[], int sizeA, int B[], int sizeB, int C[]);

int main() {
    int sizeA, sizeB;
    int A[50], B[50], C[100];

    cout << "Enter size of A: ";
    cin >> sizeA;
    cout << "Enter " << sizeA << " sorted elements for A: ";
    for (int i = 0; i < sizeA; i++) {
        cin >> A[i];
    }

    cout << "Enter size of B: ";
    cin >> sizeB;
    cout << "Enter " << sizeB << " sorted elements for B: ";
    for (int i = 0; i < sizeB; i++) {
        cin >> B[i];
    }

    MergeSorted(A, sizeA, B, sizeB, C);

    cout << "Merged array C: ";
    for (int i = 0; i < sizeA + sizeB; i++) {
        cout << C[i] << " ";
    }
    cout << endl;

    return 0;
}

void MergeSorted(int A[], int sizeA, int B[], int sizeB, int C[]) {
    int i = 0, j = 0, k = 0;

    while (i < sizeA && j < sizeB) {
        if (A[i] < B[j]) {
            C[k++] = A[i++];
        } else {
            C[k++] = B[j++];
        }
    }

    while (i < sizeA) {
        C[k++] = A[i++];
    }

    while (j < sizeB) {
        C[k++] = B[j++];
    }
}

                        

Exercise 9

An element in an array is called a leader if it is strictly greater than all elements situated to its right. The rightmost element is always a leader. Write a function FindLeaders() that receives an array of integers and its size, and prints all the leaders in the array.

In main(), read the array size and elements, and call FindLeaders() to print the results.

Example:

Enter number of elements: 6
Enter 6 elements: 16 17 4 3 5 2
Leaders in the array: 17 5 2 
Solution
#include <stdio.h>

void FindLeaders(int arr[], int size);

int main() {
    int size;
    int arr[50];

    printf("Enter number of elements: ");
    scanf("%d", &size);

    printf("Enter %d elements: ", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    FindLeaders(arr, size);

    return 0;
}

void FindLeaders(int arr[], int size) {
    printf("Leaders in the array: ");
    
    int current_max = arr[size - 1];
    
    int leaders[50];
    int leaderCount = 0;
    
    leaders[leaderCount++] = current_max;

    for (int i = size - 2; i >= 0; i--) {
        if (arr[i] > current_max) {
            current_max = arr[i];
            leaders[leaderCount++] = current_max;
        }
    }

    for (int i = leaderCount - 1; i >= 0; i--) {
        printf("%d ", leaders[i]);
    }
    printf("\n");
}

                        
#include <iostream>
using namespace std;

void FindLeaders(int arr[], int size);

int main() {
    int size;
    int arr[50];

    cout << "Enter number of elements: ";
    cin >> size;

    cout << "Enter " << size << " elements: ";
    for (int i = 0; i < size; i++) {
        cin >> arr[i];
    }

    FindLeaders(arr, size);

    return 0;
}

void FindLeaders(int arr[], int size) {
    cout << "Leaders in the array: ";
    
    int current_max = arr[size - 1];
    
    int leaders[50];
    int leaderCount = 0;
    
    leaders[leaderCount++] = current_max;

    for (int i = size - 2; i >= 0; i--) {
        if (arr[i] > current_max) {
            current_max = arr[i];
            leaders[leaderCount++] = current_max;
        }
    }

    for (int i = leaderCount - 1; i >= 0; i--) {
        cout << leaders[i] << " ";
    }
    cout << endl;
}