简体   繁体   中英

Why %d is showing 0 in my c program output?

The program is to find the largest number amongst all the entered integers. It asks the user to enter a number 10 times or press 0 to quit, whichever is earlier. But, the output is not as expected. I will appreciate it if you can help a newbie.

#include <stdio.h>

int main()
{
    int num[10];
    int max, a;
    for (a = 0; a < 10; a++)
    {
        printf("Enter the integer: ");
        scanf("%d", &num[a]);
        if (num[a] == 0)
            break;
    }

    for(a = 0; a < 10; a++)
    { 
        num[0] = max;
        if(max < num[a])
        {
            num[a] = max;
        }
    }

    printf("This is the largest integer: %d", max); //The output is coming wrong here.

    return 0;
}
  1. Don't use num[0], you are overwriting it with max variable which is not initialized, so it is 0.
  2. Use max to the minimum type variable (INT_MIN with limits.h header file) or initalize it to max = num[0] after input is captured.
#include <limits.h>

  int max = INT_MIN;

Also you need to change your second for loop as follows so as to update the max variable as you iterate, and not the num[] variables. Loop variable starts with 1 if you already assumed max to be first element before, else loop variable will start with 0.

  for(a = 1; a < 10; a++) // a=0 if max was initialized to INT_MIN above
  { 
    if(num[a]>max)
    {
     max = num[a];
    }
  }

You never assign the max variable.

What you want to do is to check if the value entered is greater than each one you've previously entered , so you would need to use the following condition:

if (num[a] > max)
    max = num[a];

You also need to initialize max to some value (let's say, if you expect to have only positive integers, it could be 0 , but have a look at Jigsaw answer for a better solution): int max = 0; .
And eventually add an if-condition that checks if max is 0 , that way you know if no values have been entered:

if(max == 0)
    printf("No values have been entered.");

else printf("This is the largest integer: %d", max);

Notice that you can assign the elements of num and update max in the same for loop, therefore the second for becomes completely useless and you can remove it:

#include <stdio.h>

int main()
{
    int num[10];
    int max = 0, a;
    for (a = 0; a < 10; a++)
    {
        printf("Enter the integer: ");
        scanf("%d", &num[a]);
        if (num[a] == 0)
            break;
        if (num[a] > max)
            max = num[a];
    }

    if(max == 0)
        printf("No values have been entered.");
    else printf("This is the largest integer: %d", max);

    return 0;
}

I suggest you to turn on your compilers warning, especially -Wall and -Wextra , so you would notice problems like these:

<source>: In function 'main':
<source>:17:16: warning: 'max' may be used uninitialized [-Wmaybe-uninitialized]
   17 |         num[0] = max;
      |         ~~~~~~~^~~~~
<source>:6:9: note: 'max' was declared here
    6 |     int max, a;
      |         ^~~

You neither initialise nor ever write any value at all to the variable max . Using uninitialised variables is undefined behaviour.

You seem to know how to write 0 into a . But for max you seem to have it reversed. Remember that with = the value on the right of it goes into the variable on the left of it.

To fix the problem, turn any something = max; which occurs in your code into max = something; .

For starters this for loop

for (a = 0; a < 10; a++)
{
    printf("Enter the integer: ");
    scanf("%d", &num[a]);
if (num[a] == 0)
    break;
}

can enter less than 10 elements in the array num due to the condition

if (num[a] == 0)
    break;

So the next loop has to traverse exactly a elements of the array not 10 . So in the next loop you have to use another variable for the index as for example

for( int i = 0; i < a; i++)

The variable max was not initialized

int max, a;

So this for loop invokes undefined behavior where the variable max is assigned to elements of the array or where it is compared with elements of the array.

for(a = 0; a < 10; a++)
{ 
  num[0] = max;
  if(max < num[a])
  {
   num[a] = max;
  }
}

Moreover the variable max is not changed within the for loop. So the loop in whole does not make sense.

Pay attention to that the user can enter 0 at once. So the array will not contain any valid values. In this case the array does not have a maximum value.

Your program can look for example the following way

#include <stdio.h>

int main( void )
{
    enum { N = 10 };
    int num[N];

    int i = 0;

    do
    {
        printf( "Enter the integer (0 - stop): " );
        scanf( "%d", &num[i] );
    } while ( num[i] != 0 && ++i < N );

    int max_i = 0;

    for ( int j = 1; j < i; j++ )
    { 
        if ( num[max_i] < num[j] )
        {
            max_i = j;
        }
    }
  
    if ( i == 0 )
    {
        puts( "You have not entered numbers unequal to zero." );
    }
    else
    {
        printf("This is the largest integer: %d", num[max_i] );
    }

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM