0% found this document useful (0 votes)
2 views1 page

2d array

Uploaded by

bishtakhilesh79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

2d array

Uploaded by

bishtakhilesh79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <stdlib.h>

int sumOfArray(int array[], int n) {


int sum = 0;
for (int i = 0; i < n; i++) {
sum += array[i];
}
return sum;
}

int main() {
int r, c;

printf("Enter the number of rows: ");


scanf("%d", &r);

printf("Enter the number of columns: ");


scanf("%d", &c);

int** array = (int**)malloc(r * sizeof(int*));


for (int i = 0; i < r; i++) {
array[i] = (int*)malloc(c * sizeof(int));
}

printf("Enter the elements of the array: \n");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &array[i][j]);
}
}

int sum = 0;
for (int i = 0; i < r; i++) {
sum += sumOfArray(array[i], c);
}

printf("Sum of all the elements in the array: %d\n", sum);


for (int i = 0; i < r; i++) {
free(array[i]);
}
free(array);

return 0;
}

You might also like