Skip to content

Instantly share code, notes, and snippets.

@srezasm
Created December 27, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srezasm/ae036b89bfa97d9de8a51335d212b247 to your computer and use it in GitHub Desktop.
Save srezasm/ae036b89bfa97d9de8a51335d212b247 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void CinArray(int arr[], int n) {
cout << "enter " << n << " numbers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
}
void CoutArray(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
cout << arr[i] << " ";
}
cout << arr[n - 1] << endl;
}
int RecursiveGetMax(int arr[], int n, int max = 0) {
if (n == -1)
return arr[max];
if (arr[n - 1] > arr[max])
max = n - 1;
return RecursiveGetMax(arr, n - 1, max);
}
void RecursiveSort(int arr[], int n, int start = 0) {
if (start != n) {
for (int i = start; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
arr[i] += arr[j];
arr[j] = arr[i] - arr[j];
arr[i] -= arr[j];
}
}
}
RecursiveSort(arr, n, start + 1);
}
}
int main() {
int n;
cout << "enter n: ";
cin >> n;
int array[n];
CinArray(array, n);
int m = RecursiveGetMax(array, n);
cout << "the maximum is " << m << endl;
RecursiveSort(array, n);
cout << "sorted array is: ";
CoutArray(array, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment