944,073 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 57496
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 13th, 2005
0

Transpose a matrix

Expand Post »
I need help with transposing a matrix.

I already did the transpose matrix using 2 arrays.

But I can't figure out how to do it using only one array.
So read a matrix and create the transpose within itself.
Do I use the swap or what?

Any help would be great.

Later
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mariners95 is offline Offline
2 posts
since Feb 2005
Feb 13th, 2005
0

Re: Transpose a matrix

If it's a square matrix then you can do it with something like this:
  1. for (int i = 0; i < 4; i++) {
  2. for (int j = i + 1; j < 4; j++) {
  3. int save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
  7. }
If it's a non-square matrix then you're SOL.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 15th, 2005
0

Re: Transpose a matrix

Quote originally posted by Narue ...
If it's a square matrix then you can do it with something like this:
  1. for (int i = 0; i < 4; i++) {
  2. for (int j = i + 1; j < 4; j++) {
  3. int save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
  7. }
If it's a non-square matrix then you're SOL.
Your program won't work. The reason is that when you write matrix[i][j] = matrix[j][i] you've lost the contents of matrix[i][j] forever and you can't get it back to put in position [[j][i]. This will do it.
  1. for (int i = 0; i < 4; i++)
  2. for (int j = 0; j < 4; j++)
  3. { save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
murschech is offline Offline
60 posts
since Dec 2004
Feb 15th, 2005
0

Re: Transpose a matrix

Quote originally posted by Narue ...
If it's a square matrix then you can do it with something like this:
  1. for (int i = 0; i < 4; i++) {
  2. for (int j = i + 1; j < 4; j++) {
  3. int save = matrix[i][j];
  4. matrix[i][j] = matrix[j][i];
  5. matrix[j][i] = save;
  6. }
  7. }
If it's a non-square matrix then you're SOL.
Sorry. I didn't read your code carfully enough before I replied.
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
murschech is offline Offline
60 posts
since Dec 2004
Feb 15th, 2005
0

Re: Transpose a matrix

>Sorry. I didn't read your code carfully enough before I replied.
Maybe you should run example code to see if it works before trying to correct it with an incorrect solution. Starting the inner loop at 0 instead of i + 1 will result in a lot of shuffling to get the same matrix that you started with, not a transposed matrix as the OP requested. So maybe you should also test your own code before posting it. Especially if you're trying to correct someone and using it as an example.

On a purely stylistic point, I have two issues with your code. First, even though it's valid the way you did it, you should always put braces around a loop or if construct that has more than a one line body. This way you don't have to rely solely on indention to prove that your code is correct. You also avoid certain pitfalls.

Second, starting the statements of a block on the same line as the opening brace is a formatting nightmare. It makes code harder to read and harder to reformat so that it's easier to follow.

Something more like this (following your apparent brace indention style):
  1. for (int i = 0; i < 4; i++)
  2. {
  3. for (int j = 0; j < 4; j++)
  4. {
  5. save = matrix[i][j];
  6. matrix[i][j] = matrix[j][i];
  7. matrix[j][i] = save;
  8. }
  9. }
You also failed to define save before using it, but I'll let you slide on that.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 30th, 2007
0

Re: Transpose a matrix

How to make a program yhat make the transpose of a matrix using array and pointers C ofcourse?
Reputation Points: 8
Solved Threads: 0
Newbie Poster
aldrinsms is offline Offline
3 posts
since Sep 2007
Sep 30th, 2007
0

Re: Transpose a matrix

Wonderful! Be a pompous ass and dredge up an old thread (after reiterating the same demand a 3rd time), offering nothing, and pretend you are being courteous! Well, done. *plonk*
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 23rd, 2008
0

Re: Transpose a matrix

// prog to mulltiply matrix
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[10][10],b[10][10],c[10][10],i,j,k,m,n,m1,n1;
  6. clrscr();
  7. printf("Enter array size ");
  8. scanf("%d%d",&m,&n);
  9. printf("Enter array size 2");
  10. scanf("%d%d",&m1,&n1);
  11. if(n!=m1)
  12. {
  13. printf("Wrong choice entered");
  14. getch();
  15. exit(0);
  16. }
  17. else
  18. {
  19. printf("Enter elements");
  20. for(i=0;i<m;i++)
  21. {
  22. for(j=0;j<n;j++)
  23. {
  24. scanf("%d",&a[i][j]);
  25. }
  26. }
  27. printf("Enter element 2");
  28. for(i=0;i<m1;i++)
  29. {
  30. for(j=0;j<n1;j++)
  31. {
  32. scanf("%d",&b[i][j]);
  33. }
  34. }
  35. for(i=0;i<m;i++)
  36. {
  37. for(j=0;j<n1;j++)
  38. {
  39. c[i][j]=0;
  40. for(k=0;k<n;k++)
  41. {
  42. c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
  43. }
  44. }
  45. }
  46. printf("REQUIRED MATRIX");
  47. for(i=0;i<m;i++)
  48. {
  49. for(j=0;j<n1;j++)
  50. {
  51. printf("\n%d",c[i][j]);
  52. }
  53. printf("\n");
  54. }
  55. getch();
  56. }
  57. }
Last edited by Ancient Dragon; Mar 23rd, 2009 at 3:53 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MANDALSHASHI is offline Offline
3 posts
since Nov 2008
Mar 23rd, 2009
0

Re: Transpose a matrix

Hi there
I read you method of transposing a matrix
it works
but i have toa sk a question!
that what is the logic behind initializing inner loop variable j=i+1 instead of j=0???
i shalll be thankful to you if you reply to this post
thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thinkammar is offline Offline
1 posts
since Mar 2009
Mar 23rd, 2009
0

Re: Transpose a matrix

>>what is the logic behind initializing inner loop variable j=i+1 instead of j=0???

which post # are you talking about?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Positive int != EOF & smallest of it
Next Thread in C Forum Timeline: Prime Number Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC