2
int  i,max,min;
int  A[11];
    min = A[1];
    max = A[1];
    for(i=1;i<=10;i++)
    {
        if(min > A[i] && A[i]%2 ==0 )  min = A[i];
        if(max < A[i] && A[i]%2 ==0 ) max = A[i];
    }
    printf("Minimum  Even : %d\n",min);
    printf("Maximum  Even : %d\n",max);
    getch();
}

When I fill my array with 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Why is the minimum even number equal to 1?

enter image description here

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Opal Opal
  • 35
  • 1
  • 3

3 Answers3

3

This test case exposes one of the dark sides of your code where you have initialized your min and max to A[1] regardless of whether A[1] is odd or even.

Problem:

There may be cases where the array does not contain any even number. In such cases, you may wish to print so instead of printing -1 or INT_MIN or INT_MAX.

If you are new to INT_MIN and INT_MAX, this will serve as a starter before proceeding to the solution.

Solution:

This solution modifies your code in such a way that it handles all the cases given that you provide the inputs without any error.

  1. Have a flag to know whether you found your min and max:

    int foundAnswer = 0;
    
  2. Add the header file limits.h and initialize your min and max as follows:

    min = INT_MAX;
    max = INT_MIN;
    
  3. Modify your loop such that the flag serves a purpose:

    Note:

    Don't waste the zeroth index of your array without any reason. Modify your array declaration and input loop accordingly before changing this one.

    for (i = 0; i < ARRAY_SIZE; ++i)
    {
     if (A[i] % 2 == 0)
     {
      foundAnswer = 1;
      if (A[i] < min) min = A[i];
      if (A[i] > max) max = A[i];
     }
    }
    
  4. Modify your printing code slightly so that all the cases are covered.

    if (foundAnswer)
    {
     // Print min
     // Print max
    }
    else
    {
     // Print "min and max not found"
    }
    

Bonus:

You can learn from the following links in order to optimize your code:

  1. How do I check if an integer is even or odd using bitwise operators

  2. The algorithms discussed here will provide you with a better time complexity to achieve the same.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • Your bug report and the first tip is based on the case where all the elements of the array are similar. I have updated my solution with respect to your suggestions. Thank you @ikegami – Ardent Coder Jan 28 '20 at 06:35
  • I appreciate the fact that `INT_MAX` is odd. I have replaced `<=` and `>=` with `<` and `>`. However, the real reason to do so is that `min` and `max` are initialized to `INT_MAX` and `INT_MIN` respectively. Re *"I said that findmin and findmax were always the same":* You also followed it up with adding an `else` before my second `if` into the code which is also visible in the edit history. That would fail in the case where the array size is 1 and the only element is even. Anyways, I have updated my code as needed @ikegami – Ardent Coder Jan 28 '20 at 07:55
  • 1
    Re: *"Ah yes, that's a bug."* No worries, it was a nice time working on this answer with you. I'm still a student; you gave me some teamwork experience. Your suggestions and bug reports were spot on. Thank you @ikegami – Ardent Coder Jan 28 '20 at 08:11
1

The problem is that you initialize min and max to an value that might not be even.

The following is an efficient solution that handles empty arrays, arrays containing only odd numbers, and arrays containing only INT_MAX:

int A[] = {1,2,3,4,5,6,7,8,9,10};
size_t n = 10;

size_t found_even = 0;
int min, max;
while (n--) {
   int val = A[n];
   if ((A[n] & 1) == 0) {  // Assumes a 2's complement machine.
      if (found_even++) {
         if      (A[n] < min) min = A[n];
         else if (A[n] > max) max = A[n];
      } else {
         min = val;
         max = val;
      }
   }
}

if (found_even) {
   printf("Minimum even: %d\n", min);
   printf("Maximum even: %d\n", max);
} else {
   printf("Minimum even: <none>\n");
   printf("Maximum even: <none>\n");
}

Slightly faster:

int A[] = {1,2,3,4,5,6,7,8,9,10};
size_t n = 10;

int found_even = 0;
int min, max;
while (n--) {
   int val = A[n];
   if ((A[n] & 1) == 0) {  // Assumes a 2's complement machine.
      min = val;
      max = val;
      found_even = 1;
      break;
   }
}

if (found_even) {
   while (n--) {
      if ((A[n] & 1) == 0) {
         if      (A[n] < min) min = A[n];
         else if (A[n] > max) max = A[n];
      }
   }

   printf("Minimum even: %d\n", min);
   printf("Maximum even: %d\n", max);
} else {
   printf("Minimum even: <none>\n");
   printf("Maximum even: <none>\n");
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

min = A[1]; Here min is already initialized to 1. So,

if(min > A[i] && A[i]%2 ==0 ) min = A[i]; condition will never be true.

Add the header file limits.h and initialize min=INT_MAX; max=INT_MIN;