Chrille Chrille - 1 year ago 61
C Question

Finding max and min values in a series of numbers In C prgram (error in my code)

This may prove to be a really easy task but I can't seem to grasp what I've done wrong. I've looked for the answer that specifies exactly this execution but can't get it right. My program is supposed to only run 10 times only but it keeps looping until i close the application window. I am new to this so...

#include < stdio.h >

int main()

{


int n, sum = 0, c, value;
n = 10;
int max = 0;
int min = 0;



printf("Write 10 numbers\n");

for (c = 1; c <= n; c++)
{
scanf_s("%d", &value);


if (c = 1)
{
max = value;
min = value;


}
else if (value < max, c <= n)
{
max = value;
}

else if (value < min, c <= n)
{
min = value;
}
}

printf("Biggest number is : %d\n", max);
printf("Smallest number is : %d\n", min);



return 0;
}

Answer Source

Here you are using assignment operator = instead of comnparison operator ==

if (c = 1)

The loop for finding minimum and maximum can be written simpler

for ( c = 1; c <= n; c++ )
{
    scanf_s( "%d", &value );

    if ( c == 1 )
    {
        max = value;
        min = value;
    }
    else if ( max < value )
    {
        max = value;
    }
    else if ( value < min )
    {
        min = value;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download