The document discusses two dimensional arrays in C programming. It explains how to declare, initialize and access 2D arrays. It provides examples of accepting 2D array elements as input and displaying them. It also discusses various matrix operations like addition, multiplication using 2D arrays. Functions to input, multiply and display matrices are demonstrated. Other topics covered include passing 2D arrays to functions, matrix transpose, finding sum of diagonal elements and multi-dimensional arrays. Finally, it briefly explains command line arguments in C.
1. 17Jan2023: 2D Arrays: Matrix operations
PROGRAMMING FOR
PROBLEM SOLVING (PPS)
B.Tech I Sem CST
2. Multidimensional arrays
One dimensional array:
int oneD[size1];
Two dimensional array:
int twoD[size1][size2];
Three dimensional array:
int threeD[size1] [size2] [size2];
N dimensional array:
int multiD[size1]…[sizen]; Dr. C. Sreedhar
3. 2D Array
int arr2d[size1][size2]; int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
int disp[2][4] =
{ 10, 11, 12, 13, 14, 15, 16, 17};
int abc[][2] = {1, 2, 3 ,4 } // Valid
int abc[][] = {1, 2, 3 ,4 } // Invalid
int abc[2][] = {1, 2, 3 ,4 } // Invalid
Dr. C. Sreedhar
4. Accept 2d elements and display
int disp[2][3];
for(i=0; i<2; i++)
{
for(j=0;j<3;j++)
{
printf("Enter disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0;j<3;j++)
{ printf("%d ", disp[i][j]); if(j==2) printf("n"); }
}
Dr. C. Sreedhar
6. Matrix operations: addition
int r,c,i,j,A[10][10], B[10][10], Sum[10][10];
scanf("%d %d",&r,&c);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
scanf("%d",&A[i][j]);
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
Sum[i][j] = A[i][j] + B[i][j];
printf("%dt", Sum[i][j]);
}
printf("n");
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
scanf("%d",&B[i][j]);
}
Dr. C. Sreedhar
7. int m, n, p, q, i, j, k;
int a[10][10], b[10][10], res[10][10];
scanf("%d%d", & m, & n); // first matrix size
scanf("%d%d", & p, & q); // second matrix size
if (n != p)
printf("Matrix is incompatible for multiplicationn");
else
{
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++) scanf("%d", & a[i][j]);
}
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++) scanf("%d", & b[i][j]);
}
Matrix Mult
Dr.
C.
Sreedhar
8. for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
res[i][j] = 0;
for (k = 0; k < p; k++) res[i][j] += a[i][k] * b[k][j];
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++) printf("%dt", res[i][j]);
printf("n");
}
}
Matrix Mult
Dr.
C.
Sreedhar
9. pass two-dimensional arrays to function
void Function(int c[2][2]);
int main()
{
int c[2][2],i,j;
printf("Enter 4 numbers:n");
for(i=0;i<2;++i)
for(j=0;j<2;++j) {
scanf("%d",&c[i][j]);
}
Function(c);
return 0;
}
Enter 4
numbers:
2
3
4
5
Displaying:
2
3
4
5
void Function(int c[2][2])
{
int i,j;
printf("Displaying:n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
printf("%dn",c[i][j]);
}
Dr. C. Sreedhar
13. for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
scanf("%d", &a[i][j]);
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
transpose[j][i] = a[i][j];
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);
if (j == r - 1) printf("n");
}
Matrix Transpose
Dr.
C.
Sreedhar
14. int matrix[3][3],sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if(i == j) sum += matrix[i][j];
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++)
printf("%d ", matrix[i][j]);
printf("n");
}
printf("Sum of diagonal elements = %dn", sum);
Sum of diagonal elements of matrix
Dr.
C.
Sreedhar
15. MultiDim.Array: Declaration, Initialization, Accessing
Syntax: datatype arrname[size1][size2]...[sizeN];
Declaration: int threeD[2][3][4];
Initialization:
int arr[3][2][2]={0,1,2,3,4,5,6,7,8,9,3,2};
int arr[3][3][3]=
{ { {10,20,30},{40,50,60},{70,80,90} },
{ {11,22,33},{44,55,66},{77,88,99} },
{ {12,23,34},{45,56,67},{78,89,90} }
}; Dr. C. Sreedhar
17. Command line arguments
There are 2 components of Command Line Argument in C:
argc:
refers to “argument count”. It is the first parameter that we use to store
the number of command line arguments.
value of argc should be greater than or equal to 0.
agrv:
refers to “argument vector”. It is basically an array of character pointer
to list all the command line arguments.
Dr. C. Sreedhar
18. #include<stdio.h>
int main(int argc, char** argv)
{
printf("Welcome to PPSn");
int i;
printf("The number of arguments are: %dn",argc);
printf("The arguments are:");
for ( i = 0; i < argc; i++)
{
printf("%sn", argv[i]);
}
return 0;
} Dr. C. Sreedhar
19. int main( int argc, char *argv[] )
{
if( argc == 2 ) {
printf("The argument supplied is %sn", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.n");
}
else {
printf("One argument expected.n");
}
}
Dr. C. Sreedhar