2

I would like to get an output of the biggest even number. but when I input 1 2 3 (3 calls to scanf) the output is 4.

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

int main() {
    int ary[100];
    int x, y = 0;
    int amount;
    scanf("%d", &amount);
    fflush(stdin);
    for (x = 1; x <= amount; x++) {
        scanf("%d", &ary[x]);
        if (ary[x] % 2 == 0) {
            if (ary[0] < ary[x]) {
                ary[0] = ary[x];
            }
        }
    }
    printf("%d", ary[0]);

    getchar();
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Suraenhom
  • 21
  • 1

3 Answers3

2

Before the loop initialize ary[0] for example the following way (otherwise uninitialized value of ary[0] is used in the program)

ary[0] = 1;

then substitute these if statements

    if(ary[x]%2==0)
    {
        if(ary[0]<ary[x])

for

if( ary[x]%2==0 && ( x == 1 || ary[0]<ary[x] ) )

And at last write

if ( ary[0] != 1 ) printf("%d",ary[0]);

Take into account that this call

fflush(stdin);

has undefined behavior and should be removed.

In fact there is no need to declare an array. Without the array the program can look like

#include <stdio.h>

int main( void )
{
    unsigned int n;
    int max_even = 1;

    printf("How many numbers are you going to enter: ");
    scanf("%u", &n);

    int x;

    for (unsigned int i = 0; i < n && scanf( "%d", &x ) == 1; i++)
    {
        if ((x % 2) == 0 && (max_even == 1 || max_even < x))
        {
            max_even = x;
        }
    }

    if (max_even != 1)
    {
        printf("maximum entered even number is %d\n", max_even);
    }
    else
    {
        puts("None even number was enetered");
    }

    return 0;
}

Its output might look like

How many numbers are you going to enter: 10
0 1 2 3 4 5 6 7 8 9
maximum entered even number is 8
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Your code does not work because ary[0] is not yet initialized the first time you compare its value to the value read, furthermore it might not be even for the other comparisons.

You should use an indicator telling you whether an even value has been seen.

Here is a solution:

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

int main(void) {
    int has_even = 0, max_even = 0, value, amount, x;

    if (scanf("%d", &amount) != 1)
        return 1;
    for (x = 0; x < amount; x++) {
        if (scanf("%d", &value) != 1)
            break;
        if (!has_even || value > max) {
            max_even = value;
            has_even = 1;
        }
    }
    if (has_even)
        printf("%d\n", max_even);
    else
        printf("no even value\n");

    getchar();
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0
#include <stdio.h>
#include <stdlib.h>

int main() {
 int ary[100];
 int ary[0 = 0;
 int x, y = 0;
 int amount;
 scanf("%d", &amount);
 fflush(stdin);
 for (x = 1; x <= amount; x++) {
    scanf("%d", &ary[x]);
    if (ary[x] % 2 == 0) {
        if (ary[0] < ary[x]) {
            ary[0] = ary[x];
        }
    }
}
printf("%d", ary[0]);

getchar();
return 0;

}