SUBJECT: PROGRAMMING FOR PROBLEM SOLVING
PAPER CODE: ES-CS291
NAME: ARITRA
SANDIP CHATTERJEE
SHAH
UNIVERSITY ROLL NO: 25300120006
25300120023
REGISTRATION NO.: 202530100110029
202530100110012
DEPARTMENT: BTech CSE
ASSIGNMENT NO.: Assignment 9
DATE: 25/06/2021
29/06/2021
TECH.ASST. FACULTY
1. Write a C program to show the diagonal element of 2d array.
Algorithm:
1. Declare all the needed variable
2.Take user input for the no. of row and column as r and c
3.if r!=c, print “Diagonal does not exits” and go to step 10
4.Take user input of the array elements
5.Print the array elements
6.Print, “Main Diagonal of the Array =”
7.i=0 and if i<r go to step 8 or go to step 10
8.pirnt, ar[i][i]
9.i++ and go to step 7
10.return 0;
Code:
#include <stdio.h>
int main()
{
int i,j,r,c,ar[25][25];
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
if(r!=c)
{
printf("Diagonal does not exits");
return 0;
}
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",ar[i][j]);
printf("\n");
}
printf("\nMain Diagonal of the Array = ");
for(i=0;i<r;i++)
printf("%d ",ar[i][i]);
return 0;
}
Output:
1. 2. Write a C program to transpose of the 2d array.
Algorithm:
1.Declear all the needed variable
2.take user input for the no. of rows and column
3.take user input of the array elements
4.Print, “The Array before transpose =” and the array elements
5.i=0 and if i<r is true go to step 6 or go to step 10
6.j=0 and if j<c is true go to step 7 or go to step 9
7.arr[j][i]=ar[i][j]
8.j++ and go to step 6
9.i++ and go to step 5
10.Print, “The Array after transpose =” and the array elements
11.return 0;
Code:
#include <stdio.h>
int main()
{
int i,j,r,c,ar[25][25],arr[25][25];
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array before transpose = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",ar[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[j][i]=ar[i][j];
}
}
printf("\nThe Array after transpose = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
return 0;
}
Output:
1. 3. Write a C program to implement column addition of 2d array.
Algorithm:
1.Declear all the needed variables
2.Take user input for the no. of rows and columns
3.Take user input for the array elements
4. Print, “The Array =” and the array elements
5.i=0 and if i<r is true go to step 6 or go to step 12
6.sum=0
7.j=0 and if j<c is true go to step 8 or go to step 10
8.sum=sum+ar[j][i]
9.j++ and go to step 7
10.Print, Sum of (i+1)th column = sum
11.i++ and go to step 5
12.return 0
Code:
#include <stdio.h>
int main()
{
int i,j,r,c,ar[25][25],sum=0;
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",ar[i][j]);
}
printf("\n");
}
printf("\n\n");
for(i=0;i<r;i++)
{
sum=0;
for(j=0;j<c;j++)
{
sum=sum+ar[j][i];
}
printf("\nSum of Column %d is = %d",i+1,sum);
}
return 0;
}
Output:
1. 4. Write a C program to implement row addition of 2d array.
Algorithm:
1.Declear all the needed variables
2.Take user input for the no. of rows and columns
3.Take user input for the array elements
4. Print, “The Array =”
5.i=0 and if i<r is true go to step 6 or go to step 12
6.sum=0
7.j=0 and if j<c is true go to step 8 or go to step 10
8.sum=sum+ar[i][j] and print ar[i][j]
9.j++ and go to step 7
10.Print, “==> Sum of Row (i+1)th = sum”
11.i++ and go to step 5
12.return 0
Code:
#include <stdio.h>
int main()
{
int i,j,r,c,ar[25][25],sum=0;
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array = \n");
for(i=0;i<r;i++)
{
sum=0;
for(j=0;j<c;j++)
{
sum=sum+ar[i][j];
printf("%d ",ar[i][j]);
}
printf(" ==> Sum of Row %d = %d\n",i+1,sum);
}
return 0;
}
Output:
1. 5. Write a C program to calculate norm of matrix.
Algorithm:
1.Declear all the needed variables and float norm =0
2.Take user input for the no. of rows and columns
3.Take user input for the array elements
4. Print, “The Array =”
5.i=0 and if i<r is true go to step 6 or go to step 12
6.sum=0
7.j=0 and if j<c is true go to step 8 or go to step 10
8.norm=norm+pow(ar[i][j],2) and print ar[i][j]
9.j++ and go to step 7
10.Print, “\n”
11.i++ and go to step 5
12.norm=sqrt(norm)
13.Print, “Norm of the Array = norm”
14. return 0
Code:
#include <stdio.h>
#include <math.h>
int main()
{
int i,j,r,c,ar[25][25];
float norm=0;
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",ar[i][j]);
norm=norm+pow(ar[i][j],2);
}
printf("\n");
}
norm=sqrt(norm);
printf("\n\nNorm of the Matrix = %.3f",norm);
return 0;
}
Output:
1. 6. Write a C program to show the reverse diagonal element of 2d array.
Algorithm:
1. Declare all the needed variable
2.Take user input for the no. of row and column as r and c
3.if r!=c, print “Diagonal does not exits” and go to step 10
4.Take user input of the array elements
5.Print the array elements
6.Print, “Reverse Diagonal of the Array =
7.i=0,j=c-1 and if i<r && j>=0 go to step 8 or go to step 10
8.pirnt, ar[i][i]
9.i++ and j-- and go to step 7
10.return 0;
Code:
#include <stdio.h>
int main()
{
int i,j,r,c,ar[25][25];
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
if(r!=c)
{
printf("Digonal does not exits");
return 0;
}
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",ar[i][j]);
printf("\n");
}
printf("\nReverse Diagonal of the Array = ");
for(i=0,j=c-1;i<r,j>=0;i++,j--)
printf("%d ",ar[i][j]);
return 0;
}
Output:
1. 7. Write a C program to trace of the matrix.
Algorithm:
1. Declare all the needed variable
2.Take user input for the no. of row and column as r and c
3.if r!=c, print “Diagonal and trace does not exits” and go to step 11
4.Take user input of the array elements
5.Print the array elements
6.Print, “Main Diagonal of the Array =”
7.i=0 and if i<r go to step 8 or go to step 10
8.trace=trace+ar[i][i];
9.i++ and go to step 7
10.Print, “Trace of the array = trace”
11.return 0
Code:
#include <stdio.h>
int main()
{
int i,j,r,c,ar[25][25],trace=0;
printf("Enter the no. of Row(<25) & Column(<25) separated by a space: ");
scanf("%d %d",&r,&c);
if(r!=c)
{
printf("Diagonal and Trace does not exits");
return 0;
}
printf("\nEnter the elements in the %d x %d Array:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&ar[i][j]);
printf("\nThe Array = \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",ar[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
trace=trace+ar[i][i];
printf("\nTrace of the Array = %d",trace);
return 0;
}
Output:
1. 8. Write a C program to calculate matrix multiplication
Algorithm:
1. Declare all the needed variable
2.Take user input for the no. of row and column of array1 as r1 and c1
3.Take user input for the no. of row and column of array2 as r2 and c2
4.if r2!=c1, print “Matrix Multiplication not possible!!” and go to step 15
5.Take user input of the array1 and array2 elements
6.Print the array1 and array2 elements
7.i=0 and if i<r1 is true go to step 8 or go to step 14
8.j=0 and if j<c2 is true go to step 9 or go to step 13
9. k=0 and if k<c1 is true go to step 10 or go to step 12
10. arr[i][j]+=ar1[i][k]*ar2[k][j]
11.k++ and go back to step 9
12.j++ and go back to step 8
13.i++ and go back to step 7
14.Print, “Resultant Array =” and print the arr array element.
15.return 0
Code:
#include <stdio.h>
int main()
{
int i,j,k,r1,r2,c1,c2;
int ar1[5][5],ar2[5][5],arr[5][5]={0};
printf("Enter the no. of Row1(<5) & Column2(<5) separated by a space: ");
scanf("%d %d",&r1,&c1);
printf("\nEnter the no. of Row2(<5) & Column2(<5) separated by a space: ");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
{
printf("\n\nMatrix Multiplication not possible!!");
return 0;
}
printf("\nEnter the elements in the %d x %d Array1 :\n",r1,c1);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&ar1[i][j]);
printf("\nEnter the elements in the %d x %d Array2 :\n",r2,c2);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&ar2[i][j]);
printf("\nThe Array1 = \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d ",ar1[i][j]);
printf("\n");
}
printf("\nThe Array2 = \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d ",ar2[i][j]);
printf("\n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
arr[i][j]+=ar1[i][k]*ar2[k][j];
}
}
}
printf("\nResultant Array = \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
return 0;
}
Output: