Thread: Raise Matrix to the power of....

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    1

    Raise Matrix to the power of....

    Hi trying to raise a Matrix to the power of p, does not work. Where is my mistake.....

    Thanks for help

    Code:
     int main() 
    
      {
        int n, p;
        printf("Numer of Rows/Colums of sq matrix: ");
        scanf("%d", &n);
        printf("to the power of: ");
        scanf("%d", &p);
    
        int m[n][n];
        int r[n][n];
    
        printf("Elemnts\n");
        for ( int b = 0 ; b < n ; b++ ) {
            for ( int d = 0 ; d < n ; d++ ) {
                printf("[%d][%d] = ", b+1, d+1);
                scanf("%d", &m[b][d]);
            }
        }
    
        int s = 0;
        for (int i = 0; i < p; i++)
        {
            for ( int b = 0 ; b < n ; b++ )
            {
                for (int d = 0 ; d < n ; d++ )
                {
                    for (int k = 0 ; k < n ; k++ )
                    {
                        s += m[b][k]*m[k][d];
                    }
                    r[b][d] = s;
                    s = 1;
                }
             }
    
            for ( int b = 0 ; b < n ; b++ ) {
                for ( int d = 0 ; d < n ; d++ ) {
                    m[b][d] = e[b][d];
                    r[b][d] = 0;
                }
            }
        }
    
        printf("RESULT:-\n");
    
        for (int c = 0 ; c < n ; c++ )
        {
            for (int d = 0 ; d < n ; d++ )
            {
                printf("%d   ", m[c][d]);
            }
            printf("\n");
        }
    
    return 0;
    }
     
    


    Last edited by BEG-inner; 08-13-2018 at 05:09 AM. Reason: Spelling mistake

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You haven't declared e, although you presumably mean to say r there.
    You reset s to 1 instead of 0.
    You set m to the result after each multiplication which means that the next time you multiply it you aren't multiplying by the original m.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem-pointer reference-Kth power of matrix
    By thriller500 in forum C++ Programming
    Replies: 4
    Last Post: 06-23-2012, 05:09 AM
  2. Using a bitwise operators to raise to the power of 5
    By philuk2000 in forum C Programming
    Replies: 23
    Last Post: 12-10-2011, 10:30 AM
  3. power operation on a matrix question..
    By transgalactic2 in forum C Programming
    Replies: 6
    Last Post: 12-19-2008, 11:11 AM
  4. Raise to the power problem
    By swgh in forum C Programming
    Replies: 7
    Last Post: 12-01-2008, 08:12 AM
  5. Can't raise x to the power of y
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 08-31-2007, 12:45 AM

Tags for this Thread