Like Tree1Likes
  • 1 Post By stdq

Problem - Matrix Program

This is a discussion on Problem - Matrix Program within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I am trying to write a program that read two matrices (one in each function), and then calculates ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    114

    Problem - Matrix Program

    Hi, everyone. I am trying to write a program that read two matrices (one in each function), and then calculates the product between the first and the second (in a function designed for that purpose only), and displays the result in the main function. However, when I compile the program, I get errors, and I believe I am making mistakes when I return each matrix to the main function, and also when I pass each matrix to a function, for example, matrices 1 and 2 to the function product(). Am I right, or my mistakes are far from that? Thanks in advance!

    Code:
    #include <stdio.h>
    double read_matrix1 (double matrix1[2][2]);
    double read_matrix2 (double matrix2[2][2]);
    double product (double matrix1[2][2], double matrix2[2][2], double matrix3[2][2]);
    
    
    int main (void)
    {
    
        double matrix1[2][2], matrix2[2][2], matrix3[2][2];
        int i, j;
    
        matrix1 = read_matrix1(matrix1);
        matrix2 = read_matrix2(matrix2);
        matrix3 = product(matrix1, matrix2);
    
        printf("The product of the two matrices yields:\n");
    
        for (i = 0 ; i < 2 ; i++)
        {
            for (j = 0 ; j < 2 ; j++)
            {
                printf("%f  ", matrix3[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    double read_matrix1 (double matrix1[2][2])
    {
    
        int i, j;
    
        printf("Input of matrix 1 elements.\n");
    
        for (i = 0 ; i < 2 ; i++)
        {
            for (j = 0 ; j < 2 ; j++)
            {
                printf("Type in the element [%d][%d]: ", i, j);
                scanf("%lf", &matrix1[i][j]);
            }
        }
    
        return matrix1[2][2];
    
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    double read_matrix2 (double matrix2[2][2])
    {
    
        int i, j;
    
        printf("Input of matrix 2 elements.\n");
    
        for (i = 0 ; i < 2 ; i++)
        {
            for (j = 0 ; j < 2 ; j++)
            {
                printf("Type in the element [%d][%d]: ", i, j);
                scanf("%lf", &matrix2[i][j]);
            }
        }
    
        return matrix2[2][2];
    
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    double product(double matrix1[2][2], double matrix2[2][2], double matrix3[2][2])
    {
    
        int i, j, k;
        double sum, total = 0;
    
        for (i = 0 ; i < 2 ; i++)
        {
            for (j = 0 ; j < 2 ; j++)
            {
                for (k = 0 ; k < 2 ; k++)
                {
                    sum = matrix1[i][k] * matrix2[k][j];
                    total = total + sum;
                }
                matrix3[i][j] = total;
                sum = 0;
            }
        }
    
        return matrix3[2][2];
    
    }
    Last edited by stdq; 05-13-2011 at 06:15 PM.
    technoexam likes this.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Step through your program, and you'll see the errors better.

    In function product, you return the address of matrix3, and use it also. That doesn't agree with the function's declared return type, and that matrix was not a parameter for this function. You should be getting a compiler error. Please prototype your functions.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Found the errors, thanks! By the time you were answering my topic, I edited the code, but it still has errors here, but now I've fixed them.

  4. #4
    Registered User technoexam's Avatar
    Join Date
    May 2011
    Location
    India
    Posts
    8
    Hello stdq ! You can refer C program for multiplication / product of two matrices. It gives the actual output that you want. Refer it.. I think, u will get the answer that you want..

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by technoexam View Post
    Hello stdq ! You can refer C program for multiplication / product of two matrices. It gives the actual output that you want. Refer it.. I think, u will get the answer that you want..
    Yeah... check it out... if you want an errored solution written for a compiler that's 20 years out of date.


    Oh, and TechnoExam... we don't hand out free source code here.

    There's an old saying: "Give a man a fish and you've fed him for a day. Teach a man to fish and you've fed his whole family for a lifetime".

    What's that mean? It means that simple answers breed simpletons.
    Last edited by CommonTater; 05-13-2011 at 11:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program - inverse of a matrix
    By chaugh in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 10:00 PM
  2. Matrix c program help!!!
    By kclew in forum C Programming
    Replies: 21
    Last Post: 02-25-2009, 03:46 AM
  3. Help! matrix program
    By jadman in forum C Programming
    Replies: 3
    Last Post: 06-21-2005, 11:20 AM
  4. 2x2 matrix program
    By screamer903 in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 07:02 AM
  5. matrix program
    By J in forum C Programming
    Replies: 4
    Last Post: 04-21-2002, 08:07 PM

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