|
Qbasic:
REM This program asks for a matrix to be input.
REM It prints the matrix and its transpose.
REM This program uses no commands that are specific to matrices.
CLS
PRINT This program prints your matrix and its transpose.
INPUT Enter the dimensions of the matrix separated by a comma.,m,n
DIM a(m,n)
PRINT
PRINT Please press enter after each element of the matrix.
PRINT Enter all the elements of one row before the next row.
REM This loop reads the matrix
FOR i=1 to m
FOR j=1 to n
INPUT a(i,j)
NEXT j
NEXT i
REM This loop prints the matrix
PRINT
PRINT This is the matrix that you entered:
FOR i=1 to m
FOR j=1 to n
PRINT a(i,j),
NEXT j
PRINT
NEXT i
REM This loop prints the transpose of the matrix
PRINT
PRINT This is the transpose of your matrix:
FOR j=1 to n
FOR i=1 to m
PRINT a(i,j),
NEXT i
PRINT
NEXT j
END
Pascal:
Program intro(input, output);
{This program asks for a matrix to be input.
It prints the matrix and its transpose.
This program uses no commands that are specific to matrices.}
uses crt; {Necessary for some Pascal compilers}
type
matrix=array[1..10,1..10] of real;
var
m,n: integer; {dimensions of the matrices}
a: matrix; {matrix}
procedure readmatrix(var a:matrix; m,n:integer);
var
i,j: integer; {counters}
begin {read}
for i:=1 to m do
begin {do}
for j:=1 to n do
read(a[i,j]);
readln;
end; {do}
end; {read}
procedure writematrix(a:matrix; m,n:integer);
var
i,j: integer; {counters}
begin {write}
for i:=1 to m do
begin {each line}
writeln;
for j:=1 to n do
write(a[i,j]:6:2);
end; {each line}
writeln;
end; {write}
procedure writetranspose(a:matrix; m,n:integer);
var
i,j: integer; {counters}
begin {write}
for j:=1 to n do
begin {each line}
writeln;
for i:=1 to m do
write(a[i,j]:6:2);
end; {each line}
writeln;
end; {write}
begin{main program}
clrscr;
writeln('Enter the dimensions of the matrix ');
writeln('separated by a space. Then hit return.');
read(m,n);
writeln('Enter your matrix.');
writeln('Enter each element followed by a return.');
writeln('Enter the first row before you go to the next row.');
readmatrix(a,m,n);
writeln('The matrix that you entered is :');
writematrix(a,m,n);
writeln('The transpose of your matrix is :');
writetranspose(a,m,n);
writeln('Press return to leave the program');
readln;
end. {main program}
|
Send comments on material to Tamara Carter
These pages are maintained by Hilena Vargas
(hvargas@rice.edu)
Updated: September 21, 2000
Copyright ©1995 - 2000 Tamara Lynn Anthony