Question

In: Computer Science

Q3) ​Write a main function to test your SortTriple will ask the user to enter the...

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

Solutions

Expert Solution

#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;
}


Related Solutions

Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
ARDUINO Create a function that will ask the user to enter the values for each of...
ARDUINO Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
Your Application should ask the user to enter their name and their salary.
Create a C# Console Application, name the solution Homework 6 and the project TaxRate.Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric...
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
In your python program, ask the user to enter the annual income of an employee and...
In your python program, ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee’s years of experience is greater than 4, if so the employee gets $6000 loan; otherwise,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT