Pointer array Odd Numbers

PHOTO EMBED

Sun Jun 09 2024 11:30:52 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #pointer

#include <stdio.h>

int count_odds(int* data_array, int size);

int main(void)
{
	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 result_1 = count_odds(data_array_1, 6);
	printf("data_array_1 has %d odd numbers.\n", result_1);
	
	int result_2 = count_odds(data_array_2, 8);
	printf("data_array_2 has %d odd numbers.\n", result_2);
	
	int result_3 = count_odds(data_array_3, 11);
	printf("data_array_3 has %d odd numbers.\n", result_3);
	
	return 0;
}

// TODO: Insert your function definition here!
int count_odds(int* data_array, int size)
{
    int count = 0;
    
    
    for(int i =0; i < size ;i++)
    {
        if(data_array[i] > 0 && data_array[i] % 2 != 0)
        {
            count++;
        }
    }
    return count;
}
content_copyCOPY

Expected data_array_1 has 6 odd numbers. data_array_2 has 0 odd numbers. data_array_3 has 5 odd numbers. The purpose of this exercise is to provide students with an understanding of array manipulation and function calls in C. The main task is to implement a function that counts the number of odd integers in an array. The function must be called with three different arrays in the main() function to demonstrate its utility. Requirements Function Declaration and Definition Declare and define a function named count_odds that: Takes an integer array pointer named data_array and an integer named size as parameters. Returns an integer representing the count of odd numbers in the array. Main Function In the main() function: Declare and initialize three arrays, data_array_1, data_array_2, and data_array_3, with different integer elements. Call the count_odds function three times: once for each array. Store the results in variables named result_1, result_2, and result_3. Print the number of odd integers found in each array using printf.