Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that accepts a variable number of input values and returns the

ID: 3822563 • Letter: W

Question

Write a function that accepts a variable number of input values and returns the sorted values as an array. The function will be called, for example, like this:

b = msort(2,1,4,3)

and will return a vector b=[1 2 3 4] (either row or column vector is OK)

The function should have the following features:

- (10 pts) Help text, including H1 (first line)

- (30 pts) The function should sort in ascending order, i.e., from lowest to highest.

- (20 pts) Optionally include a string parameter ‘d’ or ‘D’ to change the sort order to descending, i.e., highest to lowest. It should be placed as the last argument to the function, i.e., b=msort(2,1,4,3,’d’) returns [4 3 2 1].

- (20 pts) Have an optional second return argument that includes the number of swaps made during the sort process. If no sorting is done, the number of swaps will be zero. The function is then called as [b,n] = msort(2,1,4,3); here b is assigned the sorted value as above, and n is the number of swaps that occurred during the sorting process.

- (10 pts) Should work for only a single input argument, i.e., msort(3) returns 3, rather than generating an error.

- (10 pts) Issue an error if anything other than a number or ‘d’ or ‘D’ is sent as an argument.

Explanation / Answer


#include <iostream>
#include <vector>
#include <stdio.h>    
#include <stdarg.h>
#include<stdlib.h>
using namespace std;

struct result
{
vector<int> b; //vector
int n; //no of swap
};

struct result ArraySort(int size,...)
{
struct result r;
r.n=0; //initialize no of swap to zero

int n=size-1;
int i,j,val,a;
va_list vl;
va_start(vl,size);

for (i=1;i<size+1;i++)
{
    val=va_arg(vl,int);
    if(i!=size)
     r.b.push_back(val);
}

//sorting in decreasing order
if(val==68 || val==100)
{
for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (r.b[i] < r.b[j])
            {
                a = r.b[i];
                r.b[i] = r.b[j];
                r.b[j] = a;
                r.n++;
            }
        }
    }

}
else if(val==97 || val==65) //sorting in increasing order
{
for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (r.b[i] > r.b[j])
            {
                a = r.b[i];
                r.b[i] = r.b[j];
                r.b[j] = a;
                r.n++;
            }
        }
    }

}
else
{
    cout<<"Enter valid option 'd' or 'D' for sorting in decreasing order or 'a' or 'A' for sorting in increasing order ";
    exit(0);
}

va_end(vl);

return r;
}

int main()
{

struct result res;

//first parameter is the size of the no of arguments
res=ArraySort(5,2,1,4,3,'d');

cout<<"no of swaps: "<<res.n<<endl;
cout<<"The sorted array is : ";
for(int i=0;i<res.b.size();i++)
cout<<res.b[i]<<" ";
cout<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote