Monday, August 12, 2019

TWO DIMENSIONAL ARRAY

Through PTInstitute, 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. 

1.1 PROGRAM TO CHECK WHETHER THE TWO(3*3) MATRIXES ARE EQUAL OR NOT

DESCRIPTION:

First we taken the two(3*3) matrixes, afterwards we need to check that all elements inside the two matrixes are equal or not in both matrixes with respect toposition wise also, if all elements with respect to position in two matrixes are equal then prints both matrices are EQUAL otherwise prints both matrixes are not equal.

#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]);
}
}
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”);
}

1.2 TO PERFORM SCALAR MULTIPLICATON OF(3*3) MATRIX

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.
#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
{
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
{
for(j=0;j<col;j++)
{
printf(” %d”,matrix1[i][j]);
}
printf(“\n”);
}
}//end of main function

1.3 TO FIND THE SUM OF EACH ROW AND COLUMN OF A 3*3 MATRIX

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 columns also.afterwards print all the sums individually.

#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++;
}

}

1.4 MULTIPLICATION OF TWO 3*3 MATRIX

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 satisified then only you to process for further steps otherwise print no multiplication is possible,if it is satisified then only multiply 2 matrix.
#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
{
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”);
}
}
}

1.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.
#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
}
}

1.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 temporory 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.
#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 calacuating 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];
}
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++)
{
printf(“%c”,array2[j]);
}
}

1.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 comapare the both strings if it is equal print as the given string is palindrome else print the given string is not an palindrome.

#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′)//while loop to find the length of the string till the element become
{
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 in
another 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”);
}

1.8 PROGRAM TO INTERCHANGING THE DIGONALS OF MATRIX

DESCRIPTION:

Firstly we take the matrix (only square matrix),afterwards we need to interchange the left diagonal elements towards right and viceversa and print the result of interchanging the diagonal elements.

#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”);
}
}

1.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.

#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)
{
matrix[row][column]=matrix[row][column];//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”);
}
}

1.1 PROGRAM TO READ 10 NAMES INTO 2D ARRAY AND SORT THEM BASED ON THE FIRST ELEMENT IN ALPHABETICAL ORDER

DESCRIPTION:

Firstly we can take some of names from the user input and then we have to compare first letter of each names and then use swapping technique if one letter greater than other,after completing the sorting technique, we will get strings of names in alphabetical order.
#include<stdio.h>
#include<string.h>
int main()
{
char names[10][10],temp;
int i,j,k;
printf(“enter the names(10):\n”);
for(i=0;i<10;i++)
{
scanf(“%s”,&names[i][0]);
}
for(k=0;k<10;k++)//loop for swapping until all the elements will be sorting in alphabetical order
{
for(i=0;i<10-1;i++)//swapping all the elements in the array only one time
{
if(names[i][0]>names[i+1][0])/*comparing the first letters of the names and if
greater swap else don’t swap*/
{
for(j=0;j<10;j++)
{
temp=names[i][j];
names[i][j]=names[i+1][j];//swapping
names[i+1][j]=temp;
}
}
}
}
printf(“\n”);
printf(“string name in alphabetical order\n”);//after the swapping technique print the sorted
names
for(i=0;i<10;i++)
{
printf(“%s\n”,&names[i][0]);
}
}