It's a c++ program that takes input for 3*3 matrices and displays the sum and product of those matrices. Now I have improved the program and it's working as expected, so is there anything that can be improved in this program.
#include <stdio.h>
//function prototypes
To get the input for an array.
void getarr(int *x);
To add the two matrices.
void addm(int *x, int *y, int *z);
To print an array.
void displaym(int *x);
To multiply two arrays.
void multm(int *x, int *y, int *z);
main function
int main() {
//declaring arrays for matrices
int a[3][3];
int b[3][3];
int c[3][3];
int d[3][3];
//getting input from user
printf("\nEnter nine numbers as the values for first matrix:\n");
getarr(a[0]);
printf("\nThe matrix you entered is:\n");
displaym(a[0]);
printf("\nEnter nine numbers as the values for second matrix:\n");
getarr(b[0]);
printf("\nThe matrix you entered is:\n");
displaym(b[0]);
//calling function for addition
addm(a[0], b[0], c[0]);
//calling function for multiplication
multm(a[0], b[0], d[0]);
//printing the matrices
printf("\nThe sum of the matrices is:\n");
displaym(c[0]);
printf("\nThe product of the matrices is:\n");
displaym(d[0]);
return 0;
}
definition of functions
void getarr(int *x) {
for (int j = 0; j < 9; j++) {
printf("%d:", j);
scanf("%d", x);
x++;
}
}
void addm(int *x, int *y, int *z) {
for (int i = 0; i < 9; i++) {
*z = *x + *y;
z++;
x++;
y++;
}
}
void multm(int *x, int *y, int *z) {
for(int j=0;j<3;j++){
for (int i = 0; i < 3; i++, z++) {
*z = (*x++)*(*y)+(*x++)**(y + 3)+(*x)**(y + 6);
x -= 2, y += 1;
}
x += 3, y -= 3;
}
}
void displaym(int *x) {
printf("\n\n");
for (int i = 0; i < 9; i++) {
printf("%d ", *x++);
if (i == 2 || i == 5)
printf("\n");
}
}