Skip to content

Commit

Permalink
matrix_exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
bcalagoz committed Nov 6, 2022
1 parent fc1a7ae commit 4c285bc
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified codeforwin_c_exercises/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Write a C program to read elements in two matrices and add elements of both matrices.
C program for addition of two matrix.
Matrix addition program in C.
Logic to add two matrix in C programming.
*/

#include<stdio.h>

int main(){

int array1[3][3],array2[3][3];

printf("Enter elements in matrix A of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array1[i][j]);
}

}

printf("Enter elements in matrix B of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array2[i][j]);
}

}

printf("Sum of matrices A+B =\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",(array1[i][j]+array2[i][j]));
}
printf("\n");
}


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Write a C program to read elements in two matrices and find the difference of two matrices.
Program to subtract two matrices in C.
Logic to subtract two matrices in C programming.
*/

#include<stdio.h>

int main(){

int array1[3][3],array2[3][3];

printf("Enter elements in matrix A of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array1[i][j]);
}

}

printf("Enter elements in matrix B of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array2[i][j]);
}

}

printf("Difference of both matrices A-B =\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",(array1[i][j]-array2[i][j]));
}
printf("\n");
}


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Write a C program to read elements in a matrix and perform scalar multiplication of matrix.
C program for scalar multiplication of matrix.
How to perform scalar matrix multiplication in C programming.
Logic to perform scalar matrix multiplication in C program.
*/

#include<stdio.h>

int main(){

int array1[3][3],multiply;

printf("Enter elements in matrix A of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array1[i][j]);
}

}

printf("Enter any number to multiply with matrix A:");
scanf("%d",&multiply);


printf("Resultant matrix c.A =\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",(array1[i][j] * multiply));
}
printf("\n");
}

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Write a C program to read elements in two matrices and multiply them.
Matrix multiplication program in C.
How to multiply matrices in C.
Logic to multiply two matrices in C programming.
*/

#include<stdio.h>

int main(){

int array1[3][3],array2[3][3],array3[3][3];
int sum ;
printf("Enter elements in matrix A of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array1[i][j]);
}

}

printf("Enter elements in matrix B of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array2[i][j]);
}

}

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum = 0;
for (int k = 0; k < 3; k++)
{
sum += (array1[i][k]*array2[k][j]);
}
array3[i][j] = sum;
}

}


printf("Product of matrix A * B =\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",array3[i][j]);
}
printf("\n");
}


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Write a C program to enter elements in two matrices and check whether both matrices are equal or not.
C program to check whether elements of two matrices are equal or not.
Logic to check if two matrices are equal or not in C programming.
*/

#include<stdio.h>

int main(){

int array1[3][3],array2[3][3];
int flag;

printf("Enter elements in matrix A of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array1[i][j]);
}

}

printf("Enter elements in matrix B of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array2[i][j]);
}

}

flag = 1;

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (array1[i][j] != array2[i][j])
{
flag = 0;
break;
}

}
}

if (flag == 1)
{
printf("Matrix A is equal to Matrix B");
}
else
{
printf("Matrix A is NOT equal to Matrix B");
}

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Write a C program to read elements in a matrix and find the sum of main diagonal (major diagonal) elements of matrix.
Find sum of all elements of main diagonal of a matrix.
Logic to find sum of main diagonal elements of a matrix in C programming.
*/
#include<stdio.h>

int main(){

int array[3][3];
int sum = 0;


printf("Enter elements in matrix of size 3x3:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d",&array[i][j]);
}

}

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i == j)
{
sum += array[i][j];
}
}
}

printf("Sum of main diagonal elements = %d\n",sum);

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Write a C program to read elements in a matrix and find the sum of minor diagonal (opposite diagonal) elements.
C program to calculate sum of minor diagonal elements.
Logic to find sum of opposite diagonal elements of a matrix in C programming.
*/

#include<stdio.h>
#define SIZE 3

int main(){

int array[SIZE][SIZE];
int sum = 0;


printf("Enter elements in matrix of size 3x3:");
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
scanf("%d",&array[i][j]);
}

}

for (int i = 0; i < SIZE; i++)
{
for (int j = SIZE-1; j >= 0; j--)
{
if ((i+j)==(SIZE-1) )
{
sum += array[i][j];
}
}
}

printf("Sum of minor elements = %d\n",sum);

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Write a C program to read elements in a matrix and find the sum of elements of each row and columns of matrix.
C program to calculate sum of rows and columns of matrix.
Logic to find sum of each row and columns of a matrix in C programming.
*/
#include<stdio.h>
#define SIZE 3

int main(){

int array[SIZE][SIZE];
int sum;


printf("Enter elements in matrix of size 3x3:");
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
scanf("%d",&array[i][j]);
}

}

for (int i = 0; i < SIZE; i++)
{
sum = 0;
for (int j = 0; j < SIZE; j++)
{
sum += array[i][j];
}
printf("Sum of elements of Row %d = %d\n",i+1,sum);
}

for (int i = 0; i < SIZE; i++)
{
sum = 0;
for (int j = 0; j < SIZE; j++)
{
sum += array[j][i];
}
printf("Sum of elements of Column %d = %d\n",i+1,sum);
}

return 0;
}

0 comments on commit 4c285bc

Please sign in to comment.