Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>

int main() {
	printf("Enter M and N of matrix A: ");
	int m,n;
	int p,q;
	scanf("%i %i",&m,&n);
	printf("Enter matrix elements: ");
	int i,j,matA[101][101];
	for (i=0; i<m; i++)
		for(j=0; j<n; j++){
			scanf("%i",&matA[i][j]);
		}


int temp;
	for (i = 0; i < m; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            temp = matA[i][j];
            matA[i][j] = matA[j][i];
            matA[j][i] = temp;
        }
    }

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n / 2; j++)
        {
            temp = matA[i][j];
            matA[i][j] = matA[i][n - 1 - j];
            matA[i][n - 1 - j] = temp;
        }
    }



	for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
            printf("%2d ", matA[i][j]);

        printf("\n");
    }
    printf("\n");








	return 0;
}


What I have tried:

It rotates N x N matrix like it should, but there is problem with M x N matrix.
Posted
Updated 19-Nov-17 0:50am
Comments
Richard MacCutchan 19-Nov-17 3:04am    
Of course there is because a matrix of size M x N (where M and N are not equal), cannot be rotated in situ, as the dimensions are not correct. The answer is to create a new matrix of size N x M and copy elements that way.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:
C#
int temp;

and run your app in the debugger. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Yes, I could probably tell you what "the problem" is - but it's not difficult to do this yourself, and you will learn something really worthwhile at the same time!
 
Share this answer
 
Comments
Amar Ćatović 19-Nov-17 10:40am    
I couldn't figure it out :( I created another matrix so I could copy elements into it, but it just doesn't work.
OriginalGriff 19-Nov-17 11:22am    
So use the debugger and look at what is going on!
Create a small matrix - 2 x 3 - and watch what happens as your code runs.

This isn't difficult - and it's a lot easier to develop this skill on a small piece of code like this that on your next, bigger, task.
Amar Ćatović 19-Nov-17 13:33pm    
Now it prints elements into new matrix, but it is still N x N matrix... Debug is not helping me, it makes me even more confused
OriginalGriff 20-Nov-17 5:11am    
Look closely at what it's doing!
Write it down on a piece of paper, and do each operation manually - without reference to your code. Then after each manual operation, check exactly what the code does for that iteration.
Amar Ćatović 20-Nov-17 11:34am    
could you please write the code so I can study it. It just doesn't rotate like it should :(
Quote:
It rotates N x N matrix like it should, but there is problem with M x N matrix.

You need to take into account that the shape of the matrix changes after transpose.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900