# Answer to Question #71834 in C++ for m

Question #71834

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.

The first function to sort the numbers and the second function to reverse the result of the first function.

Expert's answer

#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] << " ";

}

reverseArr(arr, n);

cout << "\nThe reversed array:\n";

for(int i = 0; i < n; ++i)

{

cout << arr[i] << " ";

}

return 0;

}

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] << " ";

}

reverseArr(arr, n);

cout << "\nThe reversed array:\n";

for(int i = 0; i < n; ++i)

{

cout << arr[i] << " ";

}

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment