Commit 2f19a3a9 by otsall

matrices

parent 327044a5
Showing with 100 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
int main (){
int x, y, size, byhand, diagonal1 = 1, diagonal2 = 1;
printf("input the size of the matrices\n");
scanf("%d", &x);
y = x;
size = x;
int matrix1[x][y],
matrix2[x][y],
resultingMatrix[x][y];
printf("type 1 to intput the elements of the matrices by hand. other inputs will generate random elements\n");
scanf("%d", &byhand);
if (byhand != 1){
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
matrix1[x][y] = rand() % 500;
matrix2[x][y] = rand() % 500;
}
}
}else{
printf("enter elements of first matrix\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
scanf("%d", &matrix1[x][y]);
}
}
printf("enter elements of the second matrix\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
scanf("%d", &matrix2[x][y]);
}
}
}
printf("first matrix:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
printf("%d, ", matrix1[x][y]);
}
printf("\n");
}
printf("second matrix:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
printf("%d, ", matrix2[x][y]);
}
printf("\n");
}
printf("sum of the matrices is as follows:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
resultingMatrix[x][y]=matrix1[x][y]+matrix2[x][y];
printf("%d, ", resultingMatrix[x][y]);
}
printf("\n");
}
printf("substraction of the matrices is as follows:\n");
for(x = 0; x < size; x++){
for(y=0; y < size; y++){
resultingMatrix[x][y]=matrix1[x][y]-matrix2[x][y];
printf("%d, ", resultingMatrix[x][y]);
}
printf("\n");
}
printf("the sum of the multiplication of the diagonals of the first matrix:\n");
//using byhand because i dont want to declare another variable. it acts as the sum of the multiplication of the diagonals.
for(x = 0; x < size; x++){
y = x;
diagonal1 = matrix1[x][y] * diagonal1;
}
for(x=0; x < size; x++){
y = size - 1 - x;
diagonal2 = matrix1[x][y] * diagonal2;
}
byhand = diagonal1 + diagonal2;
printf("%d\n", byhand);
diagonal1=1;
diagonal2=1;
printf("the sum of the multiplication of the diagonals of the second matrix:\n");
for(x = 0; x < size; x++){
y = x;
diagonal1 = matrix2[x][y] * diagonal1;
}
for(x=0; x < size; x++){
y = size - 1 - x;
diagonal2 = matrix2[x][y] * diagonal2;
}
byhand = diagonal1 + diagonal2;
printf("%d\n", byhand);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment