You are on page 1of 5

Surigao Del Norte State University

C COLLEGE OF ENGINEERING & INFORMATION TECHNOLOGY


Narciso Street, Surigao City

Functions in Arrays
DRILL 5

STUDENT NAME :
SECTION :
DATE OF SUBMISSION: SCORE
Score

Engr. Catherine M. Verallo, MSCpE


Instructor
Topic 5 : Functions in Arrays

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

A. Functions with 1D array


In C programming, array are essential data structures used to store a collection of elements of
the same data type. Functions allow us to organize code into modular blocks, making the code
easier to understand, maintain, and reuse. Passing 1D arrays to functions, modifying array
elements within functions, and returning arrays from functions are important concepts when
working with arrays and functions together.

1. Passing 1D Arrays to Functions


Passing a 1D array to a function in C involves sending the base address of the array to the
function. The function can then access and manipulate the array elements directly. This allows
us to work with arrays more efficiently and avoids the need for global variables.

#include<stdio.h>

double getSum(double[], int);

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;
}

double getSum(double arr[], int size) {


double sum = 0;
for(int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

Output :
Sum = 66.46

Explanation of the code :

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.

2. Returning Arrays from Functions


#include <stdio.h>
#include <stdlib.h>

int* createArray(int size) {


int* arr = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
arr[i] = i + 1;
}
return arr;
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int size = 5;
int* newArr;

newArr = createArray(size);

printf("New Array: ");


printArray(newArr, 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.

B. Functions with 2D array


In C programming, working with 2D arrays is essential for handling tabular data or matrices.
Functions play a crucial role in code organization and reusability. Understanding how to pass
2D arrays to functions, modify their elements, and return arrays from functions is fundamental
for efficient programming. This guide will walk you through these concepts step-by-step with
code snippets, diagrams, and explanations to help you grasp the concepts effectively.

Passing 2D Arrays to Functions


Passing a 2D array to a function allows you to perform operations on the array without having to
copy it entirely. In C, arrays are passed by reference, which means the function receives a
pointer to the original array, making it possible to modify the array directly.
#include <stdio.h>
void displayArray(int rows, int cols, int arr[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
displayArray(3, 3, matrix);
return 0;
}
4
Output
1 2 3
4 5 6
7 8 9

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.

II. Laboratory Exercises


1. Write the function that accepts an integer array and its size then prints all the values of that array
in one line separated by a comma and a space (15 points)
2. Create a function named print2DArray that accepts a multidimensional array of
integers, its number of rows, and its number of columns. This function prints the values
of the multidimensional array starting from first until the last element.

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

Enter number of rows: 3


Enter number of columns: 3
1 2 3
4 5 6
7 8 9
Array Values:
1 2 3
4 5 6
7 8 9
Reference :

CodeChum
5

You might also like