pointer Zero Array

PHOTO EMBED

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

Saved by @meanaspotato #c #pointer

#include <stdio.h>

void print_array(int *p_array, int num_elements);
void zero_out_array(int *p_array, int num_elements);
int main(void)
{
	int main_array[] = { 15, 24, 33, 42, 51 };

	// TODO: Insert code here...
	int num_elements = sizeof(main_array)/sizeof(main_array[0]);
	
	print_array(main_array,num_elements);
    zero_out_array(main_array,num_elements);
    print_array(main_array,num_elements);
	return 0;
}

void print_array(int *p_array, int num_elements)
{
	printf("print_array called:\n");
	// TODO: Insert code here...
	for(int i = 0; i < num_elements; i++)
	{
	    printf("%d ",p_array[i]);
	}
	printf("\n\n");
}

void zero_out_array(int *p_array, int num_elements)
{
	printf("zero_out_array called:\n\n");
	// TODO: Insert code here...
	for(int i = 0; i < num_elements; i++)
	{
	    *(p_array+i) = 0;
	}
}
content_copyCOPY

Expected print_array called: 15 24 33 42 51 zero_out_array called: print_array called: 0 0 0 0 0 The objective of this exercise is to familiarize students with the concepts of array manipulation using pointer arithmetic and index notation in C. Students are required to implement two functions: print_array, which prints the elements of an array, and zero_out_array, which sets all elements of an array to zero. Requirements Function Declarations and Definitions Declare and define a function called print_array that: Takes an integer pointer p_array and an integer num_elements as parameters. Prints each element of the array. Utilizes the bracket array index access notation [ ] for array traversal. Declare and define a function called zero_out_array that: Takes an integer pointer p_array and an integer num_elements as parameters. Sets each element of the array to zero. Uses pointer arithmetic (* and ++) for array traversal. Main Function In the main() function: Declare and initialize an integer array named main_array with values {15, 24, 33, 42, 51}. Call print_array to print the initial elements of main_array. Call zero_out_array to set all elements of main_array to zero. Call print_array again to print the modified main_array to confirm that all elements have been set to zero.