Hello,
first the code that works:
When I enter 9 1s (3*3 matrix) in the 2nd matrix here, I get 9 1s printed out.Code:int main(int argc, char *argv[]) { int r; int rc; int c; float *m1; float *m2; float *m3; int i; float scan; printf("Please enter rows of matrix 1: "); scanf("%i", &r); printf("Please enter columns/rows of matrix 1/2: "); scanf("%i", &rc); printf("Please enter columns of matrix 2: "); scanf("%i", &c); m1 = malloc(r*rc); m2 = malloc(rc*c); m3 = malloc(r*c); printf("Enter elements of matrix 1:\n"); for(i = 0; i < r*rc; i++) { scanf("%f", &scan); m1[i] = scan; } printf("Enter elements of matrix 2:\n"); for(i = 0; i < rc*c; i++) { scanf("%f", &scan); m2[i] = scan; } for(i = 0; i < rc*c; i++) { printf("%f\n", m2[i]); } printf("Thank you, come again\n"); return 0; }
Now the broken code:
Here I get 1.0 1.0 1.0 1.0 0.0 0.0 ...Code:int main(int argc, char *argv[]) { int r; int rc; int c; float *m1; float *m2; float *m3; int i; float scan; printf("Please enter rows of matrix 1: "); scanf("%i", &r); printf("Please enter columns/rows of matrix 1/2: "); scanf("%i", &rc); printf("Please enter columns of matrix 2: "); scanf("%i", &c); m1 = malloc(r*rc); m2 = malloc(rc*c); m3 = malloc(r*c); printf("Enter elements of matrix 1:\n"); for(i = 0; i < r*rc; i++) { scanf("%f", &scan); m1[i] = scan; } printf("Enter elements of matrix 2:\n"); for(i = 0; i < rc*c; i++) { scanf("%f", &scan); m2[i] = scan; } for(i = 0; i < r*c; i++) { m3[i] = 0.f; } for(i = 0; i < rc*c; i++) { printf("%f\n", m2[i]); } printf("Thank you, come again\n"); return 0; }
As an output.
What's wrong? Get the elements of my 2nd matrix overriden? If so, why? Or is it something else?



LinkBack URL
About LinkBacks


