NolanD
NolanD

Reputation: 5

Issues with min and max of array in C

I am make a program that will calculate the average of numbers inputted into an array also print the min and max numbers entered into the array and then calculate the Standard deviation.

I think I have the average and the Standard deviation part working correctly but when running some final test I noticed that when entering my numbers in the array any number above 3 will not display in the Minimum number print.

So for example I could enter 11,12,13,14,15,16,17,18,19,20 and I would get 20 for the max and 3 for the min.

Any idea on why I am getting this. I am guessing something is limiting that variable but I can't see what.

Sorry if this is a stupid question.

Thanks,

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

int main(void)
{
    //variables
    float num[100];
    float average=0;
    float var=0;
    float stand=0;
    float sum=0; 
    float sum1=0;
    int i=0;
    int n=10;
    int min;
    int max;

    //ask user to enter numbers
    printf("Enter 10 numbers\n",n);
    for(i=0; i<n; i++)
    {
        scanf("%f", &num[i]);
    }

    //find average of numbers
    for(i=0; i<n; i++)
    {
        sum = sum + num[i];
    }
    average = sum /(float) n;

    for(i=0; i<n; i++)
    {
        //if greater than max
        if(num[i]>max)
        {
            max = num[i];
        }
    }

    //if smaller than min
    if(num[i]<min)
    {
        min = num[i];
    }

    //calculate standard deviation
    for(i=0; i<n; i++)
    {
        sum1 = sum1 + pow((num[i] - average),2);
    }

    stand = sqrt(sum1/n);

    //print results      
    printf("Average of all numbers  = %.2f\n", average);
    printf("Maximum number          = %d\n", max);
    printf("Minimum number          = %d\n", min);
    printf("Standard deviation      = %.2f\n", stand);

    system("PAUSE");
    return 1;
}

Upvotes: 0

Views: 435

Answers (3)

mdave16
mdave16

Reputation: 186

You aren't assigning values to your variables (which in other languages instantiates to 0 usually). In languages like C, it's sorta assigned from RAM. The way I think about it, is you have been given some space in a room, but you don't know where, or what's in it at the moment. Just some space and you know that the amount of space is correct!. It's not completely true, but as a beginner it's a good grasp to what's happening. (It was for me at least)

Mentally go through the code, if min was set to 3 at the beginning of the code, and you'd see it's logically correct.

Two possible things you could do are:

min = inf
max = -inf

another solution would be to use a value you already know

min = num[0]
max = num[0]

Upvotes: 1

Prakash
Prakash

Reputation: 110

After initialization

i.e max = min = num[0];

you need to do small change i.e Add if smaller than min logic in for loop

example: instead of this code

for(i=0; i<n; i++)
{
    //if greater than max
    if(num[i]>max)
    {
        max = num[i];
    }
}

//if smaller than min
if(num[i]<min)
{
    min = num[i];
}

Use this code

for(i=0; i<n; i++)
{
    //if greater than max
    if(num[i]>max)
    {
        max = num[i];
    }

    //if smaller than min
    if(num[i]<min)
    {
        min = num[i];
    }
}

Now you will get min and max values in array.

Upvotes: 1

Ash
Ash

Reputation: 6035

This code shouldn't compile. Considering it's running for you I'd guess you were initially initializing them to values lower or larger than the values in the array.

Based on what @samgak said in the comment:

Change this part of the code to look like this:

printf("Enter 10 numbers\n",n);
for(i=0; i<n; i++)
{
     scanf("%f", &num[i]);
}
max = min = num[0];   //ADD THIS LINE TO INITIALIZE YOUR MAX AND MIN

Upvotes: 2

Related Questions