Answer to Question #256367 in C for Amit

Question #256367

Write a C code of O(n) order to remove all the odd numbers from the array. Example: the

array contains 10, 2, 3, 7, 8, 6, 11. The output should be 10, 2, 8, and 6


1
Expert's answer
2021-10-26T11:13:57-0400
#include <stdio.h>

#define SIZE 10

// size - original size of array; will be updated after remnoving odd integers
void RemoveOdd(int *arr, int *size) {
  int sz = *size;
  for (int i = 0, j = 0; i < *size; i++) {
    if (arr[i] % 2 == 0) {
      arr[j] = arr[i];
      j++;
      sz--;
    }
  }
  *size = sz;
}

int main() {
  int arr[SIZE];
  for (int i = 0; i < SIZE; i++) arr[i] = i + 1;
  int size = SIZE;
  RemoveOdd(arr, &size);
  for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS