Thread: Matrix

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

    Matrix

    Ok heres my problem i need to take a matrix like these below and rotate the numbers.... How might i do this ?? i figure the code would have 4 for loops and each one would change the row and cololum of each varible.

    Here is an example Matrix:
    123
    456
    789

    And this is what I want to do....
    412
    753
    896

    Code:
    void Array2::rotate()
    {
      int temp;
    
        for(int r=0; r < n; r++)
        {
    
          	for(int c=c-1; c<n; c++)
             {
                temp=A[r][c];
                A[r][c]=A[c][r];
                A[c][r]= temp;
    
             }
             	 for(int r=r+1;  r< n; r++)
                {
                temp=A[c][r];
                A[r][c]=A[c][r];
                A[r][c]=temp;
                }
                	for(int c=r; c<n; c++)
                	{
                   temp=A[c][r];
                A[r][c]=A[c][r];
                A[r][c]=temp;
                   }
                   	 for(int r=c; r<n; r++)
                      {
                      temp=A[c][r];
                A[r][c]=A[c][r];
                A[r][c]=temp;
                      }
    
    
           }
    I have another question but until i find that i can do this one I will not post it..
    Any help would be appreciated....
    Thanks Achilles
    Last edited by Achillles; 09-13-2002 at 09:56 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
     
    int Array[][4] = { {  1,  2,  3 },
    	           {  4,  5,  6 },
                       {  7,  8,  9 } };
    
    const int MatrixSize = 3;
    
    void RotateArray(bool);
    
    int main(void)
    {
    	for(int x = 0; x < MatrixSize; x++)
    	{
    		for(int y = 0; y < MatrixSize; y++)
    		{
    			cout<<Array[x][y]<<"\t";
    		}
    		cout<<endl;
    	}
    	cout<<endl;
    	RotateArray(true);
    	for(x = 0; x < MatrixSize; x++)
    	{
    		for(int y = 0; y < MatrixSize; y++)
    		{
    			cout<<Array[x][y]<<"\t";
    		}
    		cout<<endl;
    	}
    	getch();
    	return 0;
    }
    
    void RotateArray(bool Clockwise)
    {
    	int TempSize = MatrixSize, c = 0;
    	int TempArray[MatrixSize][MatrixSize] = { 0 };
    	while(TempSize > 1)
    	{
    		for(int x = 0; x < TempSize; x++)
    		{
    			for(int y = 0; y < TempSize; y++)
    			{
    				switch (Clockwise)
    				{
    				case true:
    					if(x == 0 && y < TempSize - 1)
    					{
    						TempArray[x + c][y + 1 + c] = Array[x + c][y + c];
    					} else if (x == TempSize - 1 && y > 0) {
    						TempArray[x + c][y - 1 + c] = Array[x + c][y + c];
    					} else if (x > 0 && y == 0) {
    						TempArray[x - 1 + c][y + c] = Array[x + c][y + c];
    					} else if (x < TempSize - 1 && y == TempSize - 1) {
    						TempArray[x + 1 + c][y + c] = Array[x + c][y + c];
    					}
    					break;
    				case false:
    					if(x == 0 && y > 0)
    					{
    						TempArray[x + c][y - 1 + c] = Array[x + c][y + c];
    					} else if (x == TempSize - 1 && y < TempSize - 1) {
    						TempArray[x + c][y + 1 + c] = Array[x + c][y + c];
    					} else if (x < TempSize - 1 && y == 0) {
    						TempArray[x + 1 + c][y + c] = Array[x + c][y + c];
    					} else if (x > 0 && y == TempSize - 1) {
    						TempArray[x - 1 + c][y + c] = Array[x + c][y + c];
    					}
    					break;
    				}
    			}
    		}
    		TempSize -= 2;
    		c++;
    	}
    	if(MatrixSize % 2 == 1)
    	{
    		TempArray[MatrixSize / 2][MatrixSize / 2] = Array[MatrixSize / 2][MatrixSize / 2];
    	}
    	for(int x = 0; x < MatrixSize; x++)
    	{
    		for(int y = 0; y < MatrixSize; y++)
    		{
    			Array[x][y] = TempArray[x][y];
    		}
    	}
    }
    Last edited by XSquared; 09-13-2002 at 11:13 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Is there any purpose to flipping a matrix like that or is it just something to attempt to program?

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    This shouldn't be so complicated. Just
    do something like this
    Code:
    int col[]   = {0, 1, 2, 2, 2, 1, 0, 0};
    int row[] = {0, 0, 0, 1, 2, 2, 2, 1};
    
    int tmp = A[1][0];
    for (int i = 0; i < 8; ++i)
    {
          int r = row[i];
          int c = col[i];
          A[r][c] = tmp;
          tmp = A[r][c];
    }

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Well i didn't get it to work i want to use 4 for loops.. that makes it simple enough... and i was told that using 4 for loops would be the best option also here is my second question.. how could i take the two matrices above
    123 123
    456 * 456
    789 789
    and multipy it by r times coloum???
    ex 1*1+ 2*4 + 3* 7 then ouput that answer in r zero and coloum 0 and so and then take the next coloum and mutliply it by same row ??? anyone know how to help me in this situation ???

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, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08: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