|
Qbasic:
REM This program adds and subtracts matrices
REM It uses no commands that are specific to matrices
CLS
PRINT This program will add or subtract matrices.
INPUT Enter the dimensions of the matrices separated by a comma.,m,n
DIM a(m,n)
DIM b(m,n)
REM This loop reads the first matrix
PRINT Please press enter after each element of the matrix.
PRINT Enter all the elements of one row before the next row.
FOR i=1 to m
FOR j=1 to n
INPUT a(i,j)
NEXT j
NEXT i
100 REM This allows the user to choose what to do next
INPUT Do you wish to 1) add 2) subtract or 3) end ,s
IF s=3 then goto 500
200 PRINT Enter the next matrix.
FOR i=1 to m
FOR j=1 to n
INPUT b(i,j)
NEXT j
NEXT i
If s=1 then GOSUB 1000 ELSE IF s=2 THEN GOSUB 2000 ELSE IF s=3
THEN GOTO 500 ELSE GOTO 100
REM The previous line should be part of the line above it
PRINT The resulting matrix is:
FOR i=1 to m
FOR j=1 to n
PRINT a(i,j),
NEXT j
PRINT
NEXT i
PRINT
GOTO 100
500 PRINT Your final solution is:
FOR i=1 to m
FOR j=1 to n
PRINT a(i,j),
NEXT j
PRINT
NEXT i
END
1000 REM This adds matrices
FOR i=1 to m
FOR i=1 to n
a(i,j)=a(i,j)+b(i,j)
NEXT j
NEXT i
RETURN
2000 REM This subtracts matrices
FOR i=1 to m
FOR i=1 to n
a(i,j)=a(i,j)-b(i,j)
NEXT j
NEXT i
RETURN
Pascal:
Program add(input, output);
{This program adds and subtracts matrices.}
uses crt; {Necessary for some Pascal compilers}
type
matrix=array[1..10,1..10] of real;
var
choice : integer; {for choosing operation}
m,n: integer; {dimensions of the matrices}
a,b: 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 addmatrix(var a:matrix; b: matrix);
var
i,j: integer; {counters}
begin {addmatrix}
for i:=1 to m do
for j:=1 to n do
a[i,j]:=a[i,j]+b[i,j];
end;{addmatrix}
procedure submatrix(var a:matrix; b: matrix);
var
i,j: integer; {counters}
begin {submatrix}
for i:=1 to m do
for j:=1 to n do
a[i,j]:=a[i,j]-b[i,j];
end;{submatrix}
procedure menu{(var choice:char)};
begin {menu}
writeln;
writeln('Do you wish to :');
writeln(' 1. Add');
writeln(' 2. Subtract');
writeln(' 3. End the program');
readln(choice);
end;{menu}
procedure operation(var a : matrix);
begin {operation}
writeln('Enter your second matrix.');
writeln('Enter the elements of each row separated');
writeln('by a space. Hit return at the end of each row.');
readmatrix(b,m,n);
if choice = 1 then
addmatrix(a,b)
else if choice = 2 then
submatrix(a,b)
else if choice = 3 then
choice := 3
else if ((choice <> 1) and (choice <> 2) and (choice <> 3)) then
begin {else if}
writeln('That was not a choice.');
choice := 4;
end;{else if}
end;{operation}
begin{main program}
clrscr;
choice := 0;
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);
menu;
while choice <> 3 do
begin {while}
operation(a);
writeln('The resulting matrix is :');
writematrix(a,m,n);
menu
end; {while}
writeln('The final resulting matrix is :');
writematrix(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