Thread: Multiplying matrixes

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

    Multiplying matrixes

    Trying to multiply to matrixes using the following algorithm,
    Code:
    ABrec(A,B)
    n=A.rows; //n must be multiple of 2
     C is a new n*n matrix.
    if(n==1)
      C[0][0]=A[0][0]*B[0][0];
    else
       we partion A,B,C into 4 parts;
       C00=ABrec(A00,B00)+Arec(A10,B01);
       C01=ABrec(A00,B01)+Arec(A01,B11);
       C10=ABrec(A10,B11)+Arec(A11,B10);
       C11=ABrec(A10,B01)+Arec(A11,B11);   
    return C;
    heres my code,
    Code:
    #include <iostream>
    
    using namespace std;
    
    void Add(int **A,int **B,int **C,int n)
    {
      for(int i=0;i<n;++i)
       {
        for(int j=0;j<n;++j)
        C[i][j]+=A[i][j]+B[i][j];
        }
    return;
    }
    
    int **AB(int **A,int **B,int **C,int n)
    {
       for(int i=0;i<n;++i)
       {
        for(int j=0;j<n;++j)
        {
          C[i][j]=0;
         for(int k=0;k<n;++k)
            C[i][j]+=A[i][k]*B[k][j];
        }
    
       }
    }
    int **ABrec(int **A,int **B,int **C,int n)
    {
    
       if(n==1)
       C[0][0]=A[0][0]*B[0][0];
       else
       {
         int **A00=A,**A01=A+1,**A10=A+2,**A11=A+3;
         int **B00=B,**B01=B+1,**B10=B+2,**B11=B+3;
         int **C00=C,**C01=C+1,**C10=C+2,**C11=C+3;
         Add(ABrec(A00,B00,C00,n/2),ABrec(A01,B10,C00,n/2),C00,n/2);
         Add(ABrec(A00,B01,C01,n/2),ABrec(A00,B11,C01,n/2),C01,n/2);
         Add(ABrec(A10,B00,C10,n/2),ABrec(A11,B10,C10,n/2),C10,n/2);
         Add(ABrec(A10,B01,C11,n/2),ABrec(A11,B11,C11,n/2),C11,n/2);
    
       }
       return C;
    }
    
    
    int main()
    {
    int **A=new int*[16],**B=new int*[16],**C=new int*[16];
    for(int i=0;i<16;++i)
    {
    A[i]=new int[16];
    B[i]=new int[16];
    C[i]=new int[16];
    }
    for(int i=0;i<16;++i)
    {
     for(int j=0;j<16;++j)
     {
        A[i][j]=B[i][j]=C[i][j]=j+1;
     }
    
    }
    
    C=ABrec(A,B,C,16);
    
    }
    but the code doesnt work !!!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not only does it not work, it creates tons of memory leaks, and is just a nightmare to debug.
    Before going further, I strongly suggest you learn what vectors are, how to use 2d vectors and read this: SourceForge.net: Safer arrays in Cpp - cpwiki, especially about preventing out-of-bounds access.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    C[i][j] += A[i][j]+B[i][j];
    I don't know much about matrixes but that doesn't look like addition to me.
    Kurt

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Sorry,thats a mistake in normal method,It should be C[i][j]=A[i][j]+B[i][j],
    And I know what vector is, but whats the advantage of using vector in that recursion??

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read the article. It explains a lot of things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Books for C matrixes
    By pedofil in forum C Programming
    Replies: 1
    Last Post: 04-17-2008, 04:24 AM
  2. problem with matrixes
    By sdherzo in forum C Programming
    Replies: 9
    Last Post: 07-02-2007, 03:14 AM
  3. Help with calculations with tables ( matrixes )
    By sdherzo in forum C Programming
    Replies: 11
    Last Post: 06-20-2007, 07:01 AM
  4. Pruduct of matrixes
    By Yumin in forum C++ Programming
    Replies: 1
    Last Post: 01-04-2006, 03:45 AM
  5. multiply 2 matrixes
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 04-14-2005, 01:52 AM

Tags for this Thread