How to create a matrix using array 3 by 3.

Jul 2, 2011 at 11:47am
Hi! Everyone i'm mark a student taking (I.T). I was given an assignment to create a matrix that is 3 by 3 without explaining how to do it, but instead of complaining i browsed the internet and try to find some sample codes on how to create a simple 3 by 3 matrix.

My instructor gave me this output:

The transpose of a matrix:

(1st)
12 13 14
15 16 17
18 19 11

(2nd)
12 15 18
13 16 17
14 19 11

(3rd)
12 15 18
13 16 19
14 17 11

The interchanging of the rows with the corresponding columns.

I don't know how to create the for loop to interchange the numbers,
can anyone help me? I need a sample code to refer to.

I would be thankful if anyone can give me any sample codes. :)
I'm using Turbo C, C++, I don't know how to use any other programming language.
Thank you in advance for anyone's help.
Last edited on Jul 2, 2011 at 11:54am
Jul 2, 2011 at 12:27pm
If matrix T is a transpose of matrix M, T[i][j] == M[j][i].
First have a for loop for each row, then, inside it, a for loop for each column. Lastly do the assignment with swapped indices (like above).
This could be done without making a copy, but then the for loops would have to be a bit more complicated.
Jul 2, 2011 at 12:28pm
1
2
3
4
5
6
7
8
9
10
int matrix[3][3] = {{1,2,3,}, {4,5,6}, {7,8,9}};


	for (int i = 0; i < 3; i++) {
		for (int j = 0; j <3; j++)

			cout<<"  "<<matrix[j][i];
		cout<<endl;

	}
Jul 2, 2011 at 5:26pm
Hamsterman: Thanks for the help.. need sample code, but really than you :)
Jul 2, 2011 at 5:31pm

Janlan: Thank you for giving me a sample code, its a great help. ^^

I'm kinda new to the cout<<" "<<(array); cout<<endl; function.

Can you tell what its for?

Iv'e seen it in many programs when i searched for this matrix
sample code in webs but still i need further explanation
to understand the full use of it.

Thanks again for the sample code, the for loops a bit confusing :D LOL but i'm getting it now.

I just need to understand the cout function and its all done :)
Jul 2, 2011 at 5:47pm


In addition to my question my compiler don't recognize the #include<iostream>,
and using namespace std; for the cout function.

I guess my compiler doesn't have that library file.

Any other functions that works the same as the cout does?



Whats in my mind is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <stdio.h>
#include <conio.h>

void main(){

int matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}},i,j;

clrscr();

      for(i=0;i<3;i++){
                for(j=0;j<3;j++){

                     printf("%d  ",matrix[i][j]);
                             }
                       printf("\n");
                   }

getch();

}






1  2  3
4  5  6
7  8  9




That is what i know how to print the matrix and this language is what i use to create my program
Turbo C
.

Note:
printf("%d",matrix[i][j]);= printing the value of matrix[i] [j]


clrscr();= clear screen


getch();= get character from the user before exit or proceed to the next funtion


printf("\n");= new line



Question: how to interchange the values of the matrix's?
Last edited on Jul 2, 2011 at 6:09pm
Jul 2, 2011 at 6:15pm
If you just want to print the transpose, change matrix[i][j] to matrix[j][i]. If you want to save the transpose in another array, in the same for loop write transpose[i][j] = matrix[j][i].
Jul 3, 2011 at 1:39am
Hamsterman:

:) thanks man! That's just what i needed.

Its the changing of the index number, :D LOL haven't thought of that.

Thanks for the help guys!
Jul 3, 2011 at 2:58am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <stdio.h>
#include <conio.h>

void main(){

int matrix[3][3]={{12,13,14},{15,16,17},{18,19,11}};
int transpose[3][3];
int i,j;

clrscr();

printf("\n\nThe Given Matrix: \n");

for(i=0;i<3;i++){
	for(j=0;j<3;j++){

		printf("%d  ",matrix[i][j]);

		}

	   printf("\n");
	}

printf("\n\nThe Transposed Matrix: \n");

for(i=0;i<3;i++){
	for(j=0;j<3;j++){

		transpose[i][j]=matrix[j][i];

		printf("%d  ",transpose[i][j]);

			}
	printf("\n");

	}

getch();

}





The Given Matrix:
12  13  14
15  16  17
18  19  11


The Transposed Matrix:
12  15  18
13  16  19
14  17  11




LOL! :D Thank you so much guys!

Mr. Hamsterman and Janlan

Never thought it was that easy. :P

Thanks!
Jul 3, 2011 at 3:51am
Topic archived. No new replies allowed.