Title: Two Dimensional Array - PTInstitute
1TWO DIMENSIONAL ARRAY
By Professional Training Institute
- Professional Embedded Training Institute in
Bangalore
2TWO DIMENSIONAL
ARRAY
- We have introduced array as if we want to save a
hundred values then its difficult to declare
variables, so we used array .now if we want
hundred such arrays then we can have
two-dimensional arrays .so an array of arrays is
known as 2d array. Through our website you can
learn more.
31.1 PROGRAM TO CHECK WHETHER THE TWO(33)
MATRICES ARE EQUAL OR NOT
DESCRIPTION
- First we taken the two(33) matrixes, afterwards
we need to check that all elements inside the two
matrices are equal or not in both matrices with
respect to position wise also, if all elements
with respect to position in two matrices are
equal then prints both matrices are EQUAL
otherwise prints both matrixes are not equal.
4-
-
- for(i0ilt3i) //loop for checking whether two
matrixes are equal or not -
- for(j0jlt3j)
-
- if(matrix1ij!matrix2ij)
-
- count// increment the count value if the
elements are not - equal
-
-
-
- if(count0)
- printf(2 matrixes are equal\n)
- else
- printf(2 matrixes are not equal\n)
-
- includeltstdio.hgt
- int main()
-
- int matrix133,matrix233,i,j,count0
- printf(enter the elements of first 33
matrix\n) - for(i0ilt3i) //rows
-
- for(j0jlt3j)// columns
-
- scanf(d,matrix1ij) //input from the user
-
-
- printf(enter the elements of second 33
matrix\n) - for(i0ilt3i)
-
- for(j0jlt3j)
-
- scanf(d,matrix2ij)
51.2 TO PERFORM SCALAR MULTIPLICATION OF(33)
MATRIX
DESCRIPTION
- Here we can multiply any constant element with
the 33 matrix(or any other matrix).first user
ask the input for matrix along with the constant
variable.
6-
- scanf(d,matrixij)
-
-
- printf(enter the constant element to multiplied
with the matrix\n) - scanf(d,constant)
- for(i0iltrowi)
- //loop for multiplying a constant with the given
matrix -
- for(j0jltcolj)
-
- matrix1ijconstantmatrixij
-
-
- printf(the new matrix is\n)
- for(i0iltrowi)
- //loop for printing the multiplied matrix
- includeltstdio.hgt//preprocessor directive with
header file - int main()
-
- int matrix1010,i,j,n,constant,row,col,matrix1
1010 - printf(enter the number of rows of the array
elements\n) - scanf(d,row)
- printf(enter the number of columns of the array
elements\n) - scanf(d,col)
- printf(enter the matrix elements\n)
- for(i0iltrowi)
- //for row elements
-
- for(j0jltcolj)
- //for column elements
7-
- for(j0jltcolj)
-
- printf( d,matrix1ij)
-
- printf(\n)
-
- //end of main function
81.3 TO FIND THE SUM OF EACH ROW AND COLUMN OF A
33 MATRIX
DESCRIPTION
- First, We have to take(33) matrix,then we need
to find the sum of each elements of the
row1,afterwards sum of each elements of row2 and
then row3, similar method is needed to find the
sum of each elements of columns also afterwards
print all the sums individually.
9- includeltstdio.hgt
- int main()
- int matrix33,i,j,sum,row0,sum1
- printf(enter the elements of the
matrix(33)\n) - for(i0ilt3i)// scanning for rows
-
- for(j0jlt3j)// scanning for columns
-
- scanf(d,matrixij)
-
-
- for(i0ilt3i)//loop for finding the sum of
each rows and columns -
- sum0
- sum10
- for(j0jlt3j)
-
- sumsummatrixij//sum is sum of each rows
- sum1sum1matrixji//sum1 is sum of each
columns -
- printf(sum of rowdd\n,row,sum)//printing
sum of each rows value - printf(sum of columndd\n,row,sum1)//printing
sum of each - column value
- row
-
-
101.4 MULTIPLICATION OF TWO 33 MATRIX
DESCRIPTION
- First we take the two 33 matrixes(or any),
afterwards we need to check the rows of the first
matrix is equal to the columns of the second
matrix, if it is satisfied then only you to
process for further steps otherwise print no
multiplication is possible,if it is satisfied
then only multiply 2 matrix.
11- includeltstdio.hgt
- int main()
-
- int matrix133,matrix233,matrix333,row1
,col1,row2,col2,i,j,k,mul - printf(enter the number of rows of the first
matrix\n) - scanf(d,row1)
- printf(enter the columns of the first
matrix\n) - scanf(d,col1)
- printf(enter the elements of the first
matrix\n) - for(i0iltrow1i)
-
- for(j0jltcol1j)
-
- scanf(d,matrix1ij)
-
-
- printf(enter the number of rows of second
matrix\n) - scanf(d,row2)
- printf(enter the number of columns of second
matrix\n) - scanf(d,col2)
- if(row1!col2) //if rows of the 1st matrix not
equal to second matrix exit program - otherwise continue
- printf(multiplication not possible\n)
- else
-
12- printf(enter the elements of the second
matrix\n) - for(i0iltrow2i)
-
- for(j0jltcol2j)
-
- scanf(d,matrix2ij)
-
-
- printf(multiplication of two matrix is\n)
- for(i0iltrow1i) // matrix multiplication
takes place -
- for(j0jltcol2j)
-
- mul0
- for(k0kltcol1k)
-
- mulmulmatrix1ikmatrix2kj
-
- printf( d,mul)
-
- printf(\n)
-
-
-
131.5 PROGRAM TO STORE 10 NAME INTO 2D ARRAY AND
PRINT THEM
DESCRIPTION
- We declare a 2 dimensional character array of
required size,afterwards we need to store names,
and print them.
14- includeltstdio.hgt
- int main()
-
- char names2020
- int row,col,i,j
- printf(enter the rows and columns of the 2d
array\n) - scanf(d d,row,col)
- printf(enter the names you want to print\n)
- for(i0iltrowi)
-
- scanf(s,namesi0)//user input to store
names -
- for(i0iltrowi)
-
- printf(s\n,namesi0)//printing the names
-
-
151.6 PROGRAM TO SWAP THE TWO ARRAYS
DESCRIPTION
- To swap two strings in c programming we want to
ask user to enter two strings and then make a
temporary variable of same type and then place
elements of string 1 in temp and and elements of
string 2 in 1 and then temp in string 2.
16- includeltstdio.hgt
- int main()
-
- char array110,array210,temp10\0?
- int len10,len20,i,j
- printf(enter the elements of the first
array\n) - scanf(s,array1)
- printf(enter the elements of the second
array\n) - scanf(s,array2)
- while(array1len1!\0')//loop for calculating
the length of the first array -
- len1
-
- printf(the length of the first array is
d\n,len1) - while(array2len2!\0')//loop for calculating
the length of the second array -
- len2
-
- printf(the length of the second array is
d\n,len2) - for(i0iltlen1i)//using temporary variable we
swap the 2 given arrays and - print after the swapping process
-
- tempiarray1i
-
17- for(i0iltlen2i)
-
- array1iarray2i
-
- for(i0iltlen1i)
-
- array2itempi
-
- for(i0iltlen2i)
-
- printf(c,array1i)
-
- printf(\n)
- for(j0jltlen1j)
-
181.7 PROGRAM TO CHECK WHETHER THE GIVEN STRING IS
PALINDROME OR NOT
DESCRIPTION
- A string is palindrome if the reverse of that
string is equal to original string.firstly we
need to declare a character array of some size,
after we need to obtain input string from the
user, and take another array and copy the
reversing order of first string and compare the
both strings if it is equal print as the given
string is palindrome else print the given string
is not an palindrome.
19- includeltstdio.hgt
- includeltstring.hgt
- int main()
-
- char str10,rev10\0?
- int length0,i,j
- printf(enter the string\n)
- scanf(s,str)
- while(strlength!\0')
-
- length
-
- printf(length of the string isd\n,length)
- for(ilength-1,j0igt0,jltlengthi,j)/for
loop for reversing a original string and store it
inanother character array/
-
- revistrj
-
- for(i0iltlengthi)//comparing original string
with the reversing string -
- if(revistri)
- j1
- else
- j0
-
- if(j1)
- printf(palindrome\n)
- else
- printf(not an palindrome\n)
-
201.8 PROGRAM TO INTERCHANGING THE DIAGONALS OF
MATRIX
DESCRIPTION
- Firstly we take the matrix (only square
matrix),afterwards we need to interchange the
left diagonal elements towards right and
vice-versa and print the result of interchanging
the diagonal elements.
21- includeltstdio.hgt
- int main()
-
- int a1010,row,column,i,j,temp
- printf(enter the number of rows and columns of
the matrix\n) - scanf(d d,row,column)
- printf(enter the elements of the matrix\n)
- for(i0iltrowi)
-
- for(j0jltcolumnj)
-
- scanf(d,aij)
-
-
- for(i0iltrowi)
-
- tempaii
- aiiairow-i-1
- airow-i-1temp
-
- for(i0iltrowi)
-
- for(j0jltcolumnj)
-
- printf( d,aij)
-
- printf(\n)
-
-
221.9 WRITE A PROGRAM TO PRINT UPPER TRIANGULAR
MATRIX
DESCRIPTION
- We have to take one square matrix and print the
upper triangular matrix only remaining the term
should be zero or empty.
23- matrixrowcolumnmatrixrowcolumn//assign
the elements as it is if column is - greater or equal,else it is 0
-
- else
-
- matrixrowcolumn0
-
-
-
- for(row0rowlt3row)
-
- for(column0columnlt3column)
-
- printf( d,matrixrowcolumn)
-
- printf(\n)
-
-
- includeltstdio.hgt
- int main()
-
- int matrix33,row,column
- printf(enter the (33) matrix elements\n)
- for(row0rowlt3row)
-
- for(column0columnlt3column)
-
- scanf(d,matrixrowcolumn)
-
-
- for(row0rowlt3row)
-
- for(column0columnlt3column)
-
- if(columngtrow)
-
24Contact Us
Professional Training Institute 32, 38/1, 2rd
Floor,Hosur Main Road, Near Bosch Office,
Bommanahalli, Bengaluru, Karnataka-560068 9189514
22196 www.ptinstitute.in