Thread: Diagonal

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Diagonal

    So far I have managed to get diagonal of a matrix only like this, but I know there is a much simpler way, help! I a matrix is big this is useless.

    Code:
    for(a=0;a<3;a++)
    		{
    	for(b=0;b<3;b++)
    	{
    		if(a==0 && b==0)
    		{
    			printf("\n%d", mx[a][b]);
    		}
    		
    		if(a==1 && b==1)
    		{
    			printf("\n%d", mx[a][b]);
    		}
    		if(a==2 && b==2)
    		{
    			printf("\n%d", mx[a][b]);
    		}
    	}

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    start by asking yourself what is a diagonal. (look at the math definition)

  3. #3
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    i know what it is, i just don't know how to print it

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    from your code... if a==b then you print... you have eliminated the 2 ifs. if you have to print only the components that have the indexes equal try to lose a cycle (a for). you don't need to go through all the comonents of the matrix

  5. #5
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    sorry for being stupid got it!

  6. #6
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    		for(a=0;a<4;a++)
    		{
    			for(b=0;b<4;b++)
    			{
    				if(a==b)
    				{
    					b++;
    					printf("\n%d", mx[a][b]);
    				}
    			}
    		}
    I want to print upper diagonal, and i managed, but it gives me some stupid number, memory trash or something, dont know why, other than that everything works.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    lose the second for along with b and print mx[a][a]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D diagonal counter
    By poopiepantz2525 in forum C Programming
    Replies: 4
    Last Post: 05-01-2008, 06:53 PM
  2. help with 3D tic-tac-toe diagonal checking
    By tunerfreak in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2006, 10:14 PM
  3. tic-tac-toe diagonal
    By abrege in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2003, 01:10 PM
  4. Diagonal Matrix
    By mlupo in forum C Programming
    Replies: 1
    Last Post: 12-04-2002, 10:02 PM
  5. Diagonal Text?
    By Davros in forum Windows Programming
    Replies: 1
    Last Post: 03-19-2002, 04:40 PM