USING C++
NO Vectors please. Only arrays. Please include comments and reasonable variable names so I can understand.
Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.
NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.
After it has been sorted in descending order, go through all the items in the array and make sure that they all have the same number of digits as the largest element in the array by adding additional 5’s to the end of the numbers. For example, given the following sorted array:
324, 46, 6
After adding the additional digits, the array will look like the following:
324, 465, 655
Read the original data elements from a file. The elements in the file will be separated by some kind of white space, just like before. The number of elements will not exceed 10.
Screenshot
------------------------------------------------------------------------------------
Program
//Header files
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
//Function proto types
void quickSort(int *arr, int l, int r);
int divide(int *arr, int l, int r);
int numOfDigits(int num);
int main()
{
//Variable for the number of elements in the
array
int count=0;
//Declare an array
int arr[10];
//File path
ifstream in("C:/Users/deept/Desktop/input.txt");
//Error check
if (!in) {
cout << "File can't open!!"
<< endl;
exit(0);
}
//Read elements and store into array
while (!in.eof()) {
in >> arr[count];
count++;
}
//Caall quick sort
quickSort(arr, 0, count-1);
//Print sorted array
cout << "Array after sort: ";
for (int i = 0; i < count; i++) {
cout << arr[i] << "
";
}
cout << endl;
//Fing highest value's digit count
int maxDigit=numOfDigits(arr[0]);
//Append 5 at the end of other elements
for (int i = 1; i < count; i++) {
int numDigits =
maxDigit-numOfDigits(arr[i]);
string str =
to_string(arr[i]);
for (int j = 0; j < numDigits;
j++) {
str +=
'5';
}
arr[i]=stoi(str);
}
//Display array after append
cout << "Display array after append: ";
for (int i = 0; i < count; i++) {
cout << arr[i] << "
";
}
}
//Method for quick sort
//Parametres are array reference starting and ending positions of
array
//Return nothing
void quickSort(int *arr, int l, int r) {
if (l < r) {
//Get pivot value
int pvt =divide(arr, l, r);
//According to pivot divide
array
quickSort(arr, l, pvt - 1);
quickSort(arr, pvt + 1, r);
}
}
//Method to find pivot
//Take array reference,array start and end as parameters
int divide(int *arr, int l, int r) {
//Take last element as pivot
int pvt = arr[r];
//Less than pivot start
int i = l;
//Sort loop
for (int j = l; j < r; j++) {
//If element greater than pivot
swap
if (arr[j] > pvt) {
int temp =
arr[j];
arr[j] =
arr[i];
arr[i] =
temp;
i++;
}
}
//Set next pivot
arr[r] = arr[i];
arr[i] = pvt;
return i;
}
//Count the digits in a number
int numOfDigits(int num)
{
int digitCount = 0;
while (num != 0) {
num = num / 10;
++digitCount;
}
return digitCount;
}
-------------------------------------------------------------------
Output
Array after sort: 324 46 6
Display array after append: 324 465 655
---------------------------------------------------------
File:-
6 46 324
USING C++ NO Vectors please. Only arrays. Please include comments and reasonable variable names so I...