Input: 3 real-valued numbers Output: Sort them in non-decreasing order and outpu
ID: 3590746 • Letter: I
Question
Input: 3 real-valued numbers Output: Sort them in non-decreasing order and output them to “stdout” Note: You MUST follow the template described below
void sort(float* a, float* b, float* c)
{ …. }
int main()
{ float a, b, c;
//read 3 real-valued numbers from “stdin” into a, b, c
sort(…);
//print a, b, c }
Sample input/output: + Input: a line of 3 real-valued number, separated by a comma (no whitespace at all) + Output: the 3 numbers sorted in non-decreasing order, separated by a comma. Each number is printed with 2 digits after decimal point.
Explanation / Answer
#include<stdio.h>
void sort(float* a, float* b, float* c)
{
int temp;
// least among a and c
if(*a > *b)
{
temp = *a;
*a = *b;
*b = temp;
}
// least among a and c. Now a has least value
if(*a > *c)
{
temp = *a;
*a = *c;
*c = temp;
}
// now for least among b and c
if(*b > *c)
{
temp = *b;
*b = *c;
*c = temp;
}
}
int main()
{
// driver for above method
// taking user input, calling function and printing values in non-decreasing order
float a, b, c;
scanf("%f,%f,%f", &a, &b, &c);
sort(&a,&b,&c);
printf("%f, %f, %f", a, b, c);
}
/*SAMPLE OUTPUT
34.5,23.56,67.45
23.559999, 34.000000, 67.449997
*/