Thread: can someone help me with this?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    can someone help me with this?

    this is what my code looks like:
    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
    how do i get rid of the [7] line? i can't find it. i keep looking over and over again.
    Last edited by Salem; 03-01-2009 at 11:48 PM. Reason: Added [code][/code] tags, learn to use them yourself

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got two lines marked /*print a[][] */, one before the b and one after the b. (Note that you should put code in code tags.)

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    that doesn't really help...
    which line is it?
    i also get the wrong number of rows
    apparently i should be getting 7

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You complained that your a matrix was printing twice, I pointed out where you had copied your code that printed a in two places. I'm not sure what else you need here.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Get some coffee, dude. Zero to 6 is 7 lines.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Jeez dude... indent your code!

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    not my [a] matrix
    i'm not supposed to have a [7] line after my [b] line

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suggest that you try:
    1. Indenting the code.
    2. Fixing the balance of your brackets, so that the indentation matches the intent of the code - currently there (at least) one too many end-braces.
    3. Recompile.

    I very sure that the current code doesn't compile, which probably means that if your posted output is from some version of this code, it is certainly not the posted version.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    there are the same number of opening and closing braces
    unless i'm missing something
    and the code does compile:

    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");
    /* 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);
    }
    Last edited by laserlight; 03-02-2009 at 02:07 PM. Reason: code

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by burninfrost296
    there are the same number of opening and closing braces
    unless i'm missing something
    and the code does compile:
    Now it does, but your original example does not. I edited your post to add in bbcode tags, but it looks like you still have some more work to do in order to indent your code consistently, though it is good to see that you made some effort in that direction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not in your first post [or there is something else that confuses Emacs' auto-indentation feature], but it seems like that is fixed now.

    Your indentation is still a bit wonky, but a lot better than before.

    Code:
                    for(col=0; col<cols; col++)
                            fscanf(infile, "%lf", a[row]+col);
                            for(col=0; col<cols; col++)
                                    printf("%9.3f", a[row][col]);
    For example here, for second loop is NOT part of the first loop (and it would be a very bad thing if it was, since it is using the same variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    i still get the same thing when i compile it
    with the [7] line after the b[] line
    and i get 4 rows, when i obviously have 7 in my a matrix
    did i use the wrong %?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by burninfrost296 View Post
    not my [a] matrix
    i'm not supposed to have a [7] line after my [b] line
    Your code:
    Code:
    /* 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[] */
    Print a Print b Print a again ("row 7") (You've changed your comments since the first post, but not the code.)

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    did you see my new code?
    i changed it
    but i still get the same problem

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by burninfrost296 View Post
    did you see my new code?
    i changed it
    but i still get the same problem
    No, you changed the comments. You didn't change the code:
    Code:
    /* 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");

Popular pages Recent additions subscribe to a feed