| | |
Transpose a matrix
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
If it's a square matrix then you can do it with something like this:
If it's a non-square matrix then you're SOL.
C Syntax (Toggle Plain Text)
for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { int save = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = save; } }
New members chased away this month: 5
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Narue
If it's a square matrix then you can do it with something like this:
If it's a non-square matrix then you're SOL.C Syntax (Toggle Plain Text)
for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { int save = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = save; } }
C Syntax (Toggle Plain Text)
for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { save = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = save; }
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Narue
If it's a square matrix then you can do it with something like this:
If it's a non-square matrix then you're SOL.C Syntax (Toggle Plain Text)
for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { int save = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = save; } }
>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):
You also failed to define save before using it, but I'll let you slide on that.
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):
C Syntax (Toggle Plain Text)
for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { save = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = save; } }
New members chased away this month: 5
// prog to mulltiply matrix
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n,m1,n1; clrscr(); printf("Enter array size "); scanf("%d%d",&m,&n); printf("Enter array size 2"); scanf("%d%d",&m1,&n1); if(n!=m1) { printf("Wrong choice entered"); getch(); exit(0); } else { printf("Enter elements"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("Enter element 2"); for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n1;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } } } printf("REQUIRED MATRIX"); for(i=0;i<m;i++) { for(j=0;j<n1;j++) { printf("\n%d",c[i][j]); } printf("\n"); } getch(); } }
Last edited by Ancient Dragon; Mar 23rd, 2009 at 3:53 pm. Reason: add code tags
![]() |
Similar Threads
- Transpose & Inverse Matrix, Matrix Determinant (C++)
- how to transpose a matrix (C)
- matrix transpose (C)
- How to transpose a matrix? (Java)
Other Threads in the C Forum
- Previous Thread: program toswap two no...
- Next Thread: can anyone help me in bus reservation system
Views: 26544 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable visualstudio voidmain() wab win32 windows.h






