matrix transpose - cout << endl...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <cstdlib> #include <iostream> using namespace std; void matrixTranspose(float **M, int n); int main(int argc, char *argv[]) { float M[4][4] = { {11,12,13,14}, {21,22,23,24}, {31,32,33,34}, {41,42,43,44} }; int i,j, n = 4; float ** pm; pm = new float *[n]; // allocate n row pointers for(i = 0; i < n; i++) pm[i] = M[i]; // allocate the ith row cout << "Initial: M[" << n << "][" << n << "]" << endl; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) cout << " " << pm[i][j] << " "; cout << endl; } matrixTranspose(pm, n);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cout &lt;&lt; endl &lt;&lt; &quot;Transpose: M[&quot; &lt;&lt; n &lt;&lt; &quot;][&quot; &lt;&lt; n &lt;&lt; &quot;]&quot; &lt;&lt; endl; for(i = 0; i &lt; n; i++) { for(j = 0; j &lt; n; j++) cout &lt;&lt; &quot; &quot; &lt;&lt; pm[i][j] &lt;&lt; &quot; &quot;; cout &lt;&lt; endl; } system(&quot;PAUSE&quot;); return EXIT_SUCCESS; } void matrixTranspose(float **M, int n) { for(int i = 0; i &lt; n-1; i++) // start with 1st row to the (n-1)th row for(int j = i+1; j &lt; n; j++) // start with 2nd column to the nth column swap(M[i][j],M[j][i]); // t = M[i][j]; // M[i][j] = M[j][i]; // M[j][i] = t; }...
View Full Document

Ask a homework question - tutors are online