12/ Write two function to solve (Write a program that asks the user to enter an array of random numbers, then sort the numbers (descending order), after that reverse it (the first element will be the last…). )
The first function to sort the numbers and the second function to reverse the result of the first function.
1
Expert's answer
2017-12-13T12:10:07-0500
#include <iostream>
using namespace std;
void sortArray(double *arr, int n) { double minel; int mini; for(int i = 0; i < n - 1; ++i) { minel = arr[i]; mini = i; for(int j = i + 1; j < n; ++j) { if(arr[j] > minel) { minel = arr[j]; mini = j; } } arr[mini] = arr[i]; arr[i] = minel; } }
void reverseArr(double *arr, int n) { double change; for(int i = 0; i < n / 2; ++i) { change = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = change; } } int main() { int n; double *arr; cout << "Please enter the number of array elements: "; cin >> n; arr = new double [n]; for(int i = 0; i < n; ++i) { cout << "Please enter the element # " << (i + 1) << ": "; cin >> arr[i]; }
sortArray(arr, n); cout << "The sorted array:\n"; for(int i = 0; i < n; ++i) { cout << arr[i] << " "; }
Comments
Leave a comment