Answer to Question #194608 in C for Muhammad Haris

Question #194608

Write a function countEven(int*, int) which receives an integer array and its size, and returns the number of even numbers in the array.


1
Expert's answer
2021-05-18T02:58:13-0400
#include <stdio.h>
#include <stdlib.h>


int countEven(int* integerArray, int size)
{
    int counter = 0;
    int i;
    for(i = 0; i < size; i++)
    {
        if(integerArray[i]%2==0)
            counter++;
    }
    return counter;
}


int main()
{
    int size = 5;
    int* integerArray = (int*)malloc(size * sizeof(int));


    if(integerArray!=NULL)
    {
        int i;
        for(i = 0; i < size; i++)
            integerArray[i] = i+1;


        printf("Array : ");
        for(i = 0; i < size; i++)
            printf("%d ", integerArray[i]);


        printf("\n");
        printf("count even numbers: %d", countEven(integerArray, size));
        free(integerArray);
    }
    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