Matrix 2

This is a discussion on Matrix 2 within the C++ Programming forums, part of the General Programming Boards category; OK I got it to rotate but there is one problem for example it does this: 123 456 789 this ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Matrix 2

    OK I got it to rotate but there is one problem for example it does this:
    123
    456
    789
    this is not the 2 dimensional array i'm working with keep that in mind... I'm just using it for an example... this is supposed to work with any matrix no matter the size. I'm using random number gen to produce the numbers this is just an example... but this is what it does it rotates all the numbers but when it gets to the left corner it does this...
    412
    953
    876
    well here is the code
    Code:
     void Array2::rotate()
     {
       int temp1, temp2;
       int a=n-1;
    
    
      for (int r=0; r<n/2; r++)  // for rows
       {
       temp1=A[r][r];
    
         for(int c=r; c<n-1; c++) // across the top
          {
             temp2=temp1; // first number = temp2
             temp1=A[r][c]; // temp1 = second
             A[r][c]= temp2;   // A[r][c] becomes becomes whats in temp2
          }
          for(int h=r; h<n-1; h++)   // down right side
          {
          temp2=temp1; // first number = temp2
             temp1=A[h][a]; // temp1 = second
             A[h][a]= temp2;   // A[r][c] becomes becomes whats in temp2
          }
    
        for(int i=a; i>=0; i--)
          {
             temp2=temp1; // first number = temp2
             temp1=A[a][i]; // temp1 = second
             A[a][i]= temp2;   // A[r][c] becomes becomes whats in temp2
          }
       for(int j=n-1; j>=0; j--)
          {
             temp2=temp1; // first number = temp2
             temp1=A[j][r]; // temp1 = second
             A[j][r]= temp2;   // A[r][c] becomes becomes whats in temp2
          }
    
       a++;
       }
     }

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    This way should work. For a matrix like this

    12345
    45666
    78977
    12345
    45676

    Call a rotate_square(left, right, up, down) on the squares
    Code:
    12345
    4   6
    7   7
    1   5
    45676
    
    566
    8 7
    234
    
    9
    Inside the actual rotate function you would have code
    like

    Code:
    while(left <= right && up <= down)
    {
          rotate_square(left, right, up, down);
          left++;
          right--;
          up++;
          down--;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 07:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

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