The idea here is a program that uses a sentinel loop and some comparison inside
ID: 3671334 • 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 al 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 o 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 00 to quit:0-47 Ordered these numbers are -47 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 Notes That if the numbers are entered with the larger number first they are "ordered" by being displayed in the reverse order That the sentinel value is 0 0, not just one zero. That a sentinel loop works by using a priming read and a read at the bottom. That when the median value would be normally something like 2.5, it is rounded up rather than down. (Which is what integer division naturally does.) This one is a bit tricky to figure out, and it involves something we know about %2 that the overall range is calculated as the difference between the largest number entered the smallest number entered throughout the run of the programExplanation / 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.