Using C Add a function to sort the array arrays in a descend

Using C++ 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.

#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

Following is the required code :

#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
void sortDescend(double[], double[], double[], int );

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
sortDescend(y,x,z, 10);
//call function to output arrays
cout<<\"\ \ Array sorted descending by y\    x         y         z\"<<endl; //output of title line
printArrays(x,y,z,10);
//call function to sort descending by z
sortDescend(z,x,y,10);
//call function to output arrays
cout<<\"\ \ Array sorted descending by z\    x         y         z\"<<endl; //output of title line
printArrays(x,y,z,10);
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

void sortDescend(double key[], double e[], double f[], int sz)
{
//using selection sort
for( int i = 0; i < sz-1; i++)
{
    //find maximum element on right of i
    int maximum_at = i;
    for(int j = i+1; j < sz; j++ ){ //loop to find maximum element on right of i
        if( key[j] > key[ maximum_at ] ){ //check to see if, its value is less than min so far
            maximum_at = j;
        }
    }
    //now need to swap this maximum elemeent and element at i
    double temp = key[maximum_at];
    key[maximum_at] = key[i];
    key[i] = temp;
    //do same for e and f arrays too
    temp = e[maximum_at];
    e[maximum_at] = e[i];
    e[i] = temp;

    temp = f[maximum_at];
    f[maximum_at] = f[i];
    f[i] = temp;
}
}

Using C++ 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 C++ 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 C++ 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 C++ 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site