C Programming
In this tutorial we will learn how to pass and use arrays in functions in C programming language.
We know that when we want to store data of same data type under one variable name we take help of arrays.
To accept one dimensional array our function declaration will look like the following.
returnType functionName(type arr[], type size);
Example:
float findAverage(int arr[], int size);
In the above function declaration we have a function by the name findAverage
. The first parameter is of type int
and takes one dimensional integer array. The second parameter of int
type is for the size of the array i.e., number of elements. The return type of this function is set to float
so, it will return a floating point value.
To pass a one dimensional array we simply write the name of the array variable as the function argument.
For example, if we want to store marks of a student in 5 papers then we can create an integer array of size 5 and type int
and give it a name lets say, score
.
int score[5] = {90, 80, 70, 75, 85};
Note! In the above example we are assuming that the total marks of each paper is 100 and score will be an integer value.
Now lets say, we want to find the average score of the student by creating a function findAverage()
which takes an integer array and returns a floating point value.
In the following example we are passing a one dimensional array of type int
and by the name score
to the findAverage()
function.
int score[5] = {90, 80, 70, 75, 85};
int papers = 5;
float avg = findAverage(score, papers);
The complete code is given below.
#include <stdio.h>
float findAverage(int [], int);
int main(void) {
int score[5] = {90, 80, 70, 75, 85};
int papers = 5;
float avg = findAverage(score, papers);
printf("Average: %f\n", avg);
return 0;
}
float findAverage(int arr[], int size) {
// variables
int
i;
float
sum = 0,
avg = 0;
// find total
for (i = 0; i < size; i++) {
sum += arr[i];
}
// find average
avg = sum / size;
// return average
return avg;
}
Output:
Average: 80.000000
To accept two dimensional array our function declaration will look like the following.
returnType functionName(type arr[][C], type rows, type cols);
Example:
void printAverage(int arr[][C], int rows, int cols);
In the above example we have a function by the name printAverage
. The first parameter is a two dimensional array as we are using two square brackets representing the number of rows and columns.
It is important to specify the size of the second dimension i.e., number of columns. So, in the above function declaration we are passing a constant C.
The second and third parameters int rows
and int cols
represents the number of rows and columns in the array arr
respectively.
The above function returns no value so, the return type is set to void
.
To pass a two dimensional array to a function all we have to do is write the array name.
Lets say, a student gives 3 tests daily for 5 days and we want to print the average score of each day.
So, we will first create a two dimensional array lets say score
and it will have 5 rows denoting the 5 days and 3 columns denoting the 3 tests.
int score[5][3] = {
{60, 70, 80},
{90, 50, 70},
{80, 75, 75},
{90, 85, 81},
{60, 75, 80}
};
So, if we want to pass this two dimensional array score
to a function printAverage
we have to write the following.
printArray(score, ROWS, COLS);
Complete code will look like the following.
#include <stdio.h>
void printAverage(int [][3], int, int);
int main(void) {
// variables
int
ROWS = 5,
COLS = 3;
int score[5][3] = {
{60, 70, 80},
{90, 50, 70},
{80, 75, 75},
{90, 85, 81},
{60, 75, 80}
};
printAverage(score, ROWS, COLS);
return 0;
}
void printAverage(int arr[][3], int rows, int cols) {
// variables
int
r, c;
float
sum, avg;
// find average and print it
for (r = 0; r < rows; r++) {
sum = 0;
for (c = 0; c < cols; c++) {
sum += arr[r][c];
}
avg = sum / cols;
printf("Average on Day #%d = %f\n", (r + 1), avg);
}
}
Output:
Average on Day #1 = 70.000000
Average on Day #2 = 70.000000
Average on Day #3 = 76.666664
Average on Day #4 = 85.333336
Average on Day #5 = 71.666664
ADVERTISEMENT