This is a C++ question: Create the Two functions described below: Sort1 – takes
ID: 3688439 • Letter: T
Question
This is a C++ question:
Create the Two functions described below:
Sort1 – takes 3 parameters an array of integers , the number of
Elements in the array to sort and a parameter called
reversed that defaults to false.
The function sorts the array elements smallest to largest
If reversed is false , largest to smallest if true.
The function changes the array passed to it and returns
nothing.
Sort2 – takes 3 parameters an array of integers , the number of
Elements in the array to sort and a parameter called
reversed that defaults to false.
The function creates a sorted array with elements smallest to
largest, If reversed is false , largest to smallest if true.
The function returns the sorted array that was created.
Ensure this function does not change the original array.
Create a main() function to test the two functions completely.
Don’t forget to include the appropriate documentation for each function.
Use the flagged bubble sort algorithm to sort the arrays.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
void sort1(int a[], int n, char reversed[])
{
int i,j,c;
if(strcmp(reversed,"false")==0)
{
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[i])
{
c=a[j];
a[j]=a[i];
a[i]=c;
}
}
}
}
else
{
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]<a[i])
{
c=a[j];
a[j]=a[i];
a[i]=c;
}
}
}
}
printf(" Sorted Arrary is :-");
for(i=0;i<5;i++)
{
printf(" %d",a[i]);
}
getch();
}
int * sort2(int a[], int n, char reversed[])
{
int i,j,c;
if(strcmp(reversed,"false")==0)
{
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[i])
{
c=a[j];
a[j]=a[i];
a[i]=c;
}
}
}
}
else
{
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
if(a[j]<a[i])
{
c=a[j];
a[j]=a[i];
a[i]=c;
}
}
}
}
return a;
}
void main()
{
int a[5],i,*b;
clrscr();
printf(" Enter 5 values");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
sort1(a,5,"false");
b=sort2(a,5,"true");
for(i=0;i<5;i++)
{
printf(" %d",b[i]);
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.