Home > Software engineering >  Why is this program giving the result 0 or garbage value although all the conditions have been met?
Why is this program giving the result 0 or garbage value although all the conditions have been met?

Time:10-20

Why is this program giving the result 0 or garbage value although all the conditions have been met ?

As i read it is okay to write the if...else without the else clause.

// Program to find the largest of three given numbers

#include <stdio.h>

int main()
{

int a, b, c, big;

printf("Enter three given numbers : ");

scanf("%d%d%d", &a, &b, &c);

// if (1>2) (1>3) (2>3)
 
 

if (a > b) //  1>2

{    

    if (a > c)

        big = a;

    else

        big = c;
}
  printf("The largest numnber is = %d",big);

return 0;

}

CodePudding user response:

To make it more clear this if statement

if (a > b) //  1>2

{    

    if (a > c)

        big = a;

    else

        big = c;
}

may be rewritten like

if (a > b) //  1>2
{    
   //.. unimportant
}

provided that a is not greater than b. That is in this case the sub-statement of the if statement that represents the compound statement will not get the control. And as a result the variable big will not be initialized.

Your program could look the following way

#include <stdio.h>

int main( void )
{
    int a = 0, b = 0, c = 0, big;

    printf( "Enter three numbers: " );

    scanf( "%d %d %d", &a, &b, &c );

    if ( !( a < b ) && !( a < c ) )
    {
        big = a;
    }
    else if ( !( b < c ) )
    {
        big = b;
    }
    else
    {
        big = c;
    } 
 
    printf( "The largest number is = %d\n", big );

    return 0;
}

Pay attention to that the user can enter for example three numbers equal each other.

CodePudding user response:

You are missing the branch where this is false:

if (a > b)

big will then be left uninitialized and have an indeterminable value.

To catch all combinations using only two comparisons, you could do like this:

    if (a > b) big = a;
    else       big = b;

    if(c > big) big = c;

A more general form:

   int arr[...] = {filled with values};
   big = arr[0];
   for(size_t i = 1; i < sizeof arr / sizeof *arr;   i) {
       if(arr[i] > big) big = arr[i];
   }

CodePudding user response:

My two cents for the simplest solution:

#include <stdio.h>

int main()
{
    int big, b, c; // only need 3 variables
    printf("Enter three given numbers : ");
    // should check this return value
    scanf_s("%d%d%d", &big, &b, &c);  // assume first value entered is largest

    if (b > big) big = b;  // b larger? reassign
    if (c > big) big = c;  // c larger? reassign

    // big is now the largest number entered
    printf("biggest number is %d\n", big);

    return 0;
}

CodePudding user response:

Logically your programm is not correct. After if you should use else.

In your programm, when you input numbers {1,2,3}, big is never initialized. However if you input {3,2,1} it will output correct result.

#include <stdio.h>

int main()
{
    int a, b, c, big;
    printf("Enter three given numbers : ");
    scanf_s("%d%d%d", &a, &b, &c);
    if (a > b)
    {
        if (a > c)
            big = a;
        else
            big = c;
        printf("The largest numnber is = %d", big);
    }
    else
        printf("The largest numnber is not detected");
    return 0;
}
  • Related