60 690
Assignments Done
97,9%
Successfully Done
In April 2018

Answer to Question #71580 in C++ for m

Question #71580
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
Expert's answer
#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];

showArray(array, size);
sortArray(array, size);
showArray(array, size);
reverseArray(array, size);
showArray(array, size);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions