Question

Problem 5 (10 points) Write a C program, called sort3.c, that performs the following task. The program first prompts the user to enter three integers a, b, and c. It then sorts these integers in ascending order, so that swapping their values if necessary. Finally, the program prints the three integers entered by the user in ascending order. Here are a few sample runs of the program /home/userXYZ/ECE15/Final> sort3 Please enter three integers: 3 2 1 Here are the integers in ascending order: 1 2 3 /home/userXYZ/ECE15/Final> sort3 Please enter three integers: 42 -18 0 Here are the integers in ascending order: -18 0 42 /home/userXYZ/ECE15/Final> sort3 Please enter three integers: 1 2 3 Here are the integers in ascending order: 1 2 3 /home/userXYZ/ECE15/Final> sort3 Please enter three integers: 2 1 2 Here are the integers in ascending order: 1 2 2 In this problem, you do not need to verify the validity of the user input. You should assume that the user indeed enters three integers when prompted to do so There are many different ways to sort three integers a, b, c so that a b c. Here is one simple way that works. Perform the following sequence of three conditional swaps, in the specified order if a >b, swap a and b; if b >c, swap b and c if a > b, swap a and b; You can use the above method or any other method of your choice, but you are required to call the function sort-three () in order to sort a, b, and c. Moreover, the sorting has to be accomplished in place. In particular, you are not allowed to declare and use any variables inside the sort three( function. This function, however, can call other functions (such as the swap () function) that you will need to implement separately. Note that you are not allowed to use any functions from the C standard library, except for those declared in <stdio.h>.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI,
here is the full code with comments

#include <stdio.h>
void swap(int *xp, int *yp) //function to swap values
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort_three(int* a,int* b,int* c)//function to sort three integers
{
if (*a > *c)//if a>c swap
swap(a, c);

if (*a > *b)//if a>b swap
swap(a, b);

if (*b > *c)//at this point the smallest is at last, so swap
swap(b, c);
}
int main()
{
int a,b,c;
printf("Please enter three integers\n");
scanf("%d%d%d",&a,&b,&c);
sort_three(&a,&b,&c);//calling the function
printf("In ascending order:");
printf("%d %d %d",a,b,c);
return 0;
}
o/p-

Thumbs up if this was helpful, otherwise let me know in comments

Add a comment
Know the answer?
Add Answer to:
Problem 5 (10 points) Write a C program, called sort3.c, that performs the following task. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • C++ this program will get two integers from the user. The program will 1. call getProduct...

    C++ this program will get two integers from the user. The program will 1. call getProduct (int, int, int &); where the first two arguments are the two input integers and third one is the product of these 2 integers. 2. call printEvenAscending (int, int); where the two arguments are the two input integers. This function will print all -even numbers between the two arguments in an ascending order. Note, the two input integers could be in any order (such...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a...

    C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a three function program (your main function and two other functions). The main function of the program should create an array that can store 10 integers. The main function will then pass the array to a second function that gets the integers from the user and stores them in the array. Then your main will call a third function that displays the elements in the...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least...

    HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT