0% found this document useful (0 votes)
26 views10 pages

Two Dimensional Array

Two-dimensional arrays, also called matrices, are collections of one-dimensional arrays placed below each other. They are declared with two indices separated by commas inside square brackets to specify the number of rows and columns. Elements can be initialized during declaration and accessed using their row and column indices. Matrices can be added, multiplied, and have other operations performed on them by iterating through their elements in nested loops.

Uploaded by

satya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
26 views10 pages

Two Dimensional Array

Two-dimensional arrays, also called matrices, are collections of one-dimensional arrays placed below each other. They are declared with two indices separated by commas inside square brackets to specify the number of rows and columns. Elements can be initialized during declaration and accessed using their row and column indices. Matrices can be added, multiplied, and have other operations performed on them by iterating through their elements in nested loops.

Uploaded by

satya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Two-Dimensional Arrays

The Two-dimensional array is also called a matrix. a two- dimensional array is nothing but a collection of a
number of one- dimensional arrays placed one below the other.
Declaration of Two- dimensional array
Syntax:
Syntax: Storage class Datatype Arrayname[size1][size2];
Where size1 represent the number of rows
Size2 represent the number of columns
int s[ 5 ][ 2 ] ;
Initializing a Two-Dimensional Array
RowWise Assignment
int stud[ 4 ][ 2 ] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
};
Combine Assignment: Remember that, while initializing a 2-D array, it is necessary to mention the second
(column) dimension, whereas the first dimension (row) is optional.
int arr[ 2 ][ 3 ] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][ 3 ] = { 12, 34, 23, 45, 56, 45 };
Selective Assignment
int stud[ 4 ][ 2 ] = {
{ 56 },
{ 1, 33 },
{ 80 },
{ 2, 78 }
};
output;
56 0
1 33
80 0
2 78
//Write a program to add two matrices of dimension 3*3 and store the result in another matrix.
#include<stdio.h>
void main()
{
int i, j,a[3][3], b[3][3], c[3][3];
//input in first matrix
printf("Insert your matrix elements in first matrix elements :\n");
for (i= 0; i< 3; i++)
{
for (j = 0; j< 3; j++)
{
scanf("%d",&a[i][j]);
}
}
//input in second matrix
printf(" Insert your matrix elements in second matrix elements :\n");
for (i= 0; i< 3; i++)
{

for (j = 0; j< 3; j++)


{
scanf("%d",&b[i][j]);
}
}
//display the first matrix
printf(" \n ist matrix is \n");
for (i= 0; i< 3; i++)
{
for (j = 0; j< 3; j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//display the second matrix
printf(" \n 2nd matrix is \n");
for (i= 0; i< 3; i++)
{
for (j = 0; j< 3; j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
// logic of matrix addition
for (i= 0; i< 3; i++)
{
for (j = 0; j< 3; j++)
{
c[i][j]=a[i][j]+ b[i][j];
}
}
//display the addition
printf("\n result is \n");
for (i= 0; i< 3; i++)
{
for (j = 0; j< 3; j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

// Write a program to print all the diagonal elements of the Matrix.


#include <stdio.h>
int main()
{
int a[10][10];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
if(r==c)
{
//input in matrix a
printf("\nEnter matrix elements :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
scanf("%d",&a[i][j]);
}
}
//display the elements in the matrix
printf("\n matrix elements are\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//print the diagonal of matrix
printf("\n diagonal elements are\n");
for(i=0,j=0;i<r&&j<c;i++,j++)
{
printf("%d\t",a[i][j]);
}
//print the diagonal of matrix
printf("\n diagonal elements are\n");
for(i=0,j=c-1;i<r&&j>=0;i++,j--)
{
printf("%d\t",a[i][j]);
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}
return 0;
}

//Write a program to find transpose of matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a[3][4],b[4][3];
//Input in matrix
printf("\nenter ist matrices\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
//logic of transpose
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i];
}
}
//display of matrix a
printf("\ninput matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//display of matrix b after transpose
printf("\ntranspose of matrices is\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}

// Write a program in C to input two 3x3 matrix from the user and print multiplication as the result in
matrix form. //(Write comments also at appropriate places in the program)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[3][3],b[3][3],c[3][3];
//input in first matrix
printf("\nenter ist matrices\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
//input in 2nd matrix
printf("\nenter 2nd matrices\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
//display of ist matrix
printf("\n ist matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//display of 2nd matrix
printf("\n second matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
//logic of matrix multiplication
printf("\nMultiplicaiton of matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
//Display the result of matrix multiplication
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

// Write a program in 'C' to multiply the two matrices of size M × N.


OR
/* Write a C program to multiply matrix A of order m*n with matrix B of order p*q. Also display
the resultant matrix.*/
#include<stdio.h>
void main()
{
int i, j, p, q, m, n, k, tot = 0;
int a[10][10], b[10][10], c[10][10];
printf(" Please insert the number of rows and columns for first matrix \n");
scanf("%d%d",&m,&n);
printf(" Please insert the number of rows and columns for second matrix\n");
scanf("%d%d",&p,&q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other. \n");
else
{
printf(" Insert your matrix elements : \n");
for (i= 0; i< m; i++)
{
for (j = 0; j< n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf(" Insert your elements for second matrix \n");
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{
scanf("%d",&b[i][j] );
}
}
for (i = 0; i< m; i++) // number of rows in ist matrix
{
for (j = 0; j<q; j++) //number of columns in 2nd matrix
{
c[i][j]=0;
for (k = 0; k <p; k++) // number of columns in first matrix or number of rows in 2nd matrix
{
c[i][j]=c[i][j]+ a[i][k] * b[k][j];
}
}
}
//display of ist matrix
printf("\n ist matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//display of 2nd matrix
printf("\n second matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
//display the result of matrix multiplication
printf("\n matrix multiplication is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}

You might also like