multiplying a matrix inputed from a file

This is a discussion on multiplying a matrix inputed from a file within the C Programming forums, part of the General Programming Boards category; Hey everyone, I am creating a program that opens a file that contains a 4 x 6 matrix and then ...

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

    multiplying a matrix inputed from a file

    Hey everyone,

    I am creating a program that opens a file that contains a 4 x 6 matrix and then stores it in a two dimensional array. It then prompts the user to enter a number which the program multiplies each element of the array by and then stores it in a second two dimensional array to put into an output file. I am just having trouble with the multiplication, call of matrix, and and storing in the two dim array at this point. Any help with any aspect of this program is much appreciated. Thanks. By the way. I was having trouble with the indentation of my code when I displayed so I put the code also into an attachment because I am know that that is part of the protocol for posts.

    Code:
    #include<stdio.h>
    #define ROWSIZE 4
    #define COLUMNSIZE 6
    
    int ma[ROWSIZE][COLUMNSIZE];
    int mult(int x);
    
    int main()
    {
            FILE *infile;
            FILE *outfile;
    
            int ma[ROWSIZE][COLUMNSIZE];
            int i = 0;
            int j = 0;
            int multiplier;
    
            infile = fopen("input_prelab3.txt", "r");
            outfile = fopen("output_prelab3.txt", "w");
    
            printf("Enter an integer to multiply to the loaded matrix: ");
            scanf("%d", &multiplier);
    
    
    
            if (infile)
            {
                    for ( ;; )
                    {
                            int c = getc(infile);
                            if ( c == EOF )
                                    break;
                            if ( c != '\n' && c != '\r' )
                            {
                                    ma[i][j] = c;
                                    if ( ++j >= COLUMNSIZE )
                                    {
                                            j = 0;
                                            if ( ++i >= ROWSIZE )
                                                     break;
                                    }
                            }
                    }
                    fclose(infile);
            }
            for (i = 0; i < ROWSIZE; i++)
            {
                    for(j = 0; j < COLUMNSIZE; j++)
                            putint(ma[i][j]);
                    putint('\n');
            }
            return (int mult(int multiplier);
    }
    
    int mult(int x)
    {
            for (i = 0; i < 4; i++)
                    for (j = 0; j < 6; j++)
                            mb[i][j] = x * ma[i][j];
    }
    Thanks again.
    Last edited by eater; 04-20-2009 at 03:33 AM.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Actually I think this code will probably be better and more simple:


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define COL 6
    #define ROW 4
    int mult(int ma[][6]);
    
    int main()
    {
            int i =0;
            int j = 0;
            int multiply;
            FILE *infile;
    
            infile = fopen("input_prelab3.txt", "r");
    
            int ma[4][6];
    
            for (i = 0; i<4; i++)
                    for (j = 0; j < 6; j++)
                            fscanf(infile, "%d*[,]\n", ma[i][j]);
    
            multiply = mult(ma);
            printf("%d", multiply);
    
            fclose(infile);
    
            return 0;
    
    }
    int mult(int ma[][6])
    {
            int i = 0;
            int j = 0;
            int multiply;
            for(i =0; i<ROW; i++)
                    for(j = 0; j<COL; j++)
                            multiply *= ma[i][j];
            return multiply;
    }
    Last edited by eater; 04-20-2009 at 03:33 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you disable java-script, can you please try to use code-tags despite the java-code checking that you do so doesn't run?

    --
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Actually, you should edit your post, highlight the code and press the # sign at the top of the edit window, to wrap your code in code tags.

    Code should never be without it.

    I pray your code is not indented as ugly as it appears, without the code tags.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    The second code compiles without errors but there is a bus error. What should I do? And will this perform the correct operation?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                            fscanf(infile, "%d*[,]\n", ma[i][j]);
    Take the address of ma[i][j] perhaps?

    --
    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.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    int multiply;
            for(i =0; i<ROW; i++)
                    for(j = 0; j<COL; j++)
                            multiply *= ma[i][j];
    note that you are using not initialized variable
    increase your warning level
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I'm sorry, which variable has not already been initialized vart? And I tried taking the address of ma[i][j] but it still returns a bus error

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eater View Post
    I'm sorry, which variable has not already been initialized vart? And I tried taking the address of ma[i][j] but it still returns a bus error
    multiply.

    And you'd have to post the whole code again for us to tell if you have fixed it or not.

    --
    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.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    k so I set multiply = to 1 and I added an address operator but its still giving me a bus error? thanks for your patience/


    Code:
    #include<stdio.h>
    #define COL 6
    #define ROW 4
    int mult(int ma[][6]);
    
    int main()
    {
            int i =0;
            int j = 0;
            int multiply;
            FILE *infile;
    
            infile = fopen("t.rtf", "r");
    
            int ma[4][6];
    
            for (i = 0; i<4; i++)
                    for (j = 0; j < 6; j++)
                            fscanf(infile, "%d*[,]\n", &ma[i][j]);/*no address*/
            printf("%d", mult(ma));
            fclose(infile);
    
            /*return 0;*/
    }
    int mult(int ma[4][6])
    {
            int i = 0;
            int j = 0;
            int multiply = 1;
            for(i =0; i<ROW; i++)
                    for(j = 0; j<COL; j++)
                            multiply *= ma[i][j];
            return multiply;
    }

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Hey guys so disregard everything I have done previosly I have made some serious headway on my program at the cost of my sleep and/or health. haha. So at this point the entire program should run except for I am having a problem with the easiest part. The part I am having trouble with has to go within my original matrix, ma, and determine both the MAX, and MIN values in a single function call. I know I am close but I keep getting a segmentation error. Thanks in advance.


    Code:
    #include<stdio.h>
    
    int mult(int ma[][6], int m, int mb[][6], int row, int col);
    
    int main()
    {
            int i =0;
            int j = 0;
            int multiply;
            int ma[4][6];
            int mb[4][6];
    
            FILE *infile;
            FILE *outfile;
    
            infile = fopen("input_prelab3.txt", "r");
            outfile = fopen("output_prelab3.txt", "w");
    
            for (i = 0; i<4; i++)
                    for (j = 0; j < 6; j++)
                            fscanf(infile, "%d", &ma[i][j]);/*no address*/
    
            printf("Enter a number to mutiply by:");
            scanf("%d", &multiply);
            mult(ma, multiply, mb, 4, 6);
    
            for (i = 0; i<4; i++)
            {
                    for (j = 0; j < 6; j++)
                            fprintf(outfile, "%d ", mb[i][j]);
                    fprintf(outfile, "\n", mb[i][j]);
            }
    
            printf("%d %d", max_min(ma));
            fclose(infile);
            fclose(outfile);
    }
    
    int mult(int ma[][6], int m, int mb[][6], int row, int col)
    {
            int i = 0;
            int j = 0;
            for(i =0; i<row; i++)
                    for(j = 0; j<col; j++)
                           mb[i][j] =  m * ma[i][j];
            return 0;
    }
    
    int max_min(int ma[][6], int row, int col)
    {
            int i = 0;
            int j = 0;
            int MAX = ma[0][0];
            int MIN = ma[0][0];
            for(i =0; i < row; i++)
                    for(j = 0; j < col; j++)
                    {
                            if (ma[i][j]> MAX)
                                    MAX == ma[i][j];
                            if (ma[i][j] < MIN)
                                    MIN == ma[i][j];
                    }
            return MAX, MIN;
    }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can return just one value from max_min(), not two.

    Code:
    max = max_min(ma, &min);
    Where min is a variable in main() (which should be int main() BTW)

    and the last lines in max_min is simply:

    Code:
    return max;
    }

    Does that make sense to you?

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Well I thought it did so I changed my code to look like this but it doesnt output correctly. Did I change my code correctively. I believe my main function was already designated to a return an integer value before?

    Code:
    #include<stdio.h>
    
    int mult(int ma[][6], int m, int mb[][6], int row, int col);
    
    int main()
    {
            int i =0;
            int j = 0;
            int min;
            int max;
            int multiply;
            int ma[4][6];
            int mb[4][6];
    
            FILE *infile;
            FILE *outfile;
    
            infile = fopen("input_prelab3.txt", "r");
            outfile = fopen("output_prelab3.txt", "w");
    
            for (i = 0; i<4; i++)
                    for (j = 0; j < 6; j++)
                            fscanf(infile, "%d", &ma[i][j]);/*no address*/
    
            printf("Enter a number to mutiply by:");
            scanf("%d", &multiply);
            mult(ma, multiply, mb, 4, 6);
    
            for (i = 0; i<4; i++)
            {
                    for (j = 0; j < 6; j++)
                            fprintf(outfile, "%d ", mb[i][j]);
                    fprintf(outfile, "\n", mb[i][j]);
            }
            max = max_min(ma, &min);
            printf("%d %d", max_min(ma));
            fclose(infile);
            fclose(outfile);
    }
    
    int mult(int ma[][6], int m, int mb[][6], int row, int col)
    {
            int i = 0;
            int j = 0;
            for(i =0; i<row; i++)
                    for(j = 0; j<col; j++)
                           mb[i][j] =  m * ma[i][j];
            return 0;
    }
    
    int max_min(int ma[][6], int row, int col)
    {
            int i = 0;
            int j = 0;
            int MAX = ma[0][0];
            int MIN = ma[0][0];
            for(i =0; i < row; i++)
                    for(j = 0; j < col; j++)
                    {
                            if (ma[i][j]> MAX)
                                    MAX == ma[i][j];
                            if (ma[i][j] < MIN)
                                    MIN == ma[i][j];
                    }
            return MAX;
    }

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    ugh I have been trying to figure this out for far too long. is there anyone who knows how I should fix this?

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    dbzx ur in my class aren't you? do you know how to do this?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21