Given 4 integers, find the smallest. --try to minimize the number of comparisons
ID: 3642129 • Letter: G
Question
Given 4 integers, find the smallest. --try to minimize the number of comparisons, and not repeat comparison. Hint: Use an extra variable to store partial results#include <stdio.h>
int main(void){
int a, b, c, d;
printf("Please enter 4 numbers separated by spaces > ");
scanf("%d%d%d%d",&a,&b,&c,&d");
if(a>b)
a=b;
if(a>c)
a=c;
if(a>d)
a=d;
printf("%d is the smallest",a);
return(0);
}
What's wrong with the codes?
Thanks in advance.
Explanation / Answer
This works. #include int main(void){ int a, b, c, d; printf("Please enter 4 numbers separated by spaces > "); scanf("%d%d%d%d",&a,&b,&c,&d); if(a>b) a=b; if(a>c) a=c; if(a>d) a=d; printf("%d is the smallest ",a); 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.