Copy the template for program and review it Note how the sam

Copy the template for program and review it. Note how the same function can sort parallel arrays using different arrays (keys) to sort by just by changing the order of the parameters. Finish/modify the comments to the template as appropriate for the course.
Add a function to sort the array arrays in a descending manner using selection or insertion sorts. Call this function to sort descending (high to low) using y as the key, call the function to print the arrays, call the function to sort descending using z as the key, and call the function again to print the arrays.
Do not use break, continue, or return in your loops.
Hint: The change from ascending to descending only involves changing relational operators

Make sure that you have included your comments containing the purpose of the functions i.e. what problem or task is being solved plus the input needed function call, output expected from the function (either using cout or sending back to the function call), and the processing needed to get the output from the input immediately. The description of the purpose should be detailed enough that a person reading your code would not need any

#include <iostream>
#include <iomanip>
using namespace std;

void printArrays(const double [], const double [], const double [], int size);
void sortAscend(double [], double [], double [], int );
//add prototype for descending sort

int main()
{
   double x[10] = {4.5, 9.8, 1.2, 10.9, 5.5, 6.2, 3.2, 10.5, 2.9, 5.4};
   double y[10] = {11.6, 3.2, 5.9, 8.8, 1.3, 7.2, 4.3, 2.2, 15.3, 7.8};
   double z[10] = {1.2, 6.7, 2.3, 14.8, 5.3, 9.2, 11.5, 4.6, 8.9, 6.1};


   cout<<\"Original array\ x y z\"<<endl; //output of title line

   //call function to output arrays
   printArrays(x,y,z,10);


   //call function to sort ascending by x
   sortAscend(x, y, z, 10);

   cout<<\"\ \ Array sorted ascending by x\ x y z\"<<endl; //output of title line
   //call function to output arrays
   printArrays(x,y,z,10);

   //call function to sort ascending by z (note z is passed as key)
   sortAscend(z, y, x, 10);

   cout<<\"\ \ Array sorted ascending by z\ x y z\"<<endl; //output of title line
   //call function to output arrays
   printArrays(x,y,z,10);

   //call function to sort descending by y

   //call function to output arrays


   //call function to sort descending by z

   //call function to output arrays
   return 0;
}

/* Function to output three 1-dimensional double arrays
input: three 1- dimensional double arrays and size of the arrays
output: The values in the arrays
processing: loop that runs from 0 to size of arrays - 1
*/
void printArrays(const double a[], const double b[], const double c[], int size)
{
   //loop to output elements of the arrays
   for(int i = 0; i < size; i++)
   {
       cout<<setprecision(1)<<fixed<<setw(5)<<a[i]<<setw(10)<<b[i]<<setw(10)<<c[i]<<endl;
   }
}


/* Function to sort three 1-dimensional double arrays in an ascending manner using bubble sort algorithm.
Function will sort according to the first array listed in the call
input: 3 double arrays and the size of the arrays, the key array is the array used to know when to swap values
output: the arrays will be sorted ascending according to the array designated as key.
processing: The bubble sort algorithm will be used to decide what values need to be exchanged and will
perform the same exchange for each array.
*/
void sortAscend(double key[], double e[], double f[], int sz)
{
   int last = sz-2;
   bool sorted = false;
   double temp;

   while(!sorted) //outer loop that runs until can go through entire array without needing to swap values
   {
       sorted = true;
       for(int j = 0; j <= last; j++) //loop to compare adjacent values
       {
           if (key[j] > key[j+1]) //test to see if out of order
           {

               sorted = false;
               // swap key values (could be done by another function)
               temp = key[j];
               key[j] = key[j+1];
               key[j+1] = temp;


               //perform same swap on second array
               temp = e[j];
               e[j] = e[j+1];
               e[j+1] = temp;

               //perform same swap on third array
               temp = f[j];
               f[j] = f[j+1];
               f[j+1] = temp;

           }//end if
       }//end inner loop
       last = last-1;
   }//end outer loop

}//close function


//add comments for your function and your function to sort descending - use selection or insertion sort

Solution

include <iostream>
#include <iomanip>
using namespace std;
void printArrays(const double [], const double [], const double [], int size);
void sortAscend(double [], double [], double [], int );
//add prototype for descending sort
int main()
{
double x[10] = {4.5, 9.8, 1.2, 10.9, 5.5, 6.2, 3.2, 10.5, 2.9, 5.4};
double y[10] = {11.6, 3.2, 5.9, 8.8, 1.3, 7.2, 4.3, 2.2, 15.3, 7.8};
double z[10] = {1.2, 6.7, 2.3, 14.8, 5.3, 9.2, 11.5, 4.6, 8.9, 6.1};

cout<<\"Original array\ x y z\"<<endl; //output of title line
//call function to output arrays
printArrays(x,y,z,10);

//call function to sort ascending by x
sortAscend(x, y, z, 10);
cout<<\"\ \ Array sorted ascending by x\ x y z\"<<endl; //output of title line
//call function to output arrays
printArrays(x,y,z,10);
//call function to sort ascending by z (note z is passed as key)
sortAscend(z, y, x, 10);
cout<<\"\ \ Array sorted ascending by z\ x y z\"<<endl; //output of title line
//call function to output arrays
printArrays(x,y,z,10);
//call function to sort descending by y
//call function to output arrays

//call function to sort descending by z
//call function to output arrays
return 0;
}
/* Function to output three 1-dimensional double arrays
input: three 1- dimensional double arrays and size of the arrays
output: The values in the arrays
processing: loop that runs from 0 to size of arrays - 1
*/
void printArrays(const double a[], const double b[], const double c[], int size)
{
//loop to output elements of the arrays
for(int i = 0; i < size; i++)
{
cout<<setprecision(1)<<fixed<<setw(5)<<a[i]<<setw(10)<<b[i]<<setw(10)<<c[i]<<endl;
}
}

Copy the template for program and review it. Note how the same function can sort parallel arrays using different arrays (keys) to sort by just by changing the o
Copy the template for program and review it. Note how the same function can sort parallel arrays using different arrays (keys) to sort by just by changing the o
Copy the template for program and review it. Note how the same function can sort parallel arrays using different arrays (keys) to sort by just by changing the o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site