Drill 5
Drill 5
Drill 5
Functions in Arrays
DRILL 5
STUDENT NAME :
SECTION :
DATE OF SUBMISSION: SCORE
Score
I. Learning Objective
At the end of the session, the student must be able to
• Explore passing functions in 1D array and 2D array
• Evaluate programs with functions in 1D array and 2D array.
• Create and execute programs containing functions in 1D array and 2D array
II. Discussion
#include<stdio.h>
int main(void) {
double arr[5] = {1, 3.14, 3.2220, 4.1, 55};
double sum = getSum(arr, 5);
printf("Sum = %.2lf", sum);
return 0;
}
Output :
Sum = 66.46
The code defines a function addConstant to add a constant value to each element of an integer array. It
then demonstrates the usage of this function in the main function.
The addConstant function takes three parameters:
2
• arr: A pointer to the integer array.
• size: An integer representing the size of the array.
• constant: An integer representing the constant value to be added to each element of
the array.
Inside the addConstant function:
• It uses a for loop to iterate through each element of the array.
• For each element, it adds the constant value to it.
In the main function:
• An integer array arr with initial values {1, 2, 3, 4, 5} is declared.
• The size variable is calculated as the total number of elements in the array.
• The constant variable is set to 10.
• The original array is printed using a custom function printArray.
• The addConstant function is called to modify the array by adding 10 to each element.
• The modified array is printed again using printArray.
newArr = createArray(size);
free(newArr);
return 0;
}
Output :
New Array : 1 2 3 4 5
3
Explanation:
The createArray function remains the same, which dynamically allocates memory for an integer array of
the given size and initializes its elements with consecutive integer values starting from 1.
A new function printArray is added to print the elements of an integer array. It takes two parameters:
• arr: A pointer to the integer array.
• size: An integer representing the size of the array.
Inside the printArray function:
• It uses a for loop to iterate through each element of the array.
• It prints each element using printf.
In the main function:
• The size variable is set to 5 to indicate the desired size of the new array.
• A pointer newArr is declared to hold the address of the newly created array.
• The createArray function is called, and the returned array address is assigned
to newArr.
• The elements of the newly created array are printed using the printArray function.
• After printing, the dynamically allocated memory is freed using free(newArr) to avoid
memory leaks.
Dynamically creating an integer array of size 5, initializing it with consecutive integer values from 1 to 5,
and then printing the elements of the array before freeing the allocated memory.
Explanation:
This C code defines a function displayArray to print the elements of a 2D integer array. It then
demonstrates the usage of this function in the main function to print a 3x3 matrix.
The displayArray function takes three parameters:
• rows: An integer representing the number of rows in the 2D array.
• cols: An integer representing the number of columns in the 2D array.
• arr: A 2D integer array of size rows x cols.
Inside the displayArray function:
• It uses nested for loops to iterate through each element of the 2D array.
• For each row, it prints the elements of that row separated by spaces.
• After each row, it prints a newline character to move to the next line.
In the main function:
• A 3x3 matrix called matrix is declared and initialized with values {1, 2, 3}, {4, 5, 6}, and
{7, 8, 9}.
• The displayArray function is called, passing the dimensions of the matrix (3 rows and 3
columns) and the matrix array as arguments.
• The displayArray function prints the elements of the matrix in a row-by-row format.
In the main, ask the user to input the rows and columns of the array and its values then
these values to the function call of print2DArray. (25 points)
Sample output
CodeChum
5