Thread: why this code is not working? i wanted to multiply a matrix by itself

  1. #1
    Registered User
    Join Date
    Jan 2016
    Location
    Chittagong,Bangladesh
    Posts
    8

    Angry why this code is not working? i wanted to multiply a matrix by itself

    insert
    Code:
    #include <stdio.h>
    #include<conio.h>
    
    int main()
    {   int i,j;
        int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
        int sqr [3][3];
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=2;j++)
                sqr[i][j] = array1[i][j]*array1[i][j];
                printf("%d\t",sqr[i][j]);
    }
    getch();
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the test input, expected output and actual output?

    Note that your indentation is misleading. It should be:
    Code:
    #include <stdio.h>
    #include<conio.h>
    
    int main()
    {
        int i,j;
        int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
        int sqr [3][3];
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=2;j++)
                sqr[i][j] = array1[i][j]*array1[i][j];
    
            printf("%d\t",sqr[i][j]);
        }
        getch();
    }
    I recommend that you use braces for if statements and loops even when unnecessary.
    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

  3. #3
    Registered User
    Join Date
    Jan 2016
    Location
    Chittagong,Bangladesh
    Posts
    8
    expected output :
    144 1296
    1024 3136


    actual output : 2686728 0
    Last edited by XLR8; 01-07-2016 at 03:26 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your multiplication appears to be correct. You just need to print at the right places. Take my advice concerning the braces first.

    By the way, are you sure that your expected output is correct? Given two 3x3 matrices A and B, AB is also a 3x3 matrix.
    Last edited by laserlight; 01-07-2016 at 03:43 AM.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > for(i=1;i<=2;i++)
    I suppose the other point is that subscripts start at 0, not 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Code:
    #include <stdio.h>
    #include<conio.h>
    
    int main()
    {
        int i,j;
        int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
        int sqr [3][3];
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=2;j++)
                sqr[i][j] = array1[i][j]*array1[i][j];
    
            printf("%d\t",sqr[i][j]);
        }
        getch();
    }
    To me it all sounds incorrect. The way how to multiply two matrices is to multiply a row of the first matrix with a column in the second matrix, not cell by cell with equal coordinates (that would actually work only for a matrix with one row and one column).

    I recommend to check out:

    https://en.wikipedia.org/wiki/Matrix_multiplication


    int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
    for(i=1;i<=2;i++)
    Another issue, an array in C starts with 0. So the for cycle should go from

    Code:
        for(i=0;i<=2;i++)
    Last edited by nerio; 01-08-2016 at 05:49 AM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Here is the code for you:

    Code:
    #include <stdio.h>
     
    int main()
    {
        int i,j;
        int array1[3][3]= {{0,0,0},{0,12,36},{0,32,56}} ;
        int sqr[3][3];
    
        /* multiplication part */
        for(i=0;i<3;i++)
        {
               for(j=0;j<3;j++){
    		                      sqr[i][j]=array1[i][0]*array1[0][j]+array1[i][1]*array1[1][j]+array1[i][2]*array1[2][j];
        			            }
        } 
    
    
    
        /* here is the printing part */
        for(i=0;i<3;i++)
        {
    	    for(j=0;j<3;j++){
    			            printf("%d\t",sqr[i][j]);
    			            }
    	    printf("\n");
        } 
        return 0;
    }
    and the output is:

    0 0 0
    0 1296 2448
    0 2176 4288
    Remember: row * column!

    (what sounds weird to me, if I write there array1[2][2], my compiler returns an error.. so I wrote there 3s and it works now.)
    Last edited by nerio; 01-08-2016 at 09:20 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    nerio: I think that you should have waited for a reply from XLR8 before proceeding to post a complete working solution. I gave hints in my previous two posts in the hope that XLR8 would figure out how to print correctly, then I ended my second post with an observation that the expected output was wrong, though couched in more placid terms: your assertion that "it all sounds incorrect" would be right once XLR8 is convinced of the problem with the expected output, but until then, it the assertion is not true: the actual output, with newlines correctly added, does match XLR8's expected output, hence by that standard it is correct. Salem's post was an elaboration of that, i.e., to point out that some elements were not being accessed at all.

    Furthermore, if you want to post code, then you should post code that is properly formatted.
    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

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Well, if his expected output is a 2x2 matrix, as he wrote

    expected output :
    144 1296
    1024 3136
    maybe he wants to multiply

    Code:
    int array1[3][3]= {{12,36},{32,56}};
    and I think I am right in this since he also wrote in his code

    Code:
    for(i=1;i<=2;i++)
    It is difficult to help people if they do not state their problem precisely.

    EDIT:

    matrix {{12,36},{32,56}} to the power of two equals {{1296,2448},{2176,4288}}, so my conclusion is that my previous post is the ultimate reply and his expected output was wrong.
    Last edited by nerio; 01-08-2016 at 09:33 AM.

  10. #10
    Registered User
    Join Date
    Jan 2016
    Location
    Chittagong,Bangladesh
    Posts
    8
    thank you all

  11. #11
    Registered User
    Join Date
    Jan 2016
    Location
    Chittagong,Bangladesh
    Posts
    8
    oh! i actually had forgotten how to multiply matrices, thnx nerio

  12. #12
    Registered User
    Join Date
    Jan 2016
    Location
    Chittagong,Bangladesh
    Posts
    8
    I just wanted to use the [1][1],[1][2],[2][1],[2][2] slots of the array, so I set i=1 in the loop, i think i messed up in the process of multiplying two matrices, i am new in programming so i couldn't explain my problem properly,and my thinking was also wrong so my expected output was wrong, i am really sorry for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. How to multiply matrix with unitary matrix?
    By maestorm in forum C Programming
    Replies: 3
    Last Post: 11-13-2013, 07:29 AM
  3. Help with this piece of code to multiply a string by two
    By Avanish Giri in forum C Programming
    Replies: 10
    Last Post: 04-11-2012, 05:58 AM
  4. OpenGL, C++ Programmers wanted! Even one line of code can help!
    By BlueFireEXE in forum C++ Programming
    Replies: 16
    Last Post: 03-03-2004, 04:52 PM
  5. Working Code Samples Wanted
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 10:05 PM