[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

C Program To Demonstrate Working of 2D Arrays

The document contains multiple C programs demonstrating the use of 2D and 3D arrays. It includes examples of initializing arrays, calculating row sums, and performing matrix multiplication. Each program is structured to showcase different functionalities of arrays in C.

Uploaded by

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

C Program To Demonstrate Working of 2D Arrays

The document contains multiple C programs demonstrating the use of 2D and 3D arrays. It includes examples of initializing arrays, calculating row sums, and performing matrix multiplication. Each program is structured to showcase different functionalities of arrays in C.

Uploaded by

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

// C program to demonstrate working of 2D arrays

#include <stdio.h>

int main() {
/*
Declare and initialize a 2×2 integer array.
arr[0][0] = 10, arr[0][1] = 20,
arr[1][0] = 30, arr[1][1] = 40
*/
int arr[2][2] = { {10, 20}, {30, 40} };

printf("2D Array Elements:\n");


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}

#include <stdio.h>

int main() {

// Create and Initialize the


// 3-dimensional array
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 },
{ 4, 5 } }, { { 6, 7 },
{ 8, 9 }, { 10, 11 } } };

// Loop through the depth


for (int i = 0; i < 2; ++i) {

// Loop through the


// rows of each depth
for (int j = 0; j < 3; ++j) {

// Loop through the


// columns of each row
for (int k = 0; k < 2; ++k)
printf("arr[%i][%i][%i] = %d ", i, j, k,
arr[i][j][k]);
printf("\n");
}
printf("\n\n");
}
return 0;
}

#include <stdio.h>

int main() {

// Create and Initialize the


// 3-dimensional array
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 },
{ 4, 5 } }, { { 6, 7 },
{ 8, 9 }, { 10, 11 } } };

// Loop through the depth


for (int i = 0; i < 2; ++i) {

// Loop through the


// rows of each depth
for (int j = 0; j < 3; ++j) {

// Loop through the


// columns of each row
for (int k = 0; k < 2; ++k)
printf("arr[%i][%i][%i] = %d ", i, j, k,
arr[i][j][k]);
printf("\n");
}
printf("\n\n");
}
return 0;
}

--------------------------

#include <stdio.h>

int main() {
int arr[3][5] = {{1,2,3,4,5}, {10,20,30,40,50}, {5,10,15,20,25}};
int i, j;
int sum;

for (i=0; i<3; i++){


sum=0;
for (j=0; j<5; j++){
sum+=arr[i][j];
}
printf("Sum of row %d: %d\n", i, sum);
}
return 0;
}
---------------------------------------------

#include<stdio.h>
int main(){
int mat1[3][3] = { {2, 4, 1} , {2, 3, 9} , {3, 1, 8} };
int mat2[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 4, 7} };
int mat3[3][3], sum=0, i, j, k;

for(i=0; i<3; i++){


for(j=0; j<3; j++){
sum=0;
for(k=0; k<3; k++)
sum = sum + mat1[i][k] * mat2[k][j];
mat3[i][j] = sum;
}
}
printf("\nMatrix 1 ...\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat1[i][j]);
printf("\n");
}

printf("\nMatrix 2 ...\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat2[i][j]);
printf("\n");
}

printf("\nMultiplication of the two given Matrices: \n");


for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat3[i][j]);
printf("\n");
}

return 0;
}

You might also like