____________________________________________________________________________
Programming For Problem Solving Lab
WEEK-8
NAME :R.DEEKSHITH DATE : 7.11.2024
HTNO : 24R21A05EA SECTION :D
BRANCH :CSE
PROBLEM STATEMENT:
1. You are given two square matrices, A and B, of size m x n. Your
task is to write a program that subtracts matrix B from
matrix A and prints the resulting matrix.
ANALYSIS:
Input : r,c,a
Logic : diff[i][j]=a[i][j]-b[i][j];
Output : Print the resulting matrix after subtracting matrix B from
matrix A.
C-program:
#include<stdio.h>
int main()
{
int r,c,a[10][10],b[10][10],i,j,diff[10][10];
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
diff[i][j]=a[i][j]-b[i][j];
printf("%d ",diff[i][j]);
}
printf("\n");
}
return 0;
}
You are given a square matrix of size n×nn×n. Your task is to
interchange the two diagonals of the matrix and print the updated
matrix.
ANALYSIS:
Input : r,c;
Logic : matrix[i][i]=matrix[i][r-i-1];
Output : Print the updated n×nn×n matrix after interchanging the
diagonals.
C_PROGRAM:
#include <stdio.h>
int main() {
int r,c;
scanf("%d %d ", &r,&c);
if(r!=c)
{
printf("The given order is not of a square matrix");
}
else {
int matrix[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (int i = 0; i < r; i++) {
int temp = matrix[i][i];
matrix[i][i]=matrix[i][r-i-1];
matrix[i][r-i-1]=temp;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf(" %d", matrix[i][j]);
}
printf("\n");
}
}
return 0;
}
3) You are given two matrices, AA and BB, each of size r×cr×c. Your task is to
determine whether the two matrices are equal or not.
ANALYSIS:
Input : int a[10][10],i,j,r,c,
Logic : if(a[i][j] != b[i][j]){
printf("Not Equal");
Output : Print "Equal" if the two matrices are equal.
C-PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],i,j,r,c,b[10][10];
scanf("%d%d", &r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j] != b[i][j]){
printf("Not Equal");
exit(0);
}
}
}
printf("Equal");
}