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…).
using while, for and array if possible
1
Expert's answer
2017-12-06T07:13:06-0500
#include <iostream>
using namespace std;
void showArray(int ar[], int size) { for (int i = 0; i < size; i++) cout << ar[i] << " "; cout << endl; } void sortArray(int ar[], int size) { int i = 0; while (i < size) { if (i == 0 || ar[i - 1] >= ar[i] ) i++; else { int temp = ar[i]; ar[i] = ar[i - 1]; ar[i - 1] = temp; i--; } } } void reverseArray(int ar[], int size) { for (int i = 0, j = size - 1; i < j; i++, j--){ int temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } }
int main() { size_t size; cout << "Input size of array: "; cin >> size; int array[size]; cout << "Input " << size << " elements.\n"; for (int i = 0; i < size; i++) cin >> array[i];
Comments
Leave a comment