Passing an Array in a Function - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Passing an Array in a Function

Share This
Here we will discuss different ways of passing an array in a function. An array can be passed in a function as a formal parameter in three different ways as follow.

  • Formal parameters as a pointer
  • Formal parameters as a sized array
  • Formal parameters as an unsized array
  • The following code shows how to send an array in a function.

One-Dimensional Array: Formal parameters as a pointer
#include <stdio.h> void readArrayElements(int *nums, int n); int calSummation(int *nums, int n); int main() { int nums[100]; int i, n; printf("Enter number of elements:"); scanf("%d", &n); readArrayElements(nums, n); printf("Sum of numbers is %d", calSummation(nums, n)); return 0; } void readArrayElements(int *nums, int n) { int i; printf("Enter numbers:"); for(i = 0; i < n; i++) { scanf("%d", &nums[i]); } } int calSummation(int *nums, int n) { int i, sum = 0; for(i = 0; i < n; i++) { sum += nums[i]; } return sum; }
One-Dimensional Array: Formal parameters as a sized array
#include <stdio.h> void readArrayElements(int nums[100], int n); int calSummation(int nums[100], int n); int main() { int nums[100]; int i, n; printf("Enter number of elements:"); scanf("%d", &n); readArrayElements(nums, n); printf("Sum of numbers is %d", calSummation(nums, n)); return 0; } void readArrayElements(int nums[100], int n) { int i; printf("Enter numbers:"); for(i = 0; i < n; i++) { scanf("%d", &nums[i]); } } int calSummation(int nums[100], int n) { int i, sum = 0; for(i = 0; i < n; i++) { sum += nums[i]; } return sum; }
One-Dimensional Array: Formal parameters as an unsized array
#include <stdio.h> void readArrayElements(int nums[], int n); int calSummation(int nums[], int n); int main() { int nums[100]; int i, n; printf("Enter number of elements:"); scanf("%d", &n); readArrayElements(nums, n); printf("Sum of numbers is %d", calSummation(nums, n)); return 0; } void readArrayElements(int nums[], int n) { int i; printf("Enter numbers:"); for(i = 0; i < n; i++) { scanf("%d", &nums[i]); } } int calSummation(int nums[], int n) { int i, sum = 0; for(i = 0; i < n; i++) { sum += nums[i]; } return sum; }
Multi-Dimensional Array: Formal parameters as a pointer
#include <stdio.h> int ** dynamicMemAllocation(int m, int n); void readMatrix(int **mat, int m, int n); void addMatrix(int **mat1, int **mat2, int **res, int m, int n); void printMatrix(int **mat, int m, int n); int main() { int m, n, i; int **mat1, **mat2, **res; printf("Enter order of matrices: "); scanf("%d %d", &m, &n); mat1 = dynamicMemAllocation(m, n); mat2 = dynamicMemAllocation(m, n); res = dynamicMemAllocation(m, n); printf("Hello"); readMatrix(mat1, m, n); readMatrix(mat2, m, n); addMatrix(mat1, mat2, res, m, n); printMatrix(res, m, n); return 0; } int ** dynamicMemAllocation(int m, int n) { int **mat; int i; mat = (int **)malloc(m * sizeof(int *)); for(i = 0; i < m; i++) *(mat+i) = (int *)malloc(n * sizeof(int)); return mat; } void readMatrix(int **mat, int m, int n) { int i, j; printf("Enter elements of matrix:\n"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("%d", (*(mat+i)+j)); } } } void addMatrix(int **mat1, int **mat2, int **res, int m, int n) { int i, j; for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { res[i][j] = mat1[i][j] + mat2[i][j]; } } } void printMatrix(int **mat, int m, int n) { int i, j; printf("The resultant matrix is : \n"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } }
Multi-Dimentional Array: Formal parameters as a sized array
#include <stdio.h> void readMatrix(int mat[10][10], int m, int n); void addMatrix(int mat1[10][10], int mat2[10][10], int res[10][10], int m, int n); void printMatrix(int mat[10][10], int m, int n); int main() { int m, n; int mat1[10][10], mat2[10][10], res[10][10]; printf("Enter order of matrices: "); scanf("%d %d", &m, &n); readMatrix(mat1, m, n); readMatrix(mat2, m, n); addMatrix(mat1, mat2, res, m, n); printMatrix(res, m, n); return 0; } void readMatrix(int mat[10][10], int m, int n) { int i, j; printf("Enter elements of matrix:\n"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("%d", &mat[i][j]); } } } void addMatrix(int mat1[10][10], int mat2[10][10], int res[10][10], int m, int n) { int i, j; for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { res[i][j] = mat1[i][j] + mat2[i][j]; } } } void printMatrix(int mat[10][10], int m, int n) { int i, j; printf("The resultant matrix is : \n"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } }
Multi-Dimentional Array: Formal parameters as an unsized array
#include <stdio.h> void readMatrix(int mat[][10], int m, int n); void addMatrix(int mat1[][10], int mat2[][10], int res[][10], int m, int n); void printMatrix(int mat[][10], int m, int n); int main() { int m, n; int mat1[10][10], mat2[10][10], res[10][10]; printf("Enter order of matrices: "); scanf("%d %d", &m, &n); readMatrix(mat1, m, n); readMatrix(mat2, m, n); addMatrix(mat1, mat2, res, m, n); printMatrix(res, m, n); return 0; } void readMatrix(int mat[][10], int m, int n) { int i, j; printf("Enter elements of matrix:\n"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("%d", &mat[i][j]); } } } void addMatrix(int mat1[][10], int mat2[][10], int res[][10], int m, int n) { int i, j; for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { res[i][j] = mat1[i][j] + mat2[i][j]; } } } void printMatrix(int mat[][10], int m, int n) { int i, j; printf("The resultant matrix is : \n"); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } }


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.