c - Trying to write a program that counts the amount of even numbers in an array and returns the value -


i trying make program count number of numbers in provided arrays. when run program now, return amount of numbers in array, not amount of numbers. reason count_even function doesn't work. can help?

 #include <stdio.h>  int main() {     int data_array_1[] = { 1, 3, 5, 7, 9, 11 };     int data_array_2[] = { 2, -4, 6, -8, 10, -12, 14, -16 };     int data_array_3[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };     int data_array_4[] = { 6, 2, 4, 5, 1, -9 };     int data_array_5[] = { 1, 3, 9, 23, 5, -2, 4 };      int result_1 = count_even(data_array_1, 6);      printf("data_array_1 has %d numbers.\n", result_1);      int result_2 = count_even(data_array_2, 8);      printf("data_array_2 has %d numbers.\n", result_2);      int result_3 = count_even(data_array_3, 11);      printf("data_array_3 has %d numbers.\n", result_3);      int result_4 = count_even(data_array_4, 6);      printf("data_array_4 has %d numbers.\n", result_4);      int result_5 = count_even(data_array_5, 7);      printf("data_array_5 has %d numbers.\n", result_5);       return 0; }  int count_even(int* data_array, int size) {     int even_num = 0;      (int = 0; == size; i++)     {         if (data_array[size] % 2 == 0)         {             even_num++;         }     }      return even_num; } 

the condition in loop wrong. correct condition should "as long index smaller size", yours "as long index equal to size".

the condition should i < size.

as result, seems should return 0 (for non-working code), not size.

also, using size index, when should use i.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -