Question: Modify the function is_even in program below. The function should use pointer arithmetic not subscripting to visit array elements. In other words, eliminate the loop
Modify the function is_even in program below. The function should use pointer arithmetic not subscripting to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.
void is_even(int * numbers, int n, int * result);
The is_even function determines if an element in the array is an even number. If it is even, the function stores 1 at the corresponding element in the result array, otherwise, it stores 0.
The main function remains the same as in the code below.
Sample run:
Enter the number of integers: 5
Enter the array elements: 32 24 7 21 19
Output: The output array: 1 1 0 0 0
This is the code to be modified below:
#include
void is_even(int numbers[], int n, int result[]);
//A program that determines if an element in an array is an even or odd number
int main()
{
//Declaring numbers and result as and integer array that can store up to 20 numbers.
//Declaring n and i as an integer.
int numbers[20];
int n;
int result[20];
int i;
//Enter the numbers of intergers for the array
printf("Enter the number of integers: ");
scanf("%d", &n);
//Enter the values to be saved in the array
printf("Enter the array elements: ");
//Using a loop statement to read in and display the outputs
for(i = 0; i < n; i++)
scanf("%d", &numbers[i]);
is_even(numbers, n, result);
printf("The output array: ");
for(i = 0; i < n; i++)
printf("%d ", result[i]);
printf(" ");
}
void is_even(int numbers[], int n, int result[])
{
//Using loop statement and a conditional statement to check if each value enter by the is even or odd.
//Displays 1 for even number and 0 for odd numbers.
int i;
for(i = 0; i < n; i++)
{
if(numbers[i] % 2 == 0) //is it even?
result[i] = 1;
else
result[i] = 0;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
