You are on page 1of 6

QBASIC Programs:

REM Matrix manipulation programs



CLS
INPUT "Enter row number: ", m
INPUT "Enter column number: ", n

OPTION BASE 1
DIM x(m, n)

FOR i = 1 TO m
FOR j = 1 TO n
INPUT x(i, j)
NEXT j
NEXT i

REM Displaying matrix elements and their sum

PRINT "elements of matrix are:"

FOR i = 1 TO m
FOR j = 1 TO n
PRINT x(i, j);
sum = sum + x(i, j)
NEXT j
PRINT
NEXT i
PRINT "Sum of the elements is: "; sum

REM Displaying principle diagonal elements and their sum

IF m = n THEN
sum = 0
PRINT "The principle diagonal elements are: "
FOR i = 1 TO m
FOR j = 1 TO n
IF i = j THEN
PRINT x(i, j);
sum = sum + x(i, j)
ELSE
PRINT " ";
END IF
NEXT j
PRINT
NEXT i
PRINT "Sum of the principle diagonal elements is: "; sum
ELSE
PRINT "Principle diagonal does not exist"
END IF

REM Displaying boundary elements and their sum

sum = 0
PRINT "The boundary elements are: "

FOR i = 1 TO m
FOR j = 1 TO n
IF i = 1 OR i = m OR j = 1 OR j = n THEN
PRINT x(i, j);
sum = sum + x(i, j)
ELSE
PRINT " ";
END IF
NEXT j
PRINT
NEXT i
PRINT "Sum of the boundary elements are: "; sum

REM Finding transpose of a matrix and displaying it

PRINT "Transpose of the matrix is: "

FOR i = 1 TO n
FOR j = 1 TO m
PRINT x(j, i);
NEXT j
PRINT
NEXT i

REM Multiplying the matrix by an scalar

INPUT "Enter a value for scalar: ", k
PRINT "The matrix after multiplying by an scalar is: "

FOR i = 1 TO n
FOR j = 1 TO n
PRINT k * x(i, j);
NEXT j
PRINT
NEXT i






REM Square matrix examples

CLS
INPUT "Enter size of square matrix: ", n

OPTION BASE 1
DIM x(n, n)

PRINT "Unit matrix of size "; n; "x"; n; " is: "

FOR i = 1 TO n
FOR j = 1 TO n
IF i = j THEN
PRINT 1;
ELSE
PRINT 0;
END IF
NEXT j
PRINT
NEXT i

REM Entering and displaying elements of diagonal matrix and
its sum

PRINT "Enter elements of a diagonal matrix: "

FOR i = 1 TO n
FOR j = 1 TO n
IF i = j THEN
INPUT x(i, j)
ELSE
x(i, j) = 0
END IF
NEXT j
NEXT i

PRINT "The diagonal matrix is: "
FOR i = 1 TO n
FOR j = 1 TO n
PRINT x(i, j);
sum = sum + x(i, j)
NEXT j
PRINT
NEXT i

PRINT "The sum of the elements of diagonal matrix is: "; sum

REM Entering and displaying the elements of triangular matrix
and its sum

PRINT "Enter elements of upper triangular matrix: "
FOR i = 1 TO n
FOR j = 1 TO n
IF i <= j THEN
INPUT x(i, j)
ELSE
x(i, j) = 0
END IF
NEXT j
NEXT i
PRINT "The upper triangular matrix is: "
sum = 0
FOR i = 1 TO n
FOR j = 1 TO n
PRINT x(i, j);
sum = sum + x(i, j)
NEXT j
PRINT
NEXT i
PRINT "Sum of the elements of upper triangular matrix is:"; sum

PRINT "Enter elements of lower triangular matrix: "
FOR i = 1 TO n
FOR j = 1 TO n
IF i >= j THEN
INPUT x(i, j)
ELSE
x(i, j) = 0
END IF
NEXT j
NEXT i
PRINT "The lower triangular matrix is: "
sum = 0
FOR i = 1 TO n
FOR j = 1 TO n
PRINT x(i, j);
sum = sum + x(i, j)
NEXT j
PRINT
NEXT i
PRINT "Sum of the elements of lower triangular matrix is:"; sum





REM Working with square matrices

CLS
INPUT "Enter size of square matrix: ", n

OPTION BASE 1
DIM x(n, n)

REM Enter and displaying the elements of symmetric matrix

PRINT "Enter elements of a symmetric matrix: "
FOR i = 1 TO n
FOR j = 1 TO n
IF i <= j THEN
INPUT x(i, j)
END IF
NEXT j
NEXT i

FOR i = 1 TO n
FOR j = 1 TO n
IF i > j THEN
x(i, j) = x(j, i)
END IF
NEXT j
NEXT i

PRINT "The symmetric matrix is: "
FOR i = 1 TO n
FOR j = 1 TO n
PRINT x(i, j);
NEXT j
PRINT
NEXT i

REM Entering and displaying the elements of a skew-symmetric
matrix

PRINT "Enter the elements of skew-symmetric matrix: "
FOR i = 1 TO n
FOR j = 1 TO n
IF i <= j THEN
INPUT x(i, j)
END IF
NEXT j
NEXT i

FOR i = 1 TO n
FOR j = 1 TO n
IF i > j THEN
x(i, j) = 0 - x(j, i)
END IF
NEXT j
NEXT i

PRINT "The skew-symmetric matrix is: "
FOR i = 1 TO n
FOR j = 1 TO n
PRINT x(i, j);
NEXT j
PRINT
NEXT i

You might also like