Thread: C program multiply matrices

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Angry C program multiply matrices

    Hey I have a problem here. Maybe someone can figure it out
    This is my code and it's not giving me the right output

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int dim;
    	int i, row, col;
    	// inputting matrices dimensions
    	int M0, M1, M2;
    
    
    	// Calculate the product
    	scanf("%d", &dim); 
    
    
    	for (row=1; row<=dim; row++)
    	{
    		for (col=1; col<=dim; col++)
    		{
    			//Calculate (row,column) element of the product
    			M2 = 0;
    
    
    				for (i=1; i<=dim; i++)
    				{
    					// Element in M0 at (row,i)
    					M0=(row+1)*(col+1);
    
    
    					// Element in M1 at (i, column)
    					M1=row+col;
    
    
    					M2+=M0*M1; // The calculation
    				}
    
    
    				// prining an element
    				printf("%d\t", M2);
    		}
    		// Going to the next row
    		printf("\n");
    	}
    	return 0;
    }
    The output should give me this:
    Input
    2
    Correct Output
    2 5
    4 10


  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What output it gives you?

    And where are the matrices you are planning to multiply?
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As a guess: if M0 was based on row+1 and col+1, might not M1 also be based on row+1 and col+1?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by vart View Post
    What output it gives you?

    And where are the matrices you are planning to multiply?
    2
    16 36
    36 72
    Program ended with exit code: 0

    this is what it gives me and it should be multiplying
    M0[i,j]=(i+1)(j+i)
    M1[i,j]=(i+j)

    M2=M1*M0

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to use i in your multiply as well. It's M0[row][i] times M1[i][col].

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Start with something simple

    M0 should be matrix not integer

    Code:
    int M0[2][2];
    initialize it using your formula and then print it for testing.

    When it works, do the same for M1

    When both matrices are initialized correctly and are displayed correctly - start writing function that multiplies them
    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. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by vart View Post
    Start with something simple

    M0 should be matrix not integer

    Code:
    int M0[2][2];
    initialize it using your formula and then print it for testing.

    When it works, do the same for M1

    When both matrices are initialized correctly and are displayed correctly - start writing function that multiplies them
    I can't use arrays

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    > I can't use arrays
    So define a function called M0 that takes two parameter, i and j, and returns the value at that location, per the formula. Do the same for M1. Then, you can calculate M2 for a given (i, j) with a simple loop and some addition, M0(??, ??) * M1(??, ??). I'll let you figure out the ?? parts.

    Note, you don't need a function, but it fits very nicely here, and it makes your code more readable.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm rather confused: what exactly are you trying to do?

    Looking at your sample input, the input is just 2. How on earth do you get an output of 4 numbers when all you entered was the dimension of the matrix? You didn't enter the entries of the matrices, so what's there to multiply?

    Furthermore, suppose the input is 10, then what are you going to do? You cannot use arrays? Well, how are you going to store the entries of the 10 by 10 matrices (presumably) to be multiplied?
    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

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by laserlight View Post
    I'm rather confused: what exactly are you trying to do?

    Looking at your sample input, the input is just 2. How on earth do you get an output of 4 numbers when all you entered was the dimension of the matrix? You didn't enter the entries of the matrices, so what's there to multiply?

    Furthermore, suppose the input is 10, then what are you going to do? You cannot use arrays? Well, how are you going to store the entries of the 10 by 10 matrices (presumably) to be multiplied?
    Threw me for a loop too, for a bit. The answer is in post #4. The elements of the two matrices to multiply are defined by formulae:
    Quote Originally Posted by YannB View Post
    2
    16 36
    36 72
    Program ended with exit code: 0

    this is what it gives me and it should be multiplying
    M0[i,j]=(i+1)(j+i)
    M1[i,j]=(i+j)

    M2=M1*M0

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by YannB View Post
    this is what it gives me and it should be multiplying
    M0[i,j]=(i+1)(j+i)
    M1[i,j]=(i+j)
    I suppose your problem is in ranges
    for size 2 i and j should receive values 0 and 1

    You give them 1 and 2 - thus getting too high results

    And another problem - your formula is wrong -

    M2[row,col] = sum_for_all_i(M0[row,i] * M1[i,col])
    Last edited by vart; 11-15-2013 at 12:09 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 10-01-2013, 10:54 PM
  2. how many ways exists in order to multiply n matrices?
    By Amin sma in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2012, 02:32 AM
  3. 2 Matrices multiply problem
    By ydan87 in forum C Programming
    Replies: 1
    Last Post: 01-22-2012, 12:48 AM
  4. program won't multiply right
    By Will_rookwood in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2006, 07:28 PM
  5. how to multiply 2 matrices
    By newguy in forum C Programming
    Replies: 1
    Last Post: 10-29-2001, 03:48 AM