In: Computer Science
Q3) Write a main function to test your SortTriple will ask the user to enter the three values, then uses SortTriple to reorder the values if required. The main function should print the value in the correct order and a message to indicate if the values were in the correct order or not. [20 points]
● You are NOT allowed to use loops or arrays in your code [- 30 points]
● You are NOT allowed to use global variables.
● You MAY create any number of additional functions if you think there is a need to. Creating
unnecessary additional function will be penalized
● You can ONLY use scanf() and printf() functions in the main() function only
and please explain what you do as you write it and why
#include <stdio.h>
void SortTriple(int *a1,int *b1,int *c1,int *correctorder)
{
/*we use a temporary variable to help us sorting the 3 numbers
*/
int temp=0;
if((*a1>=*b1)&&(*a1>=*c1)) /*check whether a>b and
also a>c, if so a is the greatest no.*/
{
if(*b1>=*c1) /*check if b>c, if so b is 2nd greatest and c is
lowest no.*/
{
*correctorder=0; /*since reordering is done keep variable
correctorder 0 itself*/
temp=*a1;
*a1=*c1;
*c1=temp;
}
else /* if b is not greater than c, then b is lowest and c is 2nd
greatest no*/
{
*correctorder=0;
temp=*a1;
*a1=*b1;
*b1=*c1;
*c1=temp;
}
}
else if((*b1>=*a1)&&(*b1>=*c1)) /*check if b is
greater than a and c, if so, b is greated of the 3 numbers*/
{
if(*a1>=*c1) /*now check if a>c, then order is c,a,b*/
{
*correctorder=0;
temp=*a1;
*a1=*c1;
*c1=*b1;
*b1=temp;
}
else /*else we know c>a, hence order will be a,c,b*/
{
*correctorder=0;
temp=*b1;
*b1=*c1;
*c1=temp;
}
}
else if((*c1>=*a1)&&(*c1>=*b1)) /*finally we check if
c is greatest of the other two*/
{
if(*a1>=*b1) /*if c is greatest and a>b, order will be
b,a,c*/
{
*correctorder=0;
temp=*a1;
*a1=*b1;
*b1=temp;
}
else /*if c is greatest and a<b, a is the smallest, b is 2nd
largest. Hence the given numbers are in the correct ascending order
itself. so, make correctorder variable as 1. check this variables
value as 1 in main function, to print that the entered values are
in correct order.*/
{
*correctorder=1;
*a1=*a1;
*b1=*b1;
*c1=*c1;
}
}
}
int main(){
/*we are going to use 3 variables a,b,c to denote entered
values*/
/*also another variable correctorder to check whether the entered
numbers are in correct order, hence initially we assign 0 assuming
that its not in correct order*/
/*we also assume that the correct order is ascending order*/
int a,b,c,correctorder=0;
printf("This program sorts 3 numbers\n\n");
printf("Enter the first number: ");
scanf("%d",&a);
printf("\nEnter the second number: ");
scanf("%d",&b);
printf("\nEnter the third number: ");
scanf("%d",&c);
/*we are going to call the SortTriple function by passing all the
parameters by reference. pass by reference is used here because we
are allowed to print the sorted list only in main function. Pass by
reference helps the values changes in variables to get updated in
its address. so, we pass the values by reference and sort it in the
SortTriple function. Finally printing the variables in main funtion
itself.*/
SortTriple(&a,&b,&c,&correctorder);
printf("\n\nsorted list is: %d\t%d\t%d",a,b,c);
if(correctorder==1)
{
printf("\n\nThe entered values were in correct order");
}
else
{
printf("\n\nThe entered values were not in correct order");
}
return 0;
}