Could someone take a look at my code and give me some pointers why it isn't working properly. It is supposed to ask for numbers as long as they're positive integers and then calculate sum of them, divide it by largest and multiply it by smallest number.
#include <stdio.h>
int main () {
int n, largest=0, smallest=0;
float sum=0;
scanf("%d", &n);
while (n > 0) {
scanf("%d", &n);
if (n > largest) {
largest = n;
}
if (n < smallest) {
smallest = n;
}
sum += n;
}
sum = sum / largest * smallest;
printf("%f\n", sum);
return 0;
}
Because smallest
starts at zero, it will never change because if (n < smallest)
will never be true. You need:
int smallest = INT_MAX;
or similar. For INT_MAX
you'll need:
#include <limits.h>
at the top.
Move scanf to end of while or else your first input is missed and secondly, you can accept a negative input and do all the processing on it. Also, smallest has to be the Max value otherwise with smallest = 0
, n < smallest
will not work
#include <stdio.h>
int main () {
int n, largest=0, smallest=INT_MAX;
float sum=0;
scanf("%d", &n);
while (n > 0) {
if (n > largest) {
largest = n;
}
if (n < smallest) {
smallest = n;
}
sum += n;
scanf("%d", &n);
}
sum = sum / largest * smallest;
printf("%f\n", sum);
return 0;
}
Change your while
loop condition from while (n > 0)
to while (scanf("%d", &n) && n > 0)
. In this case you can also remove the first scanf("%d", &n)
and the one in the body of the while
loop.
You problem is that because your scanf("%d", &n)
is in the body of your while loop, it is not checked for being greater then 0
when performing if (n > largest)
and if (n < smallest)
.
Also you should initialize your smallest
to the max possible value. In this case being INT_MAX
.
Your code fails primarily due to int smallest=0
. This can be solve a number of ways. I recommend setting the first value read to largest
and smallest
. Also your 2nd scanf("%d", &n);
s/b at the end of the loop.
You have additional problems, though subtle.
Summing int
into a float
can lose precision in many environments when int
values > about 24,000,000. Suggest a large integer type to sum your values like uint64_t.
The final "divide it by largest and multiply it by smallest number" it the only place floating point math is needed. The math you did was OK but more likely subject to rounding errors.
If your first n
<= 0, you will perform a divide by 0.
Sample fix:
#include <stdio.h>
int main() {
int n, largest = 1, smallest = 0; // set largest to 1 to deal with first n being < 0 and thus avoiding /0
uint64_t sum = 0;
scanf("%d", &n);
while (n > 0) {
if (sum > 0) {
if (n > largest) {
largest = n;
}
if (n < smallest) {
smallest = n;
}
} else { // This is the first time as sum is 0
largest = smallest = n;
}
sum += n;
scanf("%d", &n);
}
printf("%lf\n", ((double) sum) / smallest * largest);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.