this is what my code looks like:
how do i get rid of the [7] line? i can't find it. i keep looking over and over again.Code:#include <stdio.h> int main(void) { FILE *infile; int row, col, rows, cols; double **a, *b, *c, sum=0.0; /* Define infile */ infile = fopen("matrix5.dat", "r"); if(infile==NULL){ printf("matrix5.dat does not exist.\n"); exit(100); } /* read rows and cols */ fscanf(infile, "%d %d", &rows, &cols); printf("\nrows = %2d cols = %2d\n", row, cols); /* Allocate memory to **a */ a=(double **)calloc((size_t)rows, sizeof(double *)); /* Allocate memory for a[row], row=0, 1, ..., rows-1 */ for(row = 0; row<rows; row++){ a[row] = (double *)calloc((size_t)cols, sizeof(double)); if(a[row] == NULL){ printf("\n**calloc failed in allocating memory for a[%d]\n", row ); exit(101); } } /* Allocate memory for b[rows] and c[rows] */ b = (double *)calloc((size_t)rows, sizeof(double)); c = (double *)calloc((size_t)rows, sizeof(double)); if(b==NULL || c==NULL){ printf("** calloc failed in allocating memory for b[] or c[]\n"); exit(102); } /* read all rows and save the values in a[][] */ printf("\na[][]:\n"); for(row=0; row<rows; row++){ printf("[%d]", row); for(col=0; col<cols; col++) fscanf(infile, "%lf", a[row]+col); for(col=0; col<cols; col++) printf("%9.3f", a[row][col]); printf("\n"); } /* read b[] from the last line */ printf("\nb[]"); for(row=0; row<rows; row++) fscanf(infile, "%lf", b+row); for(row=0; row<rows; row++) printf("%9.3f", b[row]); printf("\n"); /* print a[][] */ printf("[%d]", row); for(col=0; col<cols; col++) fscanf(infile, "%lf", a[row]+col); for(col=0; col<cols; col++) printf("%9.3f", a[row]+col); printf("\n"); } /* read b[] from the last line */ printf("\nb[]"); for(row=0; row<rows; row++) fscanf(infile, "%lf", b+row); for(row=0; row<rows; row++) printf("%9.3f", b[row]); printf("\n"); /* print a[][] */ printf("[%d]", row); for(col=0; col<cols; col++) fscanf(infile, "%lf", a[row]+col); for(col=0; col<cols; col++) printf("%9.3f", a[row]+col); printf("\n"); /* print b[] */ /* compute c[] = a[][] * b[] */ for(row=0; row<rows; row++){ for(col=0, sum=0.0; col<cols; col++) sum = sum + (a[row][col] *b[col]); c[row]=sum; } printf("\n"); /* print c[] */ printf("c[]"); for(row=0; row<rows; row++) printf("%9.3f", c[row]); printf("\n"); /* return memory */ free(b); free(c); for(col = 0; col<cols; col++) free(a[col]); free(a); /* close infile */ fclose(infile); exit(0); } and when i compile it i get: rows = 4 cols = 7 a[][]: [0] 10.000 9.000 8.000 7.000 6.000 5.000 4.000 [1] 9.000 11.000 -6.000 8.000 3.000 -2.000 5.000 [2] 8.000 -6.000 12.000 10.000 -8.000 -6.000 4.000 [3] 7.000 8.000 10.000 14.000 13.000 -6.000 5.000 [4] 6.000 3.000 -8.000 13.000 16.000 14.000 -9.000 [5] 5.000 -2.000 -6.000 -6.000 17.000 15.000 14.000 [6] 4.000 5.000 4.000 5.000 -9.000 19.000 10.000 b[] 1.000 2.000 -1.000 4.000 -3.000 2.000 1.000 [7] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 c[] 44.000 61.000 40.000 23.000 43.000 -24.000 105.000



LinkBack URL
About LinkBacks



