240 likes | 266 Views
Hi everyone, here we are presenting a presentation about 2-dimensional array. An array of arrays is known as a 2-dimensional array. The two-dimensional array in programmingu00a0is also known as the matrix. A matrix can be represented as a table of rows and columns. We demonstrate how to store the elements entered by the user in a 2d array and how to display the elements of a two-dimensional array. The individual elements of the above array can be accessed by using two subscript instead of one. The first subscript denotes row number and second denotes column number. As we can see in the above image both rows and columns are indexed fromu00a00. So the first element of this array is atu00a0a[0][0]u00a0and the last element is atu00a0a[1][2]. At Embedded training institute, teach 2-dimensional array very clear step by steps.<br>Read More: https://www.ptinstitute.in
E N D
TWO DIMENSIONAL ARRAY By Professional Training Institute Professional Embedded Training Institute in Bangalore
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 two-dimensional arrays .so an array of arrays is known as 2d array. Through our website you can learn more. TWO DIMENSIONAL ARRAY can have
DESCRIPTION First 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. we taken the two(3*3) 1.1 PROGRAM TO CHECK WHETHER THE TWO(3*3) MATRICES ARE EQUAL OR NOT
} } for(i=0;i<3;i++) //loop for checking whether two matrixes are equal or not { for(j=0;j<3;j++) { if(matrix1[i][j]!=matrix2[i][j]) { count++;// increment the count value if the elements are not equal } } } if(count==0) printf(“2 matrixes are equal\n”); else printf(“2 matrixes are not equal\n”); } #include<stdio.h> int main() { int matrix1[3][3],matrix2[3][3],i,j,count=0; printf(“enter the elements of first 3*3 matrix\n”); for(i=0;i<3;i++) //rows { for(j=0;j<3;j++)// columns { scanf(“%d”,&matrix1[i][j]); //input from the user } } printf(“enter the elements of second 3*3 matrix\n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf(“%d”,&matrix2[i][j]); 10 mins 15 mins
DESCRIPTION Here we can multiply any constant element with the 3*3 matrix(or any other matrix).first user ask the input for matrix along with the constant variable. 1.2 TO PERFORM SCALAR MULTIPLICATION OF(3*3) MATRIX
{ scanf(“%d”,&matrix[i][j]); } } printf(“enter the constant element to multiplied with the matrix\n”); scanf(“%d”,&constant); for(i=0;i<row;i++) //loop for multiplying a constant with the given matrix { for(j=0;j<col;j++) { matrix1[i][j]=constant*matrix[i][j]; } } printf(“the new matrix is\n”); for(i=0;i<row;i++) //loop for printing the multiplied matrix #include<stdio.h>//preprocessor directive with header file int main() { int matrix[10][10],i,j,n,constant,row,col,matrix1[10] [10]; 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(i=0;i<row;i++) //for row elements { for(j=0;j<col;j++) //for column elements 10 mins 15 mins
{ for(j=0;j<col;j++) { printf(” %d”,matrix1[i][j]); } printf(“\n”); } }//end of main function 10 mins 15 mins
DESCRIPTION First, We have to take(3*3) 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 afterwards print all the sums individually. 1.3 TO FIND THE SUM OF EACH ROW AND COLUMN OF A 3*3 MATRIX columns also
#include<stdio.h> int main() int matrix[3][3],i,j,sum,row=0,sum1; printf(“enter the elements of the matrix(3*3)\n”); for(i=0;i<3;i++)// scanning for rows { for(j=0;j<3;j++)// scanning for columns { scanf(“%d”,&matrix[i][j]); } } for(i=0;i<3;i++)//loop for finding the sum of each rows and columns { sum=0; sum1=0; for(j=0;j<3;j++) { sum=sum+matrix[i][j];//sum is sum of each rows sum1=sum1+matrix[j][i];//sum1 is sum of each columns } printf(“sum of row%d=%d\n”,row,sum);//printing sum of each rows value printf(“sum of column%d=%d\n”,row,sum1);//printing sum of each column value row++; } } 10 mins 15 mins
DESCRIPTION First we take the two 3*3 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 otherwise print no multiplication is possible,if it is satisfied then only multiply 2 matrix. 1.4 MULTIPLICATION OF TWO 3*3 MATRIX further steps
#include<stdio.h> int main() { int matrix1[3][3],matrix2[3][3],matrix3[3][3 ],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(i=0;i<row1;i++) { for(j=0;j<col1;j++) { scanf(“%d”,&matrix1[i][j]); } } 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 { 10 mins 15 mins
printf(“enter the elements of the second matrix\n”); for(i=0;i<row2;i++) { for(j=0;j<col2;j++) { scanf(“%d”,&matrix2[i][j]); } } printf(“multiplication of two matrix is\n”); for(i=0;i<row1;i++) // matrix multiplication takes place { for(j=0;j<col2;j++) { mul=0; for(k=0;k<col1;k++) { mul=mul+matrix1[i][k]*matrix2[k][j]; } printf(” %d”,mul); } printf(“\n”); } } } 10 mins 15 mins
DESCRIPTION We declare a 2 dimensional character array size,afterwards we need to store names, and print them. of required 1.5 PROGRAM TO STORE 10 NAME INTO 2D ARRAY AND PRINT THEM
#include<stdio.h> int main() { char names[20][20]; 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(i=0;i<row;i++) { scanf(“%s”,&names[i][0]);//user input to store names } for(i=0;i<row;i++) { printf(“%s\n”,&names[i][0]);//printing the names } } 10 mins 15 mins
DESCRIPTION To 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. swap two strings in c 1.6 PROGRAM TO SWAP THE TWO ARRAYS
#include<stdio.h> int main() { char array1[10],array2[10],temp[10]=”\0″; int len1=0,len2=0,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(array1[len1]!=’\0′)//loop for calculating the length of the first array { len1++; } printf(“the length of the first array is %d\n”,len1); while(array2[len2]!=’\0′)//loop for calculating the length of the second array { len2++; } printf(“the length of the second array is %d\n”,len2); for(i=0;i<len1;i++)//using temporary variable we swap the 2 given arrays and print after the swapping process { temp[i]=array1[i]; } 10 mins 15 mins
printf(“%c”,array2[j]); } } for(i=0;i<len2;i++) { array1[i]=array2[i]; } for(i=0;i<len1;i++) { array2[i]=temp[i]; } for(i=0;i<len2;i++) { printf(“%c”,array1[i]); } printf(“\n”); for(j=0;j<len1;j++) { 10 mins 15 mins
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. 1.7 PROGRAM TO CHECK WHETHER THE GIVEN STRING IS PALINDROME OR NOT
#include<stdio.h> #include<string.h> int main() { char str[10],rev[10]=”\0″; int length=0,i,j; printf(“enter the string\n”); scanf(“%s”,str); while(str[length]!=’\0′) { length++; } printf(“length of the string is=%d\n”,length); for(i=length-1,j=0;i>=0,j<length;i–,j++)/* for loop for reversing a original string and store it inanother character array*/ { rev[i]=str[j]; } for(i=0;i<length;i++)//comparing original string with the reversing string { if(rev[i]==str[i]) j=1; else j=0; } if(j==1) printf(“palindrome\n”); else printf(“not an palindrome\n”); } 10 mins 15 mins
DESCRIPTION Firstly we take the matrix (only square matrix),afterwards we need to interchange the left diagonal elements towards vice-versa and print the result of interchanging elements. 1.8 PROGRAM TO INTERCHANGING THE DIAGONALS OF MATRIX right and the diagonal
#include<stdio.h> int main() { int a[10][10],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(i=0;i<row;i++) { for(j=0;j<column;j++) { scanf(“%d”,&a[i][j]); } } for(i=0;i<row;i++) { temp=a[i][i]; a[i][i]=a[i][row-i-1]; a[i][row-i-1]=temp; } for(i=0;i<row;i++) { for(j=0;j<column;j++) { printf(” %d”,a[i][j]); } printf(“\n”); } } 10 mins 15 mins
DESCRIPTION We have to take one square matrix and print the upper triangular matrix only remaining the term should be zero or empty. 1.9 WRITE A PROGRAM TO PRINT UPPER TRIANGULAR MATRIX
matrix[row][column]=matrix[row][colum n];//assign the elements as it is if column is greater or equal,else it is 0 } else { matrix[row][column]=0; } } } for(row=0;row<3;row++) { for(column=0;column<3;column++) { printf(” %d”,matrix[row][column]); } printf(“\n”); } } #include<stdio.h> int main() { int matrix[3][3],row,column; printf(“enter the (3*3) matrix elements\n”); for(row=0;row<3;row++) { for(column=0;column<3;column++) { scanf(“%d”,&matrix[row][column]); } } for(row=0;row<3;row++) { for(column=0;column<3;column++) { if(column>=row) { 10 mins 15 mins
Professional Training Institute 32, 38/1, 2rd Floor,Hosur Main Road, Near Bosch Office, Bommanahalli, Bengaluru, Karnataka-560068 +918951422196 www.ptinstitute.in Contact Us