Thread: can someone help me with this?

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ...
    the two sections are different
    one has printf("%9.3f", a[row]+col);
    and the other has printf("%9.3f", a[row][col]);
    i need both
    Last edited by burninfrost296; 03-02-2009 at 04:58 PM.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by burninfrost296 View Post
    ...
    the two sections are different
    one has printf("%9.3f", a[row]+col);
    and the other has printf("%9.3f", a[row][col]);
    i need both
    AEach green section has both, given that the second green section is identical to the last six lines of the first green section. The first prints the a matrix that you want. The red section prints your b as you read it in. The third green section prints another row of a, if there was one, which there isn't, so it does weird things. You do not want to try to print a's after you read and print the b's, so simply remove the extraneous green section.

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    oh
    haha
    i get it now
    thanks, but when i compile it, it still prints 4 rows and 7 columns instead of 7 rows and 7 columns
    do you know why it does that?

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    << Yes >>

    Seriously, post your most recent version of the program, and I'll take a look and see what's up, OK?

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok
    thanks

    #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[][] */
    /* 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);
    }

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    learn to indent the code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you read the number of rows into the variable called "rows" and then print the variable called "row".

Popular pages Recent additions subscribe to a feed