mercredi 18 février 2015

matrix multiplication in C - structures used


Vote count:

0




I've searched for a similar situation but have drawn a blank so I'm posting my problem here:



void mtxmulti(struct matrix mAx, struct matrix mBx) {
struct matrix mCx;
printf("A * B:\n");
if ((mAx.nrows == mBx.nrows) && (mAx.ncols == mBx.ncols)) {
for (mAx.row = 0, mBx.row; mAx.row < mAx.nrows, mBx.row < mBx.nrows; mAx.row++, mBx.row++) {
for (mAx.col = 0, mBx.col; mAx.col < mAx.ncols, mBx.col < mBx.ncols; mAx.col++, mBx.col++) {
mCx.matrix[mCx.row][mCx.col] += mAx.matrix[mAx.row][mAx.col] * mBx.matrix[mBx.row][mAx.col];
}
}
mtxpr1t(mCx); /*If successful, prints the matrix*/
}


What you see here is my function to multiply two matrices together, however it crashes when I try to run it. I'm being told that 'mCx' is uninitialized and because of this won't work.


Other useful parts of the code:



struct matrix {
int** matrix;
int row, col, nrows, ncols;
};
int main(void) {
struct matrix A, B;
printf("Enter the number of rows and columns for A: ");
scanf("%d %d", &A.nrows, &A.ncols);
A.matrix = alloc(A.nrows, A.ncols); /*Allocates array*/
. /*This part of code is
. just entering the values
. of the matrix*/
printf("Enter the number of rows and columns for B: ");
scanf("%d %d", &A.nrows, &B.ncols);
A.matrix = alloc(A.nrows, A.ncols); /*Allocates array*/
. /*This part of code is
. just entering the values
. of the matrix*/
}


FYI: I hadn't needed to initialize matrix A and B, although I did allocate space for them as seen in the section above.



asked 3 mins ago







matrix multiplication in C - structures used

Aucun commentaire:

Enregistrer un commentaire