0

I am creating a program that uses a while loop to provide multiple pieces of information from input given by the user. One of these pieces is the smallest number entered. I can't get it to print anything but 0. Any idea why?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float num, sum = 0, sm, lg = 0, count = 0, avg = 0;

    printf("Please enter a series of numbers (-1 to terminate): ");
    scanf("%f", &num);

    while(num > -1){
        sum += num;
        if(lg < num)
        lg = num;
        if(sm > num)
        sm = num;
        scanf("%f", &num);
        count++;
        avg = sum / count;
    }

    printf("The sum of your numbers is: %.4f\n", sum);
    printf("You entered %.4f numbers\n", count);
    printf("The average of the numbers you entered is: %.4f\n", avg);
    printf("The smallest number you entered is: %.4f\n", sm);
    printf("The Largest number you entered is: %.4f", lg);
    return 0;
}

ex entry- 15 43 22.5 57.6 -1 Output- sum:138.1000 4 numbers entered average: 34.5250 smallest: 0.0000 Largest: 57.6000

  • Does this answer your question? [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – Joseph Sible-Reinstate Monica May 04 '20 at 02:18
  • 1
    Note that your code won't handle the biggest number properly if the largest value entered is negative (all the values are negative, in other words) — but you would get a non-zero smallest number. You don't properly initialize the smallest number. It's best to use the first entry as the initial value for both smallest and largest — then everything will work. It's trivial when you first call `scanf()` outside the loop. Don't forget to check that `scanf()` was successful each time you call it. Alternatively, since you use `float`, use +∞ and -∞ to initialize the smallest and largest initializers. – Jonathan Leffler May 04 '20 at 02:49
  • There's not much point in calculating the average on each iteration; wait until the input is complete. – Jonathan Leffler May 04 '20 at 02:52

3 Answers3

2

In your code, sm has an undefined value because you aren't initializing it. You can initialize it to something like a very large number (or, even better, INFINITY) so it can be properly compared. The same goes for lg: if you want it to work for negative values too, you should initialize it with a very small value (-INFINITY). You can use INFINITY by including math.h.

Alejandro De Cicco
  • 1,216
  • 3
  • 17
1

the posted code does not compile!

first, because it is missing the needed #include statements for the needed header files. Specifically:

#include <stdio.h>  

Then regarding this code block:

if(sm > num)
    sm = num;

on the first pass through the while() loop, the variable sm is not initialized so accessing its' contents is undefined behavior.

Also, if using visual studio in debug mode, then all the stack is cleared to 0, so no other value will ever be assigned to it. This is why the algorithm always returns 0

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

Here's what I regard as a workable program. It uses double rather than float; if you insist, you can change the types and (input) formats to suit your desires. It has no particular limit on the number of rows it will accept. It doesn't output anything if there were no inputs. There is absolutely no need to use an array for the calculations. It uses use +∞ and -∞ to initialize the smallest (min) and largest (max) values respectively. Even if the only input is +∞ or -∞ (spelled +Inf or -Inf, or +Infinity or -Infinity, optionally without the + sign, and with upper-case, lower-case or mixed-case spelling) the correct values are produced.

#include <stdio.h>
#include <math.h>

int main(void)
{
    double min = +INFINITY;
    double max = -INFINITY;
    double sum = 0.0;
    size_t cnt = 0;
    double value;

    while (scanf("%lf", &value) == 1)
    {
        sum += value;
        cnt++;
        if (value > max)
            max = value;
        if (value < min)
            min = value;
    }

    if (cnt > 0)
    {
        printf("Count   = %zu\n", cnt);
        printf("Sum     = %g\n", sum);
        printf("Min     = %g\n", min);
        printf("Max     = %g\n", max);
        printf("Average = %g\n", sum / cnt);
    }

    return 0;
}

Given ten random values between -1E6 and +1E6:

 989375.672
-826955.668
 224850.463
-401605.702
 -45457.787
 259618.099
 821069.496
-268408.724
-512449.113
 -46404.246

the program produces the output:

Count   = 10
Sum     = 193632
Min     = -826956
Max     = 989376
Average = 19363.2

I should probably put a bit more control on the output formatting, but the %g option is quite useful for numbers with wide ranges. Given the input data, using %11.3f would work well (it was used to format the output from the random number generator I used).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278