# Thread: Matrix Mulitplication

1. ## Matrix Mulitplication

HI, im a newbie to C programming, having just started doing it as part of my course at uni, and have been set a project to write a C program to multiply two matrices of varying orders up to 4x4. So far ive pegged how to get the user to input the order of the matrices and from this create 3 arrays to store the matrix data. But now i have the problem of how do i code for the user to actually enter numbers into these arrays esp for diff size matrices, ideally im just looking for it printf out enter 1x2 1x2 etc. Secondly any hints on how to implement to matrix mulit formula into C would be apprciated. This is what i have so far, though not sure about my arrays
Code:
```#include <stdio.h>
#define MATRIX_A
#define MATRIX_B
#define MATRIX_C

int main()
{
int R = 0, C = 0, C2 = 0;

printf("This program will multiply 2 matrices (up to the order or 4x4),and display the result\n");
do {
printf("Enter the number of rows in matrix A\n");

scanf ("%d", &R);
if (R>4)
{
printf("Number or rows too great\n");
}

} while (R>4);
do {
printf("Enter the number of colums in matrix A\n");

scanf ("%d", &C);
if (C>4)
{
printf("number of colums too great\n");
}

} while (C>4);
do {
printf("Enter the number of colums in Matrix B\n");

scanf ("%d", &C2);
if (C2>4)
{
printf("number of colums too great\n");
}
} while (C2>4);

int Matrix_A [R] [C]
int Matrix_B [C] [C2]
int Matrix_C [R] [C2]```
any help would be much appricaited,
P.S did search about this but didnt find nething i could understand really, like i said im a total newbie

2. Since this is for a course you are taking, I can only assume they've been teaching what you need to know to write such a program. At the very least, reading and understanding the course text should give you what you need.

You will have a hard time getting someone to do your home work for you on these boards (but some do it anyway, unfortunately).

If you post a specific problem with how to approach a design, trouble with a piece of code or a compiler error - this forum will hop on it with a quickness.

Assuming you've compiled the code you've posted, you already know that your Matrix_x declarations won't compile. When you declare the size of an array (or matrix) in C, the expression within [] must be constant. Since you know you won't have a matrix bigger than 4x4, you can go ahead and declare your matrix's as 4x4. You know the true limits of the matrix's in R, C, and C2.

You are going to have to use loops to perform the input and the multiplication itself. I'd suggest you look up "for" loops. As for how do you enter the matrix data - I'll suggest something like this:
Code:
```Enter row 1 of matrix A (3 columns): 1 2 3
Enter row 2 of matrix A (3 columns): 4 5 6
<etc...>```
Where everything after the ":" is input from the user.

Use code tags next time you post code and welcome to the C Boards.

gg

3. Thanks for the help, sorry about the code tags, didnt realise at first but hoped it has come up in a re-edit? Anyhow i solved the input method (i think) using do while loops and can happily input values into my matrix array , however is there a way to display the array using printf in a nice square display ( coded for different size matrices). I attempted to do this using a for loop but it would only print one line of elements that are the diagonal elements from top left to right. Heres what ive done so far
Code:
```include <stdio.h>
#define MAX_R 4
#define MAX_C 4
#define MAX_C2 4

int main()
{
int R = 0, C = 0, C2 = 0;
int r_i = 0, c_i = 0, c2_i = 0;
int Matrix_A [MAX_R] [MAX_C];
int x = 0, y = 0;
printf("This program will multiply 2 matrices (up to the order or 4x4),and display the result\n");
do {
printf("Enter the number of rows in matrix A\n");

scanf ("%d", &R);
if (R>4)
{
printf("Number or rows too great\n");
}

} while (R>4);
do {
printf("Enter the number of colums in matrix A\n");

scanf ("%d", &C);
if (C>4)
{
printf("number of colums too great\n");
}

} while (C>4);
do {
printf("Enter the number of colums in Matrix B\n");

scanf ("%d", &C2);
if (C2>4)
{
printf("number of colums too great\n");
}
} while (C2>4);

do  {
printf("\nFor row %d of Matrix A",r_i +1);
while(c_i < (C)) {
printf("\n\tEnter the value for column %d :",c_i +1);

scanf("%d",&Matrix_A [r_i] [c_i]);
++c_i;
}
c_i = 0;
++r_i;
}while (r_i < R);

for(x=0, y=0; x<R, y<C; ++x, ++y)
{

printf("%3d",Matrix_A [x] [y]);
if ( x != 1 && x !=2 && x !=3 && x !=4 )
continue;
printf("\n");

}

return (0);

}```
In response to it being for a course, yes i know i have the material avalible to learn this from, its just its not presented in a reader friendly manner and actually having somone point me towards the right area helps a great deal.
So any pointers would be apprciated ,

P.S congrats on a great board, good design and obvvious hogh participation

4. Ok the for loop out and it now displays all the elements but still cant get them in a nice matrix square, tried newling to get the effect but not with sucess, also now matrix A disply runs into the start of entering matrix B, can some1 suggest when i need to tell the printf to newline? thanks for all the help so far, its been brilliant, im learning more C here then i did in 10 weeks of course lectures !
so far:
Code:
```#include <stdio.h>
#define MAX_R 4
#define MAX_C 4
#define MAX_C2 4
#define MAX_R2 4
int main()
{
int R = 0, C = 0, R2 = 0, C2 = 0;
int r_i = 0, c_i = 0, c2_i = 0, r2_i = 0;
int Matrix_A [MAX_R] [MAX_C];
int Matrix_B [MAX_R2] [MAX_C2];
int Matrix_C [MAX_R] [MAX_C2];
int x = 0, y = 0;
printf("This program will multiply 2 matrices (up to the order or 4x4),and display the result\n");
do {
printf("Enter the order of Matrix A in the format NxN:\n");

scanf ("%dx%d",&R ,&C);
if (R>4 || C>4)
{
printf("Order too great\n");
}

} while (R>4 || C>4);

do  {
printf("\nFor row %d of Matrix A",r_i +1);
while(c_i < C) {
printf("\n\tEnter the value for column %d :",c_i +1);

scanf("%d",&Matrix_A [r_i] [c_i]);
++c_i;
}
c_i = 0;
++r_i;
}while (r_i < R);

for(x=0; x<R; x++){
for(y=0; y<C; y++)
{

printf("\t\n%3d",Matrix_A [x] [y]);
if ( (x == 0 && y == 0) ||  (x ==1 && y == 0))

printf("\n");

}
do {
printf("Enter the order of Matrix B in the format NxN:\n");
scanf ("%dx%d",&R2 ,&C2);
if (R2>4 || C2>4)
{
printf("Order tpp great\n");
}
}while (R2>4 || C2>4);
do {
printf("\nFor row %d of Matrix B",r2_i +1);
while(c2_i < C2) {
printf("\n\tEnter the value for column %d :",c2_i +1);

scanf("%d",&Matrix_B [r2_i] [c2_i]);
++c2_i;
}
c2_i = 0;
++r2_i;
}while (r2_i < R2);

for(x=0; x<R2; x++){
for(y=0; y<C2; y++)
{
printf("%3d",Matrix_B [x] [y]);

}
}

return (0);

}
}```

5. I hate to sound like a jerk, but it makes it easier to spot problems when you just post the piece of code that you're having trouble with and anything necessary to understanding that snippet, instead of posting the entire program each time...now, I'm not going to write your code for you, Salem pointed you in the right direction. But you need to understand how to multiply matricies, so I guess I can give you an example...

Code:
```[a   b   c]      [j   k   l]
[d   e   f]   *  [m   n   o]  =
[g   h   i]      [p   q   r]

[aj+bm+cp  ak+bn+cq  al+bo+cr]
[dj+em+fp  dk+en+fq  dl+eo+fr]
[gj+hm+ip  gk+hn+iq  gl+ho+ir]```
Happy programming

6. Sorry about posting whole code, just thought in case i had cocked it up in another part ppl could then see. ok the problem i have is to prinf the matrices and disply them properly so far
im using
Code:
```for(x=0; x<R; x++){
for(y=0; y<C; y++)
{

printf("%3d",Matrix_A [x] [y]);```
to try and shuffle through the array values and print each in turn. however it either prints only one row or column and runs into the next part of code even with \n inserts on next piece of code

secondly i thought the following for the mat multiply formula and it seems tp work ( but with not being able to display the matrices properly hard to tell).
Code:
```for(X=0; X<R; ++X){
for(Y=0; Y<C2; ++Y){
for(c_i=0; c_i<C; ++c_i){
for(r2_i=0; r2_i<R2; ++r2_i){

Matrix_C [X] [Y] = ((Matrix_A [X] [c_i] * Matrix_B [r2_i] [Y]) + (Matrix_A [X] [c_i +1] * Matrix_B [r2_i +1] [Y]));
}
}
}
}```
where X Y is the element your calculating R and C2 are orders of the original matrices and r_i c_i are just indexs

Popular pages Recent additions