Sei sulla pagina 1di 60

CHAPTER ONE

1. INTRODUCTION
Before one can design a method of solving systems of linear
equations it is imperative to have a good knowledge of the way the
system works manually. In the computer world emphasis is on the
software development sector as such researchers most a times design
soft ware applications either from the scratch or improve on an already
existing one rather than suggesting an improvement that is within the
scope of manual operation. One salient benefit of computerized system is
it’s effectiveness in handling mathematical computations and large
number systems of equations.
As the knowledge of coding (writing of programs) increases,
computerization is no more restricted to routine activities of adding,
subtracting, dividing and multiplying trivial numbers. Non-trivial (non-
arithmetic) algebras has come to lime light; as the days go by these
algebras become more complicated as they require a lot of calculations
and manipulation. Consequently the programming approach was devised
in which computer software’s go through certain specified procedures to
produce solution sets to certain mathematical problems.

1.2 AIM AND OBJECTIVE OF STUDY


The manual approach of solving systems of linear equation is
tedious, time consuming, boring, requires a level of expertise and is
pains-taking even prone to errors. Despite the fact that these are some
of the virtues of mathematics that distinguishes the mathematician from
the philosopher, the objective of this work is to code a computer software
application to carry out these tasks without much stress, but requires
only the input of values and the solution set come rolling out with high
speed millions of times faster than the manual calculation. It is also
error free so long as the application (i.e. the program) runs.

1
1.3 STATEMENT OF PROBLEMS
Many situations in physical science can be described approximately
or exactly by a set of n system of linear equation in n variables
(unknown), xi, i = 1, 2, …, n. The equations take the general form
a11x1 + a12x2 + … + a1nxn = b2
a21x1 + a22x2 + … + a2nxn = b2
: : : :
: : : :
an1x1 + an2x2 + … + annxn = bx ..................................................(1.1)
where Aij are constants which forms the elements of a square matrix A.
the bi are given and form a column matrix B. If A is define then the
equation can be sowed for the xi using the inverse of A according to the
formula
x = A-1B
Before the application of software in the solving of complex
problems, a lot of scientific theories and complex mathematical problems
{the computation of the Systolic Blood pressure (SBP) and age for a
Sample of = n individuals
b: the observed systolic blood pressure (SBP)
a1 = 1 ……………………….(1.2)
a2 = The ages
the equation Ax = b is of the form
x0a1 + x1a2 = bi ..........................................................(1.3)
Where 1 is an n-column of 1’s and x0 and x1 are the elements of x} wher
difficult to carry out because of the time consuming nature of the
problems and possibility of error in manual calculation.( Nabih N.
Abdelmalek, Ph.D., 2008)

2
1.4 SIGNIFICANCE OF THE STUDY
This project will not only expose students and lecturers to the
programming approach in solving system of linear equation but will also
make students gallop over the fulfilled C++ programming language. That
is an interpreter rather than a compiled language capable of inculcating
into itself several other programming languages making it very flexible
and programmer friendly.

1.5 SCOPE AND LIMITATION OF THE STUDY


This project work covers not just the manual solution of n systems
of linear equations in n variables, but also treats the algorithmic setout,
an object oriented C++ encoding to handle systems of n linear equations
with four n variables that have unique solution excluding the
homogeneous system of linear equations.
However, the study will be limited to two of the matrix methods for
solving linear systems, which are Crammer’s rule and the Gaussian
elimination method.

3
CHAPTER TWO

REVIEW OF RELATED LITERATURES


There has being countless works on Systems Of Linear Equation (SOLE)
by various authors many of which looked basically on the manual
computation and others on the computerized aspect of it. Taking for
example Lay David in his book “ Linear Algebra and it’s Application ”
(Lay, David C. 2005) summarises SOLE considering the matrix operation
in the following areas:
2.1 Systems Of Linear Equations

An important problem in Linear Algebra is in solving the system of m

linear equations of n variables in a field F:

a11x1 + a12x2 + ... + a1nxn = b1

a21x1 + a22x2 + ... + a2nxn = b2


: : : : : : : :
: : : : : : : :
am1x1 + am2x2 + ... + amnxn =bm …………………………...(2.1)

Where all aij are elements of a field

An example of a system of linear equations is in equation 2.2

2x1 − x2 + 1.5x3 = 8

x1 − 4x3 = − 7 …………………………………………(2.2)

A solution of the system is a list of numbers that makes


each equation true, when the values s1,s2,...sn are substituted for x1,...,xn
respectively. For example, is a solution to the equation (2.2)
because, when these values are substituted in equation (2.2) for x1, x2, x3
respectively, the equations simplify to 8 = 8 and − 7 = − 7.
The set of all possible solutions is called the solution set of the linear
system. Two linear systems are called equivalent if they have the same

4
solution set. That is, each solution of the first system is a solution to the
second system, and vice versa.
Finding the solution set of a system of two linear equations in two
variables is easy because it is the same as finding the intersection point
of two lines. A typical problem is
x1 − 3x2 = − 1
− x1 + 4x2 = 3………………………………………………………(2.3)
The graphs of these lines, which is denote by l1 and l2. A pair of numbers
statisfies both equations in the system if and only if the point
lies on both l1 and l2. In the system above, the solution is the
single point , as you easily can see on

Here the n unknowns xi,j = 1, 2, . . .,n are related by m equations. The


coefficients aij with i = 1, 2, . . .,m and j = 1, 2, . . .,n are known numbers,
as are the right-hand side quantities bi,j = 1, 2, . . .,m.
2.1.1 NONSIGULAR VERSUS SINGULAR SETS OF
EQUATIONS
If n = m then there are as many equations as unknowns, and there is a
good chance of solving for a unique solution set of xj ’s. Analytically,
there can fail to be a unique solution if one or more of the m equations is
a linear combination of the others, a condition called row degeneracy, or
if all equations contain certain variables only in exactly the same linear
combination, called column degeneracy.
(For square matrices, a row degeneracy implies a column degeneracy,
and vice versa.) A set of equations that is degenerate is called singular.
Numerically, at least two additional things can go wrong:
1. While not exact linear combinations of each other, some of the
equations may be so close to linearly dependent that roundoff errors in
the machine render them linearly dependent at some stage in the
solution process. In this case your numerical procedure will fail, and it
can tell you that it has failed.

5
2. Accumulated roundoff errors in the solution process can swamp the
true solution. This problem particularly emerges if n is too large. The
numerical procedure does not fail algorithmically. However, it returns a
set of x’s that are wrong, as can be discovered by direct substitution back
into the original equations. The closer a set of equations is to being
singular, the more likely this is to happen, since increasingly close
cancellations will occur during the solution. In fact, the preceding item
can be viewed as the special case where the loss of significance is
unfortunately total.
Much of the sophistication of complicated “linear equation-solving
packages” is devoted to the detection and/or correction of these two
pathologies. As you work with large linear sets of equations, you will
develop a feeling for when such sophistication is needed. It is difficult to
give any firm guidelines, since there is no such thing as a “typical” linear
problem. But here is a rough idea: Linear sets with n as large as 20 or 50
can be routinely solved in single precision (32 bit floating
representations) without resorting to sophisticated methods, if the
equations are not close to singular. (Poole, David 2005)
2.1.2 HOMOGENOUS AND NON-HOMOGENOUS
A homogeneous system of linear equations is one with all LHSs linear in
the variable, and all RHSs exactly 0:
a1,1x1 + ... + a1,nxn = 0
a2,1x1 + ... + a2,nxn = 0
:: : : : :
am,1x1 + ... + am,nxn = 0 …………………………….(2.4)
Of course, many systems of linear equations, such as
x+y=z
z – x = y ………………………………………………….(2.5)
may be brought into the above form by manipulating them. The
important thing is that no “constants” appear in the equations, or that

6
any constants that do appear may be cancelled out within that equation
(e.g. "x+y+4=z+4").
The importance of homogeneous systems of linear equations is that they
provide a cornerstone for solving general systems of linear equations. A
homogeneous system always has a solution: set all variables to 0. This is
the trivial solution. It may, however, have more solutions. And if you
squint at it from just the right angle, you see that its set of solutions is a
vector space: adding solutions or multiplyng a solution by a constant (a
scalar) give solutions, too.
Suppose you know how to solve the general homogeneous system above,
and find all the solutions. What about the general system

a1,1x1 + ... + a1,nxn = b1


a2,1x1 + ... + a2,nxn = b2
: : : :
am,1x1 + ... + am,nxn = bm ……………………(2.6)

First off, it might not have any solutions:


x+y=1
2x + 2y = 2 ………………………………………(2.7)
It turns out that a solution might not exist iff the homogeneous system
has infinitely many solutions (i.e. more than just the trivial all-0
solution).
But suppose it does, and we know just one particular solution (xi=ci)i=1n
for the general system. Then for any solution of the homogeneous system
(xi=yi)i=1n, it's easy to see that (xi=ci+yi)i=1n is also a solution to that
general system. Nicer yet, you can get all the solutions in this way.

2.1.3 CONSISTENT AND INCONSISTENT SYSTEMS


A system of linear equations is said to be consistent if it has either one
solution or infinitely many solutions, and a systems is said to be

7
inconsistent if it has no solution. In Linear Algebra, we are concerned
with three problems:
1. Is a system of linear equations consistent or not?
2. If so, how many elements are in the solution set?
3. What is the solution set?

Systems of Linear
Equations

Inconsisten Consistent

No Solution Unique Solution More than one


solution
Figure: 2.1

Figure 2.2: Exactly one


solution
Of course, two lines does not have to intersect in a single point, they
could be parallel, or they could coincide and "intersect" at every point on
the line. Figure 2.3 and Figure 2.4 shows graphs to visualize this.

8
Figure 2.3: No solution

Figure2.4:
Infinitely many
solutions
2.1.4 MOTIVATIONS
Systems of linear equations are common in science and mathematics.
These two examples from high school science give a sense of how they
arise.The first example is from Physics. Suppose that we are given three
objects, one with a mass known to be 2 kg, and are asked to find the
unknown masses. Suppose further that experimentation with a meter
stick produces these two balances.

Fig.2.5

Since the sum of moments on the left of each balance equals the sum of
moments on the right (the moment of an object is its mass times its

9
distance from the balance point), the two balances give this system of
two equations.

40h + 15c = 100


25c = 50 + 50h ………………………………………..(2.8)

The second example of a linear system is from Chemistry. We can mix,


under controlled conditions, toluene C7H8 and nitric acid HNO3 to
produce trinitrotoluene C7H5O6N3 along with the byproduct water
(conditions have to be controlled very well, indeed— trinitrotoluene is
better known as TNT). In what proportion should those components be
mixed? The number of atoms of each element present before the reaction

must equal the number present afterward. Applying that principle to the

elements C, H, N, and O in turn gives this system

……..........................…(2.9)

To finish each of these examples requires solving a system of equations.


In each, the equations involve only the first power of the variables. This
gives an introduction to systems of linear equations. We will solve them
later. (Hansford, Dianne, 2004)

2.2 MATRIX NOTATION

The essential information of a linear system can be described in a


rectangular array called a matrix. Given the system

10
3x1 + 5x2 + 2x3 = 0

x1 − 8x2 = 10

− 2x2 + x3 = − 8 ………………………………(2.10)

with the coefficients of each variable aligned in columns, we make a


matrix called the coefficient matrix (or matrix of coefficients) of the
system. The coefficient matrix looks like this

Which make sense if you look at the definition of a linear equation. The
second row contains a zero because the second equation could be written
as x1 − 8x2 + 0x3 = 10
We also have a matrix called the augmented matrix which for the same
system looks like this

An augmented matrix of a system consists of the coefficient matrix with


an added column containing the constants from the right sides of the
equation. Again the size of a matrix tells us how many rows and
columns it has. The augmented matrix above has 3 rows and 4 columns;
therefore it is called a 3x4 (read "3 by 4") matrix. m and n are positive
integers, an m x n matrix is a rectangular array of numbers with m rows
and n columns. Matrix notation will simplify the calculations of a linear
system.

2.2.1 ELEMENTARY ROW OPERATIONS


There are three elementary row operations:
i. Replacement
ii. Interchanging
iii. Scaling
11
The following are an explanation and examples of the three different
operations. An important thing to remember is that all operations can be
used on all matrices, not just on matrices derived from linear systems.
i. Replacement
Replace one row by the sum of itself and a multiple of another row. A
more common paraphrase of row replacement is "Add to one row a
multiple of another row."
An example is, we are given the linear system:
x1 + 4x2 = 3
2x1 + 2x2 = 4 …………………………………(2.11)

Which can be written in matrix notation as an augmented matrix, like


this

Now we have decided to eliminate the x1 term in equation 2, this can be


done by adding -2 times equation 1 to equation 2

Which gives us the matrix

ii. Interchanging
Interchange two rows.
An example is, we are given the matrix

Here we have performed an interchange operation on the two rows

12
This is a useful operation when you are trying to solve a linear system,
and can see that it will be easier to solve it by interchanging two rows. It
is a widely used operation, even though it seems odd and not very
usable.
iii. Scaling
Multiply all entries in a row by a nonzero constant. An example is, we are
given the matrix

Now a scaling operation has been performed on the first row, by


multiplying by -2

2.2.2 SOLVING LINEAR SYSTEM


The basic strategy for solving linear systems is to replace one system
with an equivalent system (i.e. another system with the same solution
set) that is easier to solve. This is done by using x1 term from the first
equation to eliminate the x1 terms in the other equations, use the x2 term
from the second equation to eliminate the x2 terms in the other
equations, and so forth, until you get a very simple equivalent system of
equations. There are three operations that are used to simplify a system.
You can replace one equation by the sum of itself and a multiple of
another equation, we can interchange two equations, and multiply all the
terms in an equation with a nonzero constant. During this example, we
can see why these operations do not change the solution set of a system.

Example 1: Solve this linear system


x1 − 2x2 + x3 = 0
2x2 − 8x3 = 8
− 4x1 + 5x2 + 9x3 = − 9 …………………………………(2.12)
The augmented matrix for this system is

13
The first thing we want is keep x1 in the first equation and eliminate it
from the other equations. To achieve this, we add 4 times equation 1 to
equation 3.

The result of this calculation is written in place of the original equation 3

Next thing we want to do is to multiply equation 2 with to obtain 1 as


the coefficient for x2, which will simplify arithmetic in the next step

Now, we use x2 in equation 2 to eliminate the − 3x2 in equation 3

The new linear system has a triangular form, which is called echelon
form, and looks likse this

The next step is to use x3 in equation 3 to eliminate the x3 and − 4x3 in


equation 1 and 2.

The system now looks like this

14
Now we are just one step away from obtaining the solution of the linear
system. The last step is to add 2 times equation 2 to equation 1. When
doing so we obtain the linear system, which has a reduced echelon form

Now that the linear system is solved, it shows that the only solution of
the original system is (29,16, 3). However, because of the many
calculations involved, it is a good practice to check your work. This is
done by substituting the solution into the original system
(29) − 2(16) + (3) = 29 − 32 + 3 = 0
2(16) − 8(3) = 32 − 24 = 8
− 4(29) + 5(16) + 9(3) = − 116 + 80 + 27 = − 9
This shows us that the solution we found is right, and therefore is a
solution of the original linear system.(Meyer, Carl D. 2001)

15
CHAPTER THREE
SYSTEM ANALYSIS/PROGRAM DESIGN
As was mentioned in chapter one we are going to concentrate
broadly on two matrix approach in the solving of systems of linear
equation and the alternative design for the various methods.
3.1 ELEMENTS OF MATRIX ALGEBRA
In working with a system of linear equations, only the coefficients
and their respective positions are of great important. It is essential to
keep the equations carefully aligned. Thus these coefficients can be
efficiently arranged in a rectangular array of numbers embraced with
brackets (real or complex) called matrix. These matrices and certain
algebraic operations defined on them shows that they are computational.
The matrix are being represented by upper case letters say A, B, C e.t.c.
while the numbers called entries or elements are represented by lower
case letters say aij, bij, cij respectively. For example, let K be an arbitrary
field. A rectangular array of the form
a11 a12 …………. A1n
a21 a22 …………. a2n
…. …. ………… ….

am1 am2 ……… amn


where the aij are scalars in K, is called a matrix over K, or simply a
matrix if K is implicit. The above matrix is also denoted by (aij), i=1,…,
m, j = 1,…, n, or simply by (aij). The m horizontal n-tuples
(a11 a12 ……. a1n), (a21 a22 ……. a2n), …, (am1 am2 …….. amn)

are the rows of the matrix, and the n vertical m-tuples

a11 a12 a1n


a21 a22 a2n
… , … , ……….., …

am1 am2 amn

16
are its columns. Note that the element aij, called the ij-compontent,
appears in the ith row and the jth column. A matrix with m rows and n
columns is called an m by n matrix, or mxn matrix; the pair of numbers
(m,n) is called its size and shape.
3.1.1 MATRIX TERMS OFTEN USED IN LINEAR SYSTEMS
i. Square Matrix: A matrix with the same number of rows as
columns is called a square matrix. A square matrix with n columns
is said to of order n, and is called n-square matrix. The diagonal
(or: main diagonal) of the n-square matrix A = (aij) consists of the
elements a11, a22,…, ann
ii. Determinant of Matrix: To every square matrix A over a field K
there is assigned a specific scalar called the determinant of A; it is
usually denoted by
det (A) or |A|
and we define det (A) to be the sum of all signed elementary
products from A for one by one matrix or a singleton vector, that is, if A
= { aij}, then det(A) = aij.
For a two by two matrix (2x2)
A= a11 a12
a21 a22 …………………………………………(3.1)

det (A) = a11 a12


a21 a22 ……………………………………….(3.2)

det (A) = a11 x a22 – a12 x a21


but this is not the case for higher orders of matrix say for a three by
three (3x3) we will use two methods to get this:
(a) Conventional method: To illustrate determinant by the
conventional method we follow the procedure shown below,
consider a 3x3 matrix of the form

17
a11 a12 a13
A= a21 a21 a23
a31. a32 a33 ……………………………………………..(3.3)

a11 a12 a13


det(A) = a21 a22 a23
a31 a32 a33 ……………………………………………….(3.4)

a22 a23 a21 a23 a21 a22


det(A) = a11 - a12 + a13
a32 a33 a31 a33 a31 a32 …………(3.5)

det(A) = a11(a22 x a33 – a32 x a23)- a12 (a21 x a33 – a23 x a31)+ a13(a21
x a32 – a22 x a31)
(b) Sarrous rule: To avoid memorizing the unwieldy expression,
this method
suggest using the mnemonic devices obtained by recopying the
first and second column and compute the determinant by
summing the product on the rightward arrow and subtracting the
product on the leftward arrows. Consider the matrix A in
equation 3.3, the determinant using Sarrus rule is illustrated
below
det (A) = a11 a12 a13 a11 a12
a21 a22 a23 a21 a22
a31. a32 a33 a31. a32 ……………………(3.6)

det (A) = (a11 x a22 x a33 + a12 x a23 x a31 + a13 x a21 x a32)-(a13 x a22 x
a31 + a11 x a23 x a32 + a12 x a21 x a33).
iii. Augmented Matrix: Suppose we have n linear equations with n
unknowns, that is, x1, x2 , ……….., xn over a field K then the
system can be written in compact form as Ax = B. The coefficient
matrix (A) and the constant vector (B) form what is called
augmented matrix written as (A|B). ie

18
a11 a12……… a1n b1
a21 a22 ……… a2n
a31 a32 ………a3n b3

3.1.2 CHOOSEN METHODS


In the previous chapters we have stated that there are several
methods for solving systems of linear equation. Nevertheless I to
concentrate on matrix method and most especially the Crammers rule
and the Gaussian elimination method of solution.
i. Crammers Rule
This is a procedure for solving systems of linear equations by means of
determinant. It is a generalized method for the solution of n linear
equations in n unknown. If A is a matrix, then Crammers rule states that
xj = det (Aj)/det (A) where det(A) is the determinant of the coefficient
matrix and det(Aj) is the determinant of the coefficient matrix with the jth
column replaced with the column vector (B) to illustrate this consider the
following system:
a11 x1 + a12 x2 + a13 x3 = b1
a21 x1 + a22 x2 + a23 x3 = b2 ……………………………(3.7)
a31 x1 + a32 x2 + a33 x3 = b3
The solution of the equation above by determinant can be gotten from
the following theorem
Theorem 1: If the determinant of the coefficient matrix A of a system of n
linear equations in n unknown is not zero, then the equation have a
unique solution. Hence for equation (3.7) if the determinant is not zero,
then the values of the unknown x1, x2, and x3 can be obtained as follows
a11 a12 a13 x1 b1
a21 a22 a23 x x2 = b2
a31. a32 a33 x3 b3 …………………….(3.8)
19
Axx=B
The determinant of the coefficient matrix A will be

a11 a12 a13


det(A) = a21 a22 a23
a31. a32 a33
while the determinant det(Aj) of the jth column obtained by substituting
the column vector (B) into the first, second and the third column of
matrix A. thus
b1 a12 a13 a11 b1 a13
det(Ax1) = b2 a22 a23 det(Ax2) = a21 b2 a23
b3. a32 a33 a31 b3 a33

a11 a12 b1
det(Ax3) = a21 a22 b2
a31. a32 b3

Thus the solution of the equations is;

x1 = det(Ax1)/ det(A), x2 = det(Ax2)/det(A) and x3= det(Ax3)/ det(A).

ii. Gaussian Elimination Method


This is another method of solving linear systems. It is called the
Gaussian elimination method, named after Karl Fredrich Gauss, one of
the greatest mathematician who ever lived. Once again considering
equation (2.1) for n number of equations with n unknown we have
a11x1 + a12x2 + ... + a1nxn = b1
a21x1 + a22x2 + ... + a2nxn = b2
... … .. .. … . …………………(3.9)
an1x1 + an2x2 + ... + annxn = bn

20
which gives an augmented matrix B of the given set of equations.
a11 a12 ... a1n b1
B= a21 a22 ... a2n b2
... … .. …
an1 an2 ... ann bn
(a) Next is to eliminate the elements other than a11 from the first
column by subtracting a21/ a11 times the first row from the second
row and a31/ a11 times the first row from the third row, etc.
(b) This gives a new matrix of the form:
a11 a12 ... a1n b1
0 a22 ... a2n d2
... … .. .. …
0 an2 ... ann dn
The process is then repeated to eliminate ci2 from the third and
subsequent row. Then
(c) This will give a lower triangle matrix which will now give room for
Back substitution to get our variables.
3.2 PROGRAME DESIGN
Computer programming (often shortened to programming or coding) is
the process of writing, testing, debugging/troubleshooting, and
maintaining the source code of computer programs. This source code is
written in a programming language. The code may be a modification of
an existing source or something completely new. The purpose of
programming is to create a program that exhibits a certain desired
behavior (customization). The process of writing source code requires
expertise in many different subjects, including knowledge of the
application domain, specialized algorithms and formal logic.
3.2.1 Overview
Within software engineering, programming (the implementation) is
regarded as one phase in a software development process. There is an

21
ongoing debate on the extent to which the writing of programs is an art,
a craft or an engineering discipline. (Paul Graham, 2003)Good
programming is generally considered to be the measured application of
all three, with the goal of producing an efficient and evolvable software
solution (the criteria for "efficient" and "evolvable" vary considerably). The
discipline differs from many other technical professions in that
programmers generally do not need to be licensed or pass any
standardized (or governmentally regulated) certification tests in order to
call themselves "programmers" or even "software engineers".
Another ongoing debate is the extent to which the programming language
used in writing computer programs affects the form that the final
program takes. This debate is analogous to that surrounding the Sapir-
Whorf hypothesis(Kenneth E. Iverson, 1980) in linguistics, that postulates
that a particular language's nature influences the habitual thought of its
speakers. Different language patterns yield different patterns of thought.
This idea challenges the possibility of representing the world perfectly
with language, because it acknowledges that the mechanisms of any
language condition the thoughts of its speaker community. Said another
way, programming is the craft of transforming requirements into
something that a computer can execute.
3.2.2 History of programming
The earliest programmable machine (that is a machine whose behavior
can be controlled by changes to a "program") was Al-Jazari's
programmable humanoid robot in 1206. Al-Jazari's robot was originally a
boat with four automatic musicians that floated on a lake to entertain
guests at royal drinking parties. His mechanism had a programmable
drum machine with pegs (cams) that bump into little levers that operate
the percussion. The drummer could be made to play different rhythms
and different drum patterns by moving the pegs to different locations.
Another sophisticated programmable machine by Al-Jazari was the castle
clock.

22
The Jacquard Loom, developed in 1801, is often quoted as a source of
prior art. The machine used a series of pasteboard cards with holes
punched in them. The hole pattern represented the pattern that the loom
had to follow in weaving cloth. The loom could produce entirely different
weaves using different sets of cards. The use of punched cards was also
adopted by Charles Babbage around 1830, to control his Analytical
Engine.( Al-Jazari, Web1)
This innovation was later refined by Herman Hollerith who, in 1896
founded the Tabulating Machine Company (which became IBM). He
invented the Hollerith punched card, the card reader, and the key punch
machine. These inventions were the foundation of the modern
information processing industry. The addition of a plug-board to his
1906 Type I Tabulator allowed it to do different jobs without having to be
rebuilt (the first step toward programming). By the late 1940s there were
a variety of plug-board programmable machines, called unit record
equipment, to perform data processing tasks (card reading). The early
computers were also programmed using plug-boards.
The invention of the Von Neumann architecture allowed computer
programs to be stored in computer memory. Early programs had to be
painstakingly crafted using the instructions of the particular machine,
often in binary notation. Every model of computer would be likely to need
different instructions to do the same task. Later assembly languages
were developed that allows the programmer to specify each instruction in
a text format, entering abbreviations for each operation code instead of a
number and specifying addresses in symbolic form (e.g. ADD X, TOTAL).
In 1954 Fortran, the first higher level programming language, was
invented. This allowed programmers to specify calculations by entering a
formula directly (e.g. Y = X*2 + 5*X + 9). The program text, or source, was
converted into machine instructions using a special program called a
compiler. Many other languages were developed, including ones for
commercial programming, such as COBOL. Programs were mostly still

23
entered using punch cards or paper tape. By the late 1960s, data storage
devices and computer terminals became inexpensive enough so programs
could be created by typing directly into the computers. Text editors were
developed that allowed changes and corrections to be made much more
easily than with punch cards.
As time progressed, computers have made giant leaps in the area of
processing power. This has brought about newer programming languages
that are more abstracted from the underlying hardware. Although these
more abstracted languages require additional overhead, in most cases
the huge increase in speed of modern computers has brought about little
performance decrease compared to earlier counterparts. The benefits of
these more abstracted languages is that they allow both an easier
learning curve for people less familiar with the older lower-level
programming languages, and they also allow a more experienced
programmer to develop simple applications quickly. Despite these
benefits, large complicated programs, and programs that are more
dependent on speed still require the faster and relatively lower-level
languages with today's hardware. (The same concerns were raised about
the original Fortran language.)
Throughout the second half of the twentieth century, programming was
an attractive career in most developed countries. Some forms of
programming have been increasingly subject to offshore outsourcing
(importing software and services from other countries, usually at a lower
wage), making programming career decisions in developed countries
more complicated, while increasing economic opportunities in less
developed areas. It is unclear how far this trend will continue and how
deeply it will impact on programmer wages and opportunities.

24
3.2.3 Modern Programming
i. Quality requirements
Whatever the approach to software development may be, the final
program must satisfy some fundamental properties. The following five
properties are among the most relevant and they found in C++:

 Efficiency/Performance: the amount of system resources a


program consumes (processor time, memory space, slow devices,
network bandwidth and to some extent even user interaction), the
less the better.
 Reliability: how often the results of a program are correct. This
depends on prevention of error propagation resulting from data
conversion and prevention of errors resulting from buffer overflows,
underflows and zero division.
 Robustness: how well a program anticipates situations of data
type conflict and other incompatibilities that result in run time
errors and program halts. The focus is mainly on user interaction
and the handling of exceptions.
 Usability: the clarity and intuitiveness of a programs output can
make or break its success. This involves a wide range of textual
and graphical elements that makes a program easy and
comfortable to use.
 Portability: the range of computer hardware and operating system
platforms on which the source code of a program can be
compiled/interpreted and run. This depends mainly on the range
of platform specific compilers for the language of the source code
rather than anything having to do with the program directly.
ii. Algorithmic Complexity
The academic field and the engineering practice of computer
programming are both largely concerned with discovering and
implementing the most efficient algorithms for a given class of problem.

25
For this purpose, algorithms are classified into orders, which expresses
resource use, such as execution time or memory consumption, in terms
of the size of an input. Expert programmers are familiar with a variety of
well-established algorithms and their respective complexities and use
this knowledge to choose algorithms that are best suited to the
circumstances.
iii. Methodologies
The first step in most formal software development projects is
requirements analysis, followed by testing to determine value modeling,
implementation, and failure elimination (debugging). There exist a lot of
differing approaches for each of those tasks. One approach popular for
requirements analysis is Use Case analysis.
iv. Measuring Language Usage
It is very difficult to determine what are the most popular of modern
programming languages. Some languages are very popular for particular
kinds of applications (e.g., COBOL is still strong in the corporate data
center, often on large mainframes, FORTRAN in engineering applications,
and C in embedded applications), while some languages are regularly
used to write many different kinds of applications. Methods of measuring
language popularity include: counting the number of job advertisements
that mention the language, the number of books teaching the language
that are sold (this overestimates the importance of newer languages), and
estimates of the number of existing lines of code written in the language
(this underestimates the number of users of business languages such as
COBOL).
v. Debugging
Debugging is a very important task in the software development process,
because an erroneous program can have significant consequences for its
users. Some languages are more prone to some kinds of faults because
their specification does not require compilers to perform as much
checking as other languages. Use of a static analysis tool can help detect

26
some possible problems. Debugging is often done with IDEs like Visual
Studio, NetBeans, and Eclipse. Standalone debuggers like gdb are also
used, and these often provide less of a visual environment, usually using
a command line.

3.2.4 Programming Languages


Different programming languages support different styles of
programming (called programming paradigms). The choice of language
used is subject to many considerations, such as company policy,
suitability to task, availability of third-party packages, or individual
preference. Ideally, the programming language best suited for the task at
hand will be selected, nevertheless my choice for this programe is C++.
3.3 CONCEPT OF C++
C++ (C Plus Plus) is a general-purpose programming language. It is
regarded as a middle-level language, as it comprises a combination of
both high-level and low-level language features. It is a statically typed,
free-form, multi-paradigm, compiled language where compilation creates
machine code for a target machine hardware, supports procedural
programming,data abstraction, object-oriented programming, and
generic programming.
The language was developed by Bjarne Stroustrup in 1979 at Bell Labs
as an enhancement to the C programming language and originally
named "C with Classes". It was renamed to C++ in 1983. Enhancements
started with the addition of classes, followed by, among other
features,virtual functions, operator overloading, multiple inheritance,
templates, and exception handling. ( Osborne,2002)
C++ enjoys wide use in the software industry. Some of its application
domains include systems software, device drivers, embedded software,
high-performance server and client applications, and entertainment
software such as video games. Several groups provide both free and
27
commercial C++ compiler software, including the GNU Project, Microsoft,
Intel, Borland and others.

3.3.1 Brief History

Bjarne Stroustrup, creator of C++.

Stroustrup began work on C with Classes in 1979. The idea of creating a


new language originated from Stroustrup's experience in programming
for his Ph.D. thesis. Stroustrup found that Simula had features that were
very helpful for large software development, but the language was too
slow for practical use. When Stroustrup started working in AT & T Bell
Labs he had the problem of analyzing the UNIX kernel with respect to
distributed computing. Remembering his Ph.D. experience, Stroustrup
set out to enhance the C language with Simula-like features. C was
chosen because it was general-purpose, fast, portable and widely used.
(Bjarne Stroustrup,October 2006)
In 1983, the name of the language was changed from C with Classes to
C++ (++ being the increment operator in C and C++). New features were
added including virtual functions, function name and operator
overloading, references, constants, user-controlled free-store memory
control, improved type checking, and BCPL style single-line comments
with two forward slashes (//). In 1985, the first edition of The C++
Programming Language was released, providing an important reference to
the language, since there was not yet an official standard.
Etymology
According to Stroustrup "the name signifies the evolutionary nature of
the changes from C". During C++'s development period, the language had
been referred to as "new C", then "C with Classes". The final name is

28
credited to Rick Mascitti (mid-1983) and was first used in December
1983. It stems from C's "++" operator (which increments the value of a
variable) and a common naming convention of using "+" to indicate an
enhanced computer program. There is no language called "C plus".
ABCL/c+ was the name of an earlier, unrelated programming language.
In The Design and Evolution of C++ (1994), Bjarne Stroustrup describes
some rules that he uses for the design of C++ and these are what makes
it unique from other programming languages :

 C++ is designed to be a statistically typed general-purpose


language that is as efficient and portable as C
 C++ is designed to directly and comprehensively support multiple
programming styles (procedural programming, data abstraction,
object-oriented programming, and generic programming)
 C++ is designed to give the programmer choice, even if this makes
it possible for the programmer to choose incorrectly
 C++ is designed to be as compatible with C as possible, therefore
providing a smooth transition from C
 C++ avoids features that are platform specific or not general
purpose
 C++ is designed to function without a sophisticated programming
environment

Inside the C++ Object Model (Lippman, 1996) describes how compilers
may convert C++ program statements into an in-memory layout.
Compiler authors are, however, free to implement the standard in their
own manner.
3.3.2 Standard Library
The C++ standard library incorporates the C standard library with some
small modifications to make it work better with the C++ language.
Another large part of the C++ library is based on the STL. This provides
such useful tools as containers (for example vectors and lists), iterators

29
to provide these containers with array-like access and algorithms to
perform operations such as searching and sorting. Furthermore
(multi)maps (associative arrays) and (multi)sets are provided, all of which
export compatible interfaces. Therefore it is possible, using templates, to
write generic algorithms that work with any container or on any
sequence defined by iterators. As in C, the features of the library are
accessed by using the #include directive to include a standard header. C++
provides 69 standard headers, of which 19 are deprecated. Most C++
compilers provide an implementation of the C++ standard library,
including the STL. Compiler-independent implementations of the STL,
3.3.3 Language features
C++ inherits most of C's syntax and the C preprocessor. The following is
a Hello world program which uses the C++ standard library stream
facility to write a message to standard output (Stroustrup Bjarne, 2000)
#include <iostream> // provides std::cout

int main()
{
std::cout << "Hello, world!\n";
}

i. Operators and operator overloading


C++ provides more than 30 operators, covering basic arithmetic, bit
manipulation, indirection, comparisons, logical operations and more.
Almost all operators can be overloaded for user-defined types, with a few
notable exceptions such as member access (. and .*). The rich set of
overloadable operators is central to using C++ as a domain Specific
Language The overloadable operators are also an essential part of many
advanced C++ programming techniques, such as smart pointers.
Overloading an operator does not change the precedence of calculations
involving the operator, nor does it change the number of operands that
the operator uses (any operand may however be ignored).

30
ii. Objects
C++ introduces object-oriented (OO) features to C. It offers classes,
which provide the four features commonly present in OO (and some non-
OO) languages: abstraction, encapsulation, inheritance and
polymorphism. Objects are instances of classes created at runtime. The
class can be thought of as a template from which many different
individual objects may be generated as a program runs.
iii. Encapsulation
Encapsulation is the hiding of information. C++ implements
encapsulation by allowing all members of a class to be declared as either
public, private, or protected. A public member of the class is accessible
to any function. A private member is accessible only to functions that are
members of that class and to functions and classes explicitly granted
access permission by the class ("friends"). A protected member is
accessible to members of classes that inherit from the class in addition to
the class itself and any friends.
The OO principle is that all of the functions (and only the functions) that
access the internal representation of a type should be encapsulated
within the type definition. C++ supports this (via member functions and
friend functions), but does not enforce it: the programmer can declare
parts or all of the representation of a type to be public, and is allowed to
make public entities that are not part of the representation of the type.
Because of this, C++ supports not just OOP, but other weaker
decomposition paradigms, like modular programming.
iv. Inheritance
Inheritance allows one data type to acquire properties of other data
types. Inheritance from a base class may be declared as public,
protected, or private. This access specifier determines whether unrelated
and derived classes can access the inherited public and protected
members of the base class. Only public inheritance corresponds to what
is usually meant by "inheritance". The other two forms are much less

31
frequently used. If the access specifier is omitted, a "class" inherits
privately, while a "struct" inherits publicly. Base classes may be declared
as virtual; this is called virtual inheritance. Virtual inheritance ensures
that only one instance of a base class exists in the inheritance graph,
avoiding some of the ambiguity problems of multiple inheritance.
Multiple inheritance is a C++ feature sometimes considered
controversial. Multiple inheritance allows a class to be derived from more
than one base class; this can result in a complicated graph of inheritance
relationships.
3.3.4 Compatibility
Producing a reasonably standards-compliant C++ compiler has proven to
be a difficult task for compiler vendors in general. For many years,
different C++ compilers implemented the C++ language to different levels
of compliance to the standard, and their implementations varied widely
in some areas such as partial template specialization. Recent releases of
most popular C++ compilers support almost all of the C++ 1998
standard. (Herb Sutter,2003)
With C
C++ is often considered to be a superset of C, but this is not strictly true.
Most C code can easily be made to compile correctly in C++, but there
are a few differences that cause some valid C code to be invalid in C++,
or to behave differently in C++.
3.3.5 Criticism
Modern critics of the language raise several points. First, since C++ is
based on and largely compatible with C, it inherits most of the criticisms
leveled at that language. Taken as a whole, C++ has a large feature set,
including all of C, plus a large set of its own additions, in part leading to
criticisms of being a "bloated" and complicated language. Bjarne
Stroustrup points out that resultant executables don't support these
claims of bloat: "I have even seen the C++ version of the 'hello world'
program smaller than the C version." (Bjarne Stroustrup, 2008) The

32
Embedded C++ standard was specified to deal with part of this, but it
received criticism for leaving out useful parts of the language that incur
no runtime penalty. Because of its large feature set, it is difficult to fully
master C++.

33
CHAPTER FOUR
IMPLIMENTATION

Having analyzed the existing system in the previous chapter, it is


now convenient for us to consider a particular problem and solve
manually which will serve as a general case for the two methods
selected-Crammer’s rule and Gaussian elimination method. The software
alternative will be designed not only for the two methods but also for any
number of systems of linear equations that involve the two methods
selected. As was stated in chapter one, I am going to manually solve a
4x4 system of linear equation with 4 unknown.
Problem: Solve the systems of linear equations
2x + y + z – 2w = 1
-x +2w = 0
x +5y – z - w = 1
x + 2y – 2z + w = 1……………………………………(4.1)
Solution:
I shall first consider the Crammer’s rule for the above equations and the
Gaussian Elimination method.
4.1 MANUAL SOLUTION USING CRAMMER’S RULE
For a 4x4 or higher matrices, we reduce the matrix to a 3x3 or lower
sub matrices and then reduce the 3x3 sub matrices or lower sub
matrices to 2x2 sub matrices and obtain the determinant. However
Sarous rule is not applicable in this case. It only holds for a 3x3 matrix.
Writing the matrix in compact form Ax=B we have it as followed
2 1 1 -2 x 1
-1 0 0 2 y = 0
1 5 -1 -1 z 1
1 2 2 1 w 1 …………………………………..(4.2)

The determinant of the coefficient matrix det(A)


2 1 1 -2
-1 0 0 2
det(A) = 1 5 -1 -1
1 2 2 1 ………………………………………….(4.3)
34
We pick the row or column with the most zeros in it, in this case it is the
second row.
For each element in the original matrix, it’s minor will be a 3x3
determinant. We’ll have to expand each of these by using three 2x2
determinants
1 1 -2 2 1 1
1 5 -1 -1 + 0 ? -0 ? + 2 1 5 -1
2 2 1 1 2 2
We could actually fill in those middle two minors, but since they're
multiplied by 0, it doesn't really matter what they are. In fact, you could
just as easily skip them. Now, there are two 3x3 determinants left to find.
= [ (-1+2) – (5+2) –2(10+2) ] + 2 [ 2(10+2) – (2+1) + (2-5)]
det(A) = [ 1-7- 24] + 2[24-3-3] = -30 + 36 = 6
In the same way the different determinants det(Ax), det(Ay), det(Az) and
det(Aw) is calculated by substituting the column vector B into the first,
second, third and forth column of A respectively, which is as follows;

1 1 1 -2
0 0 0 2
det(Ax) = 1 5 -1 -1 = 12
1 2 2 1

2 1 1 -2
det(Ay) = -1 0 0 2
1 1 -1 -1 = -1
1 1 2 1
2 1 1 -2
-1 0 0 2
det(Az) = 1 5 1 -1
1 2 1 1 = -5

2 1 1 1
-1 0 0 0
det(Aw) = 1 5 -1 1 =6
1 2 2 1

35
Finally ,
x = det(Ax)/ det(A) = 12/6 = 2 ……(4.4)
y = det(Ay)/ det(A) = -1/6 = -0.1666667 ……..(4.5)
z = det(Az)/ det(A) = -5/6 = -0.833333 ……… (4.6)
w = det(Aw)/ det(A)= 6/6 = 1 …..…….(4.7)
4.2 MANUAL SOLUTION USING GAUSSIAN ELIMINATION METHOD
Now despite the rigors of the Crammer’s rule in computing the different
determinants, I shall again apply the Gaussian elimination method that
requires absolute precision in solving the same system in the problem
above in equation (4.1). The augmented matrix of the coefficient matrix is
; 2 1 1 -2 1 R1
-1 0 0 2 0 R2
1 5 -1 -1 1 R3
1 2 2 1 1 R4
I shall perform some elementary row operations in steps as follows;
Step 1: Since R1 has the largest absolute value among all the coefficient
of x1, there will be no need for row interchange. I will proceed by R1 to
eliminate the remaining row as follows;
Step 2: Multiply the first row by 1/2 and add it to the second row to get a
new R2 as shown below;
1 1 /2 1/2 1 1 /2

-1 0 0 2 0
0 1/2 1/2 1 1/2 R2
Step 3: Multiply the first row by -1/2 and add it to the third row to get a
new R3 as shown below;
-1 -1/2 - 1/2 1 -1/2
1 5 -1 -1 1
0 9/2 -3/2 0 1/2 R3
Step 4: Multiply the first row by -1/2 and add it to the fourth row to get a
new R4 as shown below;

36
-1 -1/2 - 1/ 2 1 -1/2
1 2 2 1 1

0 3/2 3/ 2 2 1 /2 R4
Now we have the matrix of the form;
2 1 1 -2 1 R1
0 1/2 1/2 1 1/2 R2
0 9/2 -3/2 0 1/2 R3
0 3/2 3/2 2 1/2 R4
Step 5: From R2 to R4, R3 has the element with the largest absolute
value, so we interchange R3 with R2 and use R2 to eliminate R3 and R4
respectively;
2 1 1 -2 1 R1
0 9 /2 -3/2 0 1/ 2 R2
0 1/2 1/2 1 1/2 R3
0 3 /2 3/2 2 1/2 R4

Step 6: Multiply the second row by -1/9 and add it to the third row to get
a new R3 as shown below;
0 -1/2 1/6 0 -1/18
0 1/2 1/2 1 1 /2

0 0 2/3 1 4 /9 R3
Step 7: Multiplying the second row by - /3 and add it to the fourth row
1

to get a new R4 as

shown below; 0 -3/2 1/2 0 -1/6


0 3/2 3/2 2 1/2

0 0 2 2 1/3 R4

Also now we have a matrix of the form;

2 1 1 -2 1 R1
0 9/2 - /2
3 0 1/2 R2
0 0 2/3 1 4/9 R3
0 0 2 2 1/3 R4

Step 8: Once again we interchange R3 and R4, since R4 has the element
with the largest absolute value and we use R3 to eliminate R4

37
Step 9: Multiply the third row with -1/3 and add it to fourth row to get a
new R4 as shown below;
0 0 -3/2 -3/2 -1/9
0 0 2/3 1 4/9

0 0 0 1/3 1/3

Thus giving us an augmented matrix of the form;


2 1 1 -2 1
0 9/2 - /2 0
3 1/2

0 0 2 2 1 /3

0 0 0 1 /3 1/3

Making the above system of linear equations, we have;


2x + y + z – 2w = 1 . . . . . . . . . . . . (i)
9/2y - 3 /2 z = 1/2 . . . . . . . . . . .(ii)
2z + 2w = 1/3 . . . . . . . . . . . .(iii)
1/3 w= 1/3 . . . . . . . . . . . .(iv)

Now by using back-substitution from eqn.(iv) we have w = 1 ………(4.8)


Substituting w into eqn.(iii): 2z + 2(1) = 1/3 gives us
z =-5/6 = -0.833333 ………….(4.9)
Substituting z into eqn. (ii) : 9/2y - 3/2(-5/6) = 1/2 this gives us
y = -1/6 = -0.166667 …………....(4.10)
Now substituting the values of w, z and y into eqn. (i) : 2x +(-1/6) + (-5/6)
– 2(1) = 1 Which gives us our x = 2 …………………(4.11)

4.3 SYSTEM REQUIREMENT CONFIGURATIONS AND MAINTENANCE


In ordr to achieve the proposed software design, there will be the
need for at least a personal desktop computer, which should be
compatible with INTEL pentium (III) processor, having at least 256MB
RAM and also needed is the software requiremen for this project which is
either an INTEL or BORLAND or TURBOR or a BLOODSHED C++
compiler to run the programe.

38
Maintenance can be made much easier and less costly by having
good documentation, also the hardware and software should be given
regular preventive maintenance to avoid frequent overhauling of the
system.

39
CHAPTER FIVE
SUMMARY, CONCLUSION AND RECOMMENDATION
5.1 SUMMARY
This project work has being created for the production of a software
supplement to manual calculation of systems of linear equations. The
analysis of an existing system was properly done to enable the user or
just a reader to understand the basis from which the algorithms were
designed. Various steps in designing a program are also highlighted to
enhance easy understanding of the procedure in designing a program
and the operation taking place at each stage in the.
Despite the fact that the coding for the Crammer’s rule looks more
complex it is more accurate than the Gaussian elimination method due
to the fact that the software for the later has a lot of procedures to be
executed
5.2 CONCLUSION
Considering the time consuming, complex effort and possibilities of
errors prone nature of the manual manipulations, the software
alternative has restored back the confidence of handling complex linear
systems with the hope of getting a reliable solution with great speed.
More so, the constant use of this software application will anticipate
steady progress in developing one’s capacity for abstract thought and
careful reasoning.
5.3 RECOMMENDATION
With the accuracy, reliability and time saving nature of the software
alternative, the package should be installed on all the computers of the
department that is available to both students and lecturers alike to
ensure constant use of the software. Also students should be given
assignment that involve the use of the software package regularly to
develop their capacity for abstract thought and careful reasoning.

40
REFERENCE
Alexandrescu, Andrei; Herb Sutter (2004). C++ Design and Coding Standards:
Rules and Guidelines for Writing Programs. Addison-Wesley. ISBN 0-321-11358-
6
Bjarne Stroustrup's FAQ - Is C a subset of C++?". Retrieved on 2008-01-
18.
Bjarne Stroustrup's FAQ - Where did the name "C++" come from?".
Retrieved on 2008-01-16.
Bjarne Stroustrup's FAQ - When was C++ invented?. Retrieved on 2006-
05-30.
C++ The Complete Reference Third Edition, Herbert Schildt, Publisher:
Osborne McGraw-Hill.
Herb Sutter (2003-04-15). "C++ Conformance Roundup". Dr. Dobb's
Journal. Retrieved on 2006-05-30.
Hansford, Dianne (December 15, 2004), Practical Linear Algebra: A
Geometry Toolbox, AK Peters, ISBN 978-1568812342
Kenneth E. Iverson, (August 1980) "Notation as a tool of thought",
Communications of the ACM, 23: 444-465.
Lay, David C. (August 22, 2005), Linear Algebra and Its Applications (3rd
ed.), Addison Wesley, ISBN 978-0321287137
Meyer, Carl D. (February 15, 2001), Matrix Analysis and Applied Linear
Algebra, Society for Industrial and Applied Mathematics (SIAM), ISBN
978-0898714548.
Nabih N. Abdelmalek, Ph.D. & William A. Malek, M.Eng. (2008),
“LinearApproximation in C” CRC Press, Taylor & Francis Group, ISBN-
13: 978-1-58488-978-6
Poole, David (January 24, 2005), Linear Algebra: A Modern Introduction
(2nd ed.), Brooks Cole, ISBN 978-0534998455

41
Stroustrup, Bjarne (2000). The C++ Programming Language, Special
Edition, Addison-Wesley, 46. ISBN 0-201-70073-5.
Stroustrup, Bjarne (2000). The C++ Programming Language, Special
Edition, Addison-Wesley, 310. ISBN 0-201-70073-5. "A virtual member
function is sometimes called a method."
Web 1 (Al-Jazari - the Mechanical Genius, MuslimHeritage.com)

42
APPENDIX
ALGORITHM FOR CRAMMER’S RULE
1. Start
2. Specify the order of the coefficienct matrix n
3. Input the coefficient matrix A
4. Input the column matrix B
5. Compute the determinant of the coefficient matrix: det(A)
6. Delete the first column of matrix A and replace it with the column
vector B and store it as Ax.
7. Compute the determinant of (Ax).
8. Delete the second column of matrix A and replace it with the
column vector B and store it as Ay.
9. Compute the determinant of (Ay).
10.Delete the third column of matrix A and replace it with the
column vector B and store it as Az.
11.Compute the determinant of (Az).
12 Delete the fourth column of matrix A and replace it with the
column vector B and store it as Aw.
13Compute the determinant of (Aw).
14Divide each of the determinant det(Ax), det(Ay), det(Az) and det(Aw)
by the determinant of A : det(Ax)
15Display the results of x1, x2, x3 and x4
16End
PROGRAM LISTING FOR CRAMMER’S RULE
/*
Program: cramers rule.cpp
Author: KALIO ENOCH PRECIOUS
Date: 2008 OCTOBER
Purpose: The purpose of this program is to solve a Systems Linear
Equation of size
less than or equal to MAX_SIZE using CRAMMER'S RULE.

43
*/
#include <iostream.h>
#include <string.h>
const int MAX_SIZE = 20;
// Level 1 Functions
void print_message();
void initialize(float &, float *, float *, float *);
void allocate_floats(int &, float *&, float *&, float *&);
void initialize_dynamic_memory(float *, int);
void get_matrices(float [][MAX_SIZE], float *, int);
void calculate_variable_matrix(float &, float *, float *, float
[][MAX_SIZE], float *, int);
void print_results(float *, float *, int, float);
void return_memory(float *, float *, float *);
// Level 2 Functions
void calculate_determinant(float &, float [][MAX_SIZE], int);
void calculate_modified_determinant(float *, float [][MAX_SIZE], float
[], int);
void calculate_variables(float *, float *, float, int);
void print_determinants(float *, int, float);
void print_variables(float *, int, float);
// Level 3 Functions
void copy_matrix(float[][MAX_SIZE], float [][MAX_SIZE], int);
bool swap_rows(float [][MAX_SIZE], int, int);
int main()
{
int size;
float coefficient[MAX_SIZE][MAX_SIZE], *variable, *constant,
*determinant_modified, determinant_original;
char quit[1];
char yes[] = "y";

44
print_message();
do
{
initialize(determinant_original, variable, constant,
determinant_modified);
allocate_floats(size, variable, constant, determinant_modified);
initialize_dynamic_memory(determinant_modified, size);
get_matrices(coefficient, constant, size);
calculate_variable_matrix(determinant_original,
determinant_modified, variable, coefficient, constant, size);
print_results(variable, determinant_modified, size,
determinant_original);
return_memory(variable, constant, determinant_modified);
cout << "Do you want to solve another system (y/n)? ";
cin >> quit;
} while (strcmp(quit, yes) == 0);
return 0;
}
void print_message()
{
cout << " This program was written completely by KALIO ENOCH
PRECIOUS" << endl;
cout << " for Students of MATHEMATICS/COMPUTER SCIENCE."
<< endl << endl;
cout << "This program will solve a square linear system using
Cramer's Rule." << endl;
cout << "Ax = b, where A is a square (n x n) coefficient matrix and ,"
<< endl;
cout << "x is a variable (n x 1) matrix, and b is a constant (n x 1)
matrix." << endl;

45
return;
}
//
void initialize(float &i_determinant_original, float *i_variable, float
*i_constant, float *i_determinant_modified)
{
i_determinant_original = 1;
i_variable = NULL;
i_constant = NULL;
i_determinant_modified = NULL;
return;
}
void allocate_floats(int &af_size, float *&af_variable, float *&af_constant,
float *&af_determinant_modified)
{
cout << "First, enter n (the size of the square matrix): ";
cin >> af_size;
while ((af_size < 1) || (af_size > 20))//range of number of systems
{
cout << "That is not a valid size. Numbers must be greater than
0 and less than 20." << endl;
cout << "Enter n: ";
cin >> af_size;
}
af_variable = new float[af_size];
af_constant = new float[af_size];
af_determinant_modified = new float[af_size];
return;
}
//

46
void initialize_dynamic_memory(float *idm_determinant_modified, int
idm_size)
{
int i;
for (i = 0; i < idm_size; i ++)
idm_determinant_modified[i] = 1;
return;
}
//read values into matrix
void get_matrices(float gm_coefficient[][MAX_SIZE], float
*gm_constant, int gm_size)
{
int i, j;
cout << endl;
cout << "Enter the coefficient matrix (A)." << endl;
cout << "When entering the rows, put one space between each term
in the row." << endl;
for (i = 0; i < gm_size; i ++)
{
cout << "Enter row " << i + 1 << ": ";
for (j = 0; j < gm_size; j ++)
cin >> gm_coefficient[i][j];
}
cout << endl;
cout << "Enter the constant matrix (b)." << endl;
for (i = 0; i < gm_size; i ++)
{
cout << "Enter row " << i + 1 << ": ";
cin >> gm_constant[i];
}
return;

47
}
void calculate_variable_matrix(float &cvm_determinant_original, float
*cvm_determinant_modified, float *cvm_variable, float
cvm_coefficient[][MAX_SIZE], float *cvm_constant, int cvm_size)
{
if (cvm_size == 1)
{
cvm_determinant_original = cvm_coefficient[0][0];
cvm_determinant_modified[0] = cvm_constant[0];
}
else
{
calculate_determinant(cvm_determinant_original,
cvm_coefficient, cvm_size);
calculate_modified_determinant(cvm_determinant_modified,
cvm_coefficient, cvm_constant, cvm_size);
}
calculate_variables(cvm_variable, cvm_determinant_modified,
cvm_determinant_original, cvm_size);

return;
}
void print_results(float *pr_variable, float *pr_determinant_modified, int
pr_size, float pr_determinant_original)
{
print_determinants(pr_determinant_modified, pr_size,
pr_determinant_original);
print_variables(pr_variable, pr_size, pr_determinant_original);
return;
}

48
void return_memory(float *rm_variable, float *rm_constant, float
*rm_determinant_modified)
{
delete [] rm_variable;
delete [] rm_constant;
delete [] rm_determinant_modified;

return;
}
void calculate_determinant(float &cd_determinant, float
cd_coefficient[][MAX_SIZE], int cd_size)
{
int i, j, k, l;
float temp_co, temp_mat[MAX_SIZE][MAX_SIZE];
copy_matrix(cd_coefficient, temp_mat, cd_size);
for (i = 0; i < cd_size; i ++)
{
if (temp_mat[i][i] == 0)
{
if (swap_rows(temp_mat, i, cd_size) == false)
{
cd_determinant = 0;
return;
}
else
cd_determinant *= -1;
}
if (temp_mat[i][i] != 1)
{
temp_co = temp_mat[i][i];
cd_determinant *= temp_mat[i][i];

49
for (j = i; j < cd_size; j ++)
temp_mat[i][j] = temp_mat[i][j] / temp_co;
}
for (k = i + 1; k < cd_size; k ++)
for (l = (cd_size - 1); l >= i; l --)
temp_mat[k][l] = temp_mat[k][l] - (temp_mat[k][i] *
temp_mat[i][l]);
}

return;
}
void calculate_modified_determinant(float cmd_determinant_modified[],
float cmd_coefficient[][MAX_SIZE], float *cmd_constant, int cmd_size)
{
int i, j;
float temp_mat[MAX_SIZE][MAX_SIZE];
for (i = 0; i < cmd_size; i ++)
{
copy_matrix(cmd_coefficient, temp_mat, cmd_size);
for (j = 0; j < cmd_size; j ++)
temp_mat[j][i] = cmd_constant[j];
calculate_determinant(cmd_determinant_modified[i], temp_mat,
cmd_size);
}
return;
}
void calculate_variables(float *cv_variable, float
*cv_determinant_modified, float cv_determinant_original, int cv_size)
{
int i;
for (i = 0; i < cv_size; i ++)

50
cv_variable[i] = cv_determinant_modified[i] /
cv_determinant_original;
return;
}
void print_determinants(float *pd_determinant_modified, int pd_size, float
pd_determinant_original)
{
int i;
cout << endl << "Here is a list of all the determinants:" << endl;
cout << "Det(A) = " << pd_determinant_original << endl;
for (i = 0; i < pd_size; i ++)
cout << "Det(A" << i + 1 << ") = " << pd_determinant_modified[i]
<< endl;
return;
}
void print_variables(float *pv_variable, int pv_size, float
pv_determinant_original)
{
int i;
cout << endl;
if (pv_determinant_original == 0)
cout << "Cramer's Rule is not applicable for this matrix because
Det(A) = 0." << endl;
else
{
cout << "Here is a list of the variables:" << endl;
for (i = 0; i < pv_size; i ++)
cout << "x" << i + 1 << " = " << pv_variable[i] << endl;
}
return;
}

51
void copy_matrix(float matrix1[][MAX_SIZE], float matrix2[][MAX_SIZE],
int cm_size)
{
int i, j;
for (i = 0; i < cm_size; i ++)
for (j = 0; j < cm_size; j ++)
matrix2[i][j] = matrix1[i][j];
return;
}
bool swap_rows(float matrix[][MAX_SIZE], int sr_i, int sr_size)
{
int initRow = sr_i, initCol = sr_i, i, j;
float temp;
for (i = initRow; i < sr_size; i ++)
if (matrix[i][initCol] != 0)
{
for (j = initCol; j < sr_size; j ++)
{
temp = matrix[initRow][j];
matrix[initRow][j] = matrix[i][j];
matrix[i][j] = temp;
}
return true;
}
return false;
}
OUTPUT LISTING OF THE CRAMMER’S RULE PROGRAM
This program was written completely by KALIO ENOCH PRECIOUS
for Students of MATHEMATICS/COMPUTER SCIENCE.

This program will solve a square linear system using Cramer's Rule.

52
Ax = b, where A is a square (n x n) coefficient matrix and ,
x is a variable (n x 1) matrix, and b is a constant (n x 1) matrix.
First, enter n (the size of the square matrix): 4

Enter the coefficient matrix (A).


When entering the rows, put one space between each term in the row.
Enter row 1: 2 1 1 -2
Enter row 2: -1 0 0 2
Enter row 3: 1 5 -1 -1
Enter row 4: 1 2 2 1
Enter the constant matrix (b).
Enter row 1: 1
Enter row 2: 0
Enter row 3: 1
Enter row 4: 1

Here is a list of all the determinants:


Det(A) = 6
Det(A1) = 12
Det(A2) = -1
Det(A3) = -5
Det(A4) = 6

Here is a list of the variables:


x1 = 2
x2 = -0.166667
x3 = -0.833333
x4 = 1
Do you want to solve another system (y/n)?

53
ALGORITHM FOR GAUSSIAN ELIMINATION METHOD
1. Start
2. Declare the array in rows and columns
3. Find the row Rk such that ai,1 has the largest absolute value among
all the ai,1 and interchange the row with a1,j
4. Use the first row a1,j and in particular a1,1 of ai,1 to eliminate the
remaining rows. For i > 1, multiply a1,j by –ai,1/a11 and add the row
to ai,j.
5. Interchange rows excluding a1,j so that the pivoting element a2,2
has the largest absolute value among the ai,2 in the subsystem.
6. For i > 2 multiply a2,j by –ai,2/a2,2 and add the row to ai,j.
7. Interchange rows excluding a2,j so that the pivoting element a3,3
has the largest absolute value among the ai,3 in the sub-sub
system.
8. For i > 3 multiply a3,j by –ai,3/a3,3 and add the row to ai,j.
9. Solve the triangular system by back substitution.
10. End.
PROGRAM LISTING FOR THE GAUSSIAN ELIMINATION METHOD
// C++ PROGRAM of Gaussian Elimination Method
// Programmed by KALIO ENOCH PRECIOUS
// Date : OCTOBER 2008

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <new>
using namespace std;

double** matrixAlloc(int, int);


double* vectorAlloc(int);

54
void matrixFree(double**, int);
void vectorFree(double*);
void matrixPrint(double**, int);
void vectorPrint(double*, int);

int main()
{
double** a;
double* b;
double x, sum;
int n;
cout << " THIS PROGRAM WAS CODED BY KALIO ENOCH
PRECIOUS " << endl;
cout << " OF MATHS/STATISTICS DEPARTMENT" << endl;
cout << " MATHS/COMPUTER SCIENCE OPTION" << endl <<
endl;
cout << " ************ Gaussian Elimination Method *******" <<
endl << endl;
cout << " Input number of equations (that the order n)= ";
cin >> n;

a = matrixAlloc(n, n);
b = vectorAlloc(n);

cout << "Input matrix coefficients a(i,j)=" << endl;


cout << "When entering the rows, put one space between each term
in the row." << endl;
for ( int i = 0; i < n; i++)
{
cout << "Enter row " << i + 1 << ": ";
for ( int j = 0; j < n; j++)

55
cin >> a[i][j];
}
cout << "Input right-hand side vector b(i)" << endl;
for (int i = 0; i < n; i++)
{
cout << "Enter row " << i + 1 << ": ";
cin >> b[i];
}
cout << endl << endl;
cout << "Coefficient matrix A:" << endl;
matrixPrint(a, n);
cout << "Right-hand-side vector b:" << endl;
vectorPrint(b, n);

// convert to upper triangular form


for (int k=0;k<n-1;k++)
{
if ( fabs(a[k][k])>=1.e-6)
{
for (int i=k+1;i<n;i++)
{
x = a[i][k]/a[k][k];
for (int j=k+1;j<n;j++) a[i][j] = a[i][j] -a[k][j]*x;
b[i] = b[i] - b[k]*x;
}
}
else
{
cout << "zero pivot found in line:" << k << endl;
exit(1);
}

56
}
// back substituti n
b[n-1]=b[n-1]/a[n-1][n-1];
for ( int i = n-2; i >= 0; i--)
{
sum = b[i];
for (int j = i+1; j < n; j++)
sum = sum - a[i][j]*b[j];
b[i] = sum/a[i][i];
}
cout << "Solution:" << endl;
vectorPrint(b, n);
matrixFree(a, n);
vectorFree(b);
char me;
cin>>me;
return 0;
}
double** matrixAlloc(int n, int m)
{
double** p;
try {
p = new double* [n];
for ( int i = 0; i < n; i++ )
p[i] = new double[m];
}
catch (bad_alloc e) {
cout << "Exception occurred: "
<< e.what() << endl;
}

57
return p;
}

double* vectorAlloc(int n)
{
double* p;

try {
p = new double[n];
}
catch (bad_alloc e) {
cout << "Exception occurred: "
<< e.what() << endl;
}
return p;
}
void matrixFree(double** p, int n)
{
for ( int i = 0; i < n; i++)
delete[] p[i];
delete[] p;
p = 0;
}
void vectorFree(double* p)
{
delete[] p;
p = 0;
}
void matrixPrint(double** p, int n)
{
for ( int i = 0; i < n; i++)

58
{
for ( int j = 0; j < n; j++)
cout << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setprecision(4)
<< setw(12) << p[i][j];
cout << endl;
}
}
void vectorPrint(double* p, int n)
{
for ( int i = 0; i < n; i++)
cout << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setprecision(4)
<< setw(12) << p[i];
cout << endl << endl;
}
OUTPUT LISTING FOR THE PROGRAM
THIS PROGRAM WAS CODED BY KALIO ENOCH PRECIOUS
OF MATHS/STATISTICS DEPARTMENT
MATHS/COMPUTER SCIENCE OPTION

************ Gaussian Elimination Method *******

Input number of equations (that is the order n)= 4


Input matrix coefficients a(i,j)=
When entering the rows, put one space between each term in the row.
Enter row 1: 2 1 1 -2
Enter row 2: -1 0 0 2
Enter row 3: 1 5 -1 -1
Enter row 4: 1 2 2 1
Input right-hand side vector b(i)

59
Enter row 1: 1
Enter row 2: 0
Enter row 3: 1
Enter row 4: 1

Coefficient matrix A:
2.0000 1.0000 1.0000 -2.0000
-1.0000 0.0000 0.0000 2.0000
1.0000 5.0000 -1.0000 -1.0000
1.0000 2.0000 2.0000 1.0000
Right-hand-side vector b:
1.0000 0.0000 1.0000 1.0000
Solution:
2.0000 -0.1667 -0.8333 1.0000

60

Potrebbero piacerti anche