The idea here is a program that uses a sentinel loop and some comparison inside
ID: 3671814 • Letter: T
Question
The idea here is a program that uses a sentinel loop and some comparison inside the loop for some simple number processing. You are to create a program that has this interaction - it uses all integer values: Enter your two numbers or 0 0 to quit: 18 5 Ordered these numbers are 5 18 The median is 12. Enter your two numbers or 0 0 to quit: 26 29 Ordered these numbers are 26 29 The median is 28. Enter your two numbers or 0 0 to quit: 0 489 Ordered these numbers are 0 489 The median is 245. Enter your two numbers or 0 0 to quit: 65 0 Ordered these numbers are 0 65 The median is 33. Enter your two numbers or 0 0 to quit: 0 -47 Ordered these numbers are -47 0 The median is -22. Enter your two numbers or 0 0 to quit: 0 0 You had 5 pairs of numbers. The overall range of your numbers was 536.Explanation / Answer
//median of 0 and -47 should be -23
#include <stdio.h>
int main()
{
int a,b,diff,temp,median,pairs=0;
int min=1<<30,max=-min;
while(1)
{
printf("Enter your two numbers or 0 0 to quit: ");
scanf("%d %d", &a, &b);
if(a==0&&b==0)
{
printf("You had %d pairs of numbers ", pairs);
printf("The overall range of your number was %d ", max-min);
return 1;
}
else
{
pairs++;
if(a>b)
{
temp = a;
a = b;
b = temp;
}
if(a<min)
min = a;
if(b>max)
max = b;
printf("Ordered these numbers are %d %d", a, b);
diff = b-a;
if(diff%2==1)
median = a + diff/2 + 1;
else
median = a + diff/2;
printf(" The median is %d ", median);
}
}
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.