Advertisement

help me with my C code

Started by February 23, 2003 06:32 PM
2 comments, last by novice_programer 21 years, 8 months ago
can some one help me, I''ve written code to multiply 2 matrices, but my code crashes immediately after taking the row of the first matrix, I''m so frustrated right now, any help is greatly appreciated.... #include <stdio.h> #include <stdlib.h> // One dimensional limitation only const int MAX_COL = 30; struct matrix { int y; // Rows int x; // Columns int (*d)[MAX_COL]; }; void inputMatrix(matrix* m) { int x,y; m->x = 0; m->y = 0; while(m->y<=0) { printf("Enter the Row :"); scanf("%d", m->y); printf("Still in here"); } printf("B4 while loop"); while(m->x<=0 || m->x>MAX_COL) { printf("Enter the Columns"); scanf("%d", m->x); } m->d = new int[m->y][MAX_COL]; for(y=0; yy; y++) for(x=0; xx; x++) { printf("%d%d",y , x); scanf("%d",(m->d)[y][x]); } } void destroyMatrix(matrix* m) { m->x=0; m->y=0; delete [](m->d); } void displayMatrix(matrix* m) { int x,y; printf("\nRow dimension: %d\n", m->y); printf("\nColumns dimension: %d\n", m->x); for(y=0; yy; y++) { for(x=0; xx; x++) { printf("%d\n", (m->d)[y][x]); } printf("\n"); } } void multiplyMatrix(matrix* a, matrix* b, matrix* r) { int x,y,i,temp; if(a->x != b->y) { printf("\nCan NOT multiply these matrixes!!!\n"); r->x = 0; r->y = 0; r->d = NULL; } r->y = a->y; r->x = b->x; r->d = new int[a->y][MAX_COL]; for(y=0; yy; y++) for(x=0; xx; x++) { temp = 0; for(i=0; ix;i++) { temp += (a->d)[y] * (b->d)[x]; } (r->d)[y][x] = temp; } } int main() { matrix a,b,r; printf("\nInput Matrix A\n"); inputMatrix(&a); printf("\nInput Matrix B\n"); inputMatrix(&b); printf("\nDisplaying Matrix A\n"); displayMatrix(&a); printf("\nDisplaying Matrix B\n"); displayMatrix(&b); multiplyMatrix(&a,&b,&r); printf("\nDisplaying Matrix Result\n"); displayMatrix(&r); printf("\nCleaning up memory\n"); destroyMatrix(&a); destroyMatrix(&b); destroyMatrix(&r); return 0; } </i>
Enclose your source code with source tags:

  //like this:[source]//your code  

[/source]
and your code should come out formatted like an editor
Advertisement
scanf("%d", m->y);

should be:

scanf("%d", &m->y);

do this with all your scanfs.

hope it helps...
quote: Original post by novice_programer
// One dimensional limitation only
const int MAX_COL = 30;

struct matrix
{
int y; // Rows
int x; // Columns
int (*d)[MAX_COL];
};



Is int (*d)[MAX_COL]; supposed to be an array or a function pointer? m->d = new int[m->y][MAX_COL]; suggests an array.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement