Question below: Write a function called \'sort _ integers\' that takes three int
ID: 3533566 • Letter: Q
Question
Question below:
Write a function called 'sort _ integers' that takes three integers (passed by reference) and sorts them in ascending order. The 'sort_integers' function doesn't return anything. Therefore, this should be the header of the function. void sort_integers(int *a, int *b, int *c) { ... } Also, write a 'main' program that takes three integers from the user, using 'scanf, and passes them to csort_integers' for sorting. Print the three integers in the 'main' function before passing them to the function and after passing them to the function to make sure they've been sorted. This is an algorithm that you can use to sort three integers. Compare the 1st and the 2nd, if the 2nd is smaller, swap the 1st and the 2nd. Then, compare the 1st and the 3rd, if the 3rd is smaller, swap the 1st and the 3rd. At this point, the smallest is in the l2* variable. Now, compare the 2nd and the 3rd, if the 3r is smaller, swap the 2nd and the 3rd. Now, the three integers are sorted. Include samples with your solution. Below is a sample output.Explanation / Answer
#include <stdio.h>
void sort_integers(int *a,int *b,int *c)
{
int tmp;
if( *a > *b )
{ tmp=*a;
*a=*b;
*b=tmp;
}
if( *a > *c)
{
tmp=*a;
*a=*c;
*c=tmp;
}
if( *b > *c)
{
tmp=*b;
*b=*c;
*c=tmp;
}
}
int main(int argc, char *argv[])
{
printf("Enter 3 integers: ");
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("Before sorting... ");
printf("a=%d, b=%d, c=%d ",a,b,c);
sort_integers(&a,&b,&c);
printf("After sorting... ");
printf("a=%d, b=%d, c=%d ",a,b,c);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.