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 (nonarithmetic) 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. error free so long as the application (i.e. the program) runs. It is also

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. 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 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) ……………………….(1.2) If A is define then the equation can be sowed for the xi using the inverse of A according to the

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

when the values s1.2 2x1 − x2 + 1. the equations simplify to 8 = 8 and − 7 = − 7.xn because.. David C..2) A solution of the system is a list respectively... x2.sn are substituted for x1.2) for x1.1) Where all aij are elements of a field An example of a system of linear equations is in equation 2... x3 respectively.... 2005) summarises SOLE considering the matrix operation in the following areas: 2.... Two linear systems are called equivalent if they have the same 4 .5x3 = 8 x1 − 4x3 = − 7 …………………………………………(2. + amnxn =bm ………………………….. Taking for example Lay David in his book “ Linear Algebra and it’s Application ” (Lay..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 + .s2.(2. For example. of numbers that makes is a solution to the equation (2. when these values are substituted in equation (2.2) each equation true.. The set of all possible solutions is called the solution set of the linear system.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. + a2nxn = b2 : : : : : : : : : : : : : : : : am1x1 + am2x2 + . + a1nxn = b1 a21x1 + a22x2 + .

. .m and j = 1. .. In this case your numerical procedure will fail. each solution of the first system is a solution to the second system.j = 1. and vice versa. . the solution is the single point . That is. 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. a condition called row degeneracy. . 5 . a row degeneracy implies a column degeneracy.1 NONSIGULAR VERSUS SINGULAR SETS OF EQUATIONS If n = m then there are as many equations as unknowns.j = 1. and there is a good chance of solving for a unique solution set of xj ’s. .n are related by m equations.. A pair of numbers statisfies both equations in the system if and only if the point lies on both l1 and l2. as are the right-hand side quantities bi. and vice versa.) A set of equations that is degenerate is called singular. which is denote by l1 and l2. A typical problem is x1 − 3x2 = − 1 − x1 + 4x2 = 3………………………………………………………(2. 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. called column degeneracy. as you easily can see on Here the n unknowns xi. 2. 2. or if all equations contain certain variables only in exactly the same linear combination.. there can fail to be a unique solution if one or more of the m equations is a linear combination of the others. . 2. . 2.. 2. and it can tell you that it has failed.n are known numbers. Numerically. .m. In the system above. (For square matrices. .1. at least two additional things can go wrong: 1.3) The graphs of these lines. .solution set. The coefficients aij with i = 1. While not exact linear combinations of each other. Analytically. .

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

1x1 + . A homogeneous system always has a solution: set all variables to 0. you can get all the solutions in this way. The importance of homogeneous systems of linear equations is that they provide a cornerstone for solving general systems of linear equations. + am.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..nxn = b1 a2. And if you squint at it from just the right angle. it might not have any solutions: x+y=1 2x + 2y = 2 ………………………………………(2. more than just the trivial all-0 solution).. + a2. Then for any solution of the homogeneous system (xi=yi)i=1n.1.. you see that its set of solutions is a vector space: adding solutions or multiplyng a solution by a constant (a scalar) give solutions. + a1.6) am. What about the general system a1. It may.g.. and we know just one particular solution (xi=ci)i=1n for the general system.7) It turns out that a solution might not exist iff the homogeneous system has infinitely many solutions (i. Suppose you know how to solve the general homogeneous system above. and find all the solutions. too.1x1 + .. however. Nicer yet. and a systems is said to be 7 . 2.nxn = b2 : : : : ……………………(2. "x+y+4=z+4"). But suppose it does.1x1 + . This is the trivial solution. it's easy to see that (xi=ci+yi)i=1n is also a solution to that general system.nxn = bm First off. have more solutions.e..any constants that do appear may be cancelled out within that equation (e.

8 . What is the solution set? Systems of Linear Equations Inconsisten Consistent No Solution Unique Solution More than one solution Figure: 2. Figure 2.1 Figure 2.2: Exactly one solution Of course.inconsistent if it has no solution. or they could coincide and "intersect" at every point on the line.3 and Figure 2.4 shows graphs to visualize this. how many elements are in the solution set? 3. Is a system of linear equations consistent or not? 2. In Linear Algebra. If so. we are concerned with three problems: 1. they could be parallel. two lines does not have to intersect in a single point.

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. Suppose further that experimentation with a meter stick produces these two balances. one with a mass known to be 2 kg.4: Infinitely many solutions 2.2. Fig.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 .Figure 2. and are asked to find the unknown masses.4 MOTIVATIONS Systems of linear equations are common in science and mathematics.1.3: No solution Figure2.

. toluene C7H8 and nitric acid HNO3 to produce trinitrotoluene C7H5O6N3 along with the byproduct water (conditions have to be controlled very well.8) The second example of a linear system is from Chemistry. the equations involve only the first power of the variables. the two balances give this system of two equations.distance from the balance point)... (Hansford.. N... Applying that principle to the elements C... 2004) 2. Given the system 10 ...(2....... 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. 40h + 15c = 100 25c = 50 + 50h ………………………………………... Dianne...2 MATRIX NOTATION The essential information of a linear system can be described in a rectangular array called a matrix.... In each. H. We will solve them later. and O in turn gives this system ……. We can mix. This gives an introduction to systems of linear equations.9) To finish each of these examples requires solving a system of equations.…(2. under controlled conditions... indeed— trinitrotoluene is better known as TNT)..

3x1 + 5x2 + 2x3 = 0 x1 − 8x2 = 10 − 2x2 + x3 = − 8 ………………………………(2. ii. The coefficient matrix looks like this Which make sense if you look at the definition of a linear equation.1 i. Matrix notation will simplify the calculations of a linear system. we make a matrix called the coefficient matrix (or matrix of coefficients) of the system. 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. m and n are positive integers. The augmented matrix above has 3 rows and 4 columns. Again the size of a matrix tells us how many rows and columns it has.10) with the coefficients of each variable aligned in columns.2. therefore it is called a 3x4 (read "3 by 4") matrix. 2. an m x n matrix is a rectangular array of numbers with m rows and n columns. ELEMENTARY ROW OPERATIONS Replacement Interchanging Scaling 11 There are three elementary row operations: . iii.

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.11) Which can be written in matrix notation as an augmented matrix.The following are an explanation and examples of the three different operations. An example is. not just on matrices derived from linear systems. An important thing to remember is that all operations can be used on all matrices. we are given the matrix Here we have performed an interchange operation on the two rows 12 . A more common paraphrase of row replacement is "Add to one row a multiple of another row." An example is. i. Interchanging Interchange two rows. Replacement Replace one row by the sum of itself and a multiple of another row. we are given the linear system: x1 + 4x2 = 3 2x1 + 2x2 = 4 …………………………………(2.

This is done by using x1 term from the first equation to eliminate the x1 terms in the other equations. During this example. even though it seems odd and not very usable.e.This is a useful operation when you are trying to solve a linear system. Scaling Multiply all entries in a row by a nonzero constant. There are three operations that are used to simplify a system. and multiply all the terms in an equation with a nonzero constant. iii. we are given the matrix Now a scaling operation has been performed on the first row. and can see that it will be easier to solve it by interchanging two rows.12) The augmented matrix for this system is 13 . You can replace one equation by the sum of itself and a multiple of another equation. Example 1: Solve this linear system x1 − 2x2 + x3 = 0 2x2 − 8x3 = 8 − 4x1 + 5x2 + 9x3 = − 9 …………………………………(2. by multiplying by -2 2. An example is. It is a widely used operation. and so forth.2.2 SOLVING LINEAR SYSTEM The basic strategy for solving linear systems is to replace one system with an equivalent system (i. we can interchange two equations. another system with the same solution set) that is easier to solve. use the x2 term from the second equation to eliminate the x2 terms in the other equations. until you get a very simple equivalent system of equations. we can see why these operations do not change the solution set of a system.

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 . 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 is called echelon form. we use x2 in equation 2 to eliminate the − 3x2 in equation 3 The new linear system has a triangular form. which will simplify arithmetic in the next step Now.The first thing we want is keep x1 in the first equation and eliminate it from the other equations. we add 4 times equation 1 to equation 3. To achieve this.

When doing so we obtain the linear system. However. which has a reduced echelon form Now that the linear system is solved. Carl D. The last step is to add 2 times equation 2 to equation 1. and therefore is a solution of the original linear system. 3). 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. it is a good practice to check your work.16.(Meyer. because of the many calculations involved. it shows that the only solution of the original system is (29.Now we are just one step away from obtaining the solution of the linear system. 2001) 15 .

let K be an arbitrary field. n. ………… am1 am2 ……… amn where the aij are scalars in K. or simply by (aij). and the n vertical m-tuples a11 a21 … am1 a12 a22 . A1n a2n …. j = 1. The above matrix is also denoted by (aij). is called a matrix over K. The matrix are being represented by upper case letters say A. a1n). while the numbers called entries or elements are represented by lower case letters say aij. amn) are the rows of the matrix. i=1. …………. only the coefficients and their respective positions are of great important. The m horizontal n-tuples (a11 a12 ……. B. A rectangular array of the form a11 a12 a21 a22 …. C e. cij respectively. 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. (am1 am2 ……. or simply a matrix if K is implicit. m. 3. ………. ….. a1n a2n … amn am2 16 .. For example. (a21 a22 ……. These matrices and certain algebraic operations defined on them shows that they are computational.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.t. ….c..1 ELEMENTS OF MATRIX ALGEBRA In working with a system of linear equations. bij. a2n).….….

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 a21 det (A) = a11 a21 a12 a22 a12 a22
……………………………………….(3.2) …………………………………………(3.1)

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 A= a21

a12 a13 a21 a23 a33
……………………………………………..(3.3)

a31. a32 a11 det(A) = a21 a31 a22 a23 det(A) = a11 a32 a33 - a12

a12 a13 a22 a32 a23 a33 a21 a31
……………………………………………….(3.4)

a23 + a13 a33

a21 a22 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 below det (A) = a11 a21 a31. a12 a22 a32 a13 a23 a33 a11 a21 a31. a12 a22 a32 ……………………(3.6) A in equation 3.3, the determinant using Sarrus rule is illustrated

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 a21 a22 ……… a2n a31 a32 ………a3n

b1 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 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 a21 a31. a12 a22 a32 a13 a23 a33 x x1 x2 x3 19 = b1 b2 b3 …………………….(3.8) ……………………………(3.7)

1) for n number of equations with n unknown we have a11x1 + a12x2 + . ii. … . second and the third column of matrix A...9) an1x1 + an2x2 + . x1 = det(Ax1)/ det(A). a12 a22 a32 b1 b2 b3 Thus the solution of the equations is. thus b1 det(Ax1) = b2 b3. x2 = det(Ax2)/det(A) and x3= det(Ax3)/ det(A). a12 a22 a32 a13 a23 a33 a11 det(Ax2) = a21 a31 b1 b2 b3 a13 a23 a33 a11 det(Ax3) = a21 a31. .. … .. one of the greatest mathematician who ever lived. Gaussian Elimination Method This is another method of solving linear systems... + a2nxn = b2 ..... …………………(3. It is called the Gaussian elimination method. Once again considering equation (2. + annxn = bn 20 . + a1nxn = b1 a21x1 + a22x2 + . named after Karl Fredrich Gauss. a12 a22 a32 a13 a23 a33 while the determinant det(Aj) of the jth column obtained by substituting the column vector (B) into the first.Axx=B The determinant of the coefficient matrix A will be a11 det(A) = a21 a31.

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

a craft or an engineering discipline. This debate is analogous to that surrounding the SapirWhorf hypothesis(Kenneth E.2. Another ongoing debate is the extent to which the programming language used in writing computer programs affects the form that the final program takes. 1980) in linguistics. 3. 22 . because it acknowledges that the mechanisms of any language condition the thoughts of its speaker community.ongoing debate on the extent to which the writing of programs is an art. Another sophisticated programmable machine by Al-Jazari was the castle clock. 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". programming is the craft of transforming requirements into something that a computer can execute. Iverson. with the goal of producing an efficient and evolvable software solution (the criteria for "efficient" and "evolvable" vary considerably). 2003)Good programming is generally considered to be the measured application of all three. His mechanism had a programmable drum machine with pegs (cams) that bump into little levers that operate the percussion. that postulates that a particular language's nature influences the habitual thought of its speakers. The drummer could be made to play different rhythms and different drum patterns by moving the pegs to different locations. Different language patterns yield different patterns of thought.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. This idea challenges the possibility of representing the world perfectly with language. Said another way. (Paul Graham. Al-Jazari's robot was originally a boat with four automatic musicians that floated on a lake to entertain guests at royal drinking parties.

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

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

network bandwidth and to some extent even user interaction). ii. Algorithmic Complexity The academic field are and the largely engineering concerned practice with of computer and programming both discovering implementing the most efficient algorithms for a given class of problem. This involves a wide range of textual and graphical elements that makes a program easy and comfortable to use. The focus is mainly on user interaction and the handling of exceptions. memory space.  Robustness: how well a program anticipates situations of data type conflict and other incompatibilities that result in run time errors and program halts.  Reliability: how often the results of a program are correct.3 Modern Programming i. This depends on prevention of error propagation resulting from data conversion and prevention of errors resulting from buffer overflows.  Usability: the clarity and intuitiveness of a programs output can make or break its success. Quality requirements Whatever the approach to software development may be. 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.  Portability: the range of computer hardware and operating system platforms on which the source code of a program can be compiled/interpreted and run. the less the better. slow devices. underflows and zero division.3. 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. the final program must satisfy some fundamental properties. 25 .2.

There exist a lot of differing approaches for each of those tasks. Debugging Debugging is a very important task in the software development process. and C in embedded applications). Methodologies The first step in most formal software development projects is requirements analysis. followed by testing to determine value modeling. v. the number of books teaching the language that are sold (this overestimates the importance of newer languages). implementation. while some languages are regularly used to write many different kinds of applications. One approach popular for requirements analysis is Use Case analysis. iii. 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). often on large mainframes. Measuring Language Usage It is very difficult to determine what are the most popular of modern programming languages. which expresses resource use. COBOL is still strong in the corporate data center. iv. Use of a static analysis tool can help detect 26 . 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. such as execution time or memory consumption. because an erroneous program can have significant consequences for its users.g. 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. Methods of measuring language popularity include: counting the number of job advertisements that mention the language. Some languages are very popular for particular kinds of applications (e. FORTRAN in engineering applications. algorithms are classified into orders..For this purpose. and failure elimination (debugging).

( Osborne. It was renamed to C++ in 1983. It is a statically typed. multi-paradigm. the programming language best suited for the task at hand will be selected. 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". device drivers. availability of third-party packages. The choice of language used is subject to many considerations. NetBeans.2002) C++ enjoys wide use in the software industry. nevertheless my choice for this programe is C++. among other features. or individual preference. It is regarded as a middle-level language. and generic programming. Debugging is often done with IDEs like Visual Studio. operator overloading. Enhancements started with the addition of classes. suitability to task.some possible problems. as it comprises a combination of both high-level and low-level language features. multiple inheritance. and exception handling. such as company policy. embedded software. Some of its application domains include systems software. followed by. 3. templates. 3. object-oriented programming. Ideally. and these often provide less of a visual environment.3 CONCEPT OF C++ C++ (C Plus Plus) is a general-purpose programming language. and entertainment software such as video games. Several groups provide both free and 27 . free-form. usually using a command line. compiled language where compilation creates machine code for a target machine hardware.data abstraction. Standalone debuggers like gdb are also used.2. supports procedural programming.4 Programming Languages Different programming languages support different styles of programming (called programming paradigms). high-performance server and client applications. and Eclipse.virtual functions.

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

Compiler authors are. There is no language called "C plus". even if this makes it possible for the programmer to choose incorrectly C++ is designed to be as compatible with C as possible. object-oriented programming. This provides such useful tools as containers (for example vectors and lists). 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.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. free to implement the standard in their own manner.3. iterators 29 . ABCL/c+ was the name of an earlier. unrelated programming language. data abstraction. 1996) describes how compilers may convert C++ program statements into an in-memory layout. 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. however. In The Design and Evolution of C++ (1994). 3. Another large part of the C++ library is based on the STL.credited to Rick Mascitti (mid-1983) and was first used in December 1983. and generic programming)   C++ is designed to give the programmer choice. 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.

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. 2000) #include <iostream> // provides std::cout int main() { std::cout << "Hello. Furthermore (multi)maps (associative arrays) and (multi)sets are provided. and . covering basic arithmetic. Compiler-independent implementations of the STL. such as smart pointers. } i. 3.3.*). bit manipulation. world!\n".3 Language features C++ inherits most of C's syntax and the C preprocessor. Most C++ compilers provide an implementation of the C++ standard library. comparisons. of which 19 are deprecated. nor does it change the number of operands that the operator uses (any operand may however be ignored). C++ provides 69 standard headers. indirection. logical operations and more. Almost all operators can be overloaded for user-defined types. with a few notable exceptions such as member access (. including the STL.to provide these containers with array-like access and algorithms to perform operations such as searching and sorting. As in C. The following is a Hello world program which uses the C++ standard library stream facility to write a message to standard output (Stroustrup Bjarne. Therefore it is possible. all of which export compatible interfaces. Overloading an operator does not change the precedence of calculations involving the operator. 30 . to write generic algorithms that work with any container or on any sequence defined by iterators. Operators and operator overloading C++ provides more than 30 operators. the features of the library are accessed by using the #include directive to include a standard header. using templates.

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

including all of C. (Herb Sutter. this can result in a complicated graph of inheritance relationships. a "class" inherits privately. 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.2003) With C C++ is often considered to be a superset of C. avoiding some of the ambiguity problems of multiple inheritance. Most C code can easily be made to compile correctly in C++.frequently used. 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. For many years.5 Criticism Modern critics of the language raise several points. since C++ is based on and largely compatible with C. Taken as a whole.4 Compatibility Producing a reasonably standards-compliant C++ compiler has proven to be a difficult task for compiler vendors in general. Multiple inheritance allows a class to be derived from more than one base class. 3. If the access specifier is omitted. while a "struct" inherits publicly.3. 3. but there are a few differences that cause some valid C code to be invalid in C++. in part leading to criticisms of being a "bloated" and complicated language. Base classes may be declared as virtual. this is called virtual inheritance. 2008) The 32 .3. C++ has a large feature set." (Bjarne Stroustrup. plus a large set of its own additions. Virtual inheritance ensures that only one instance of a base class exists in the inheritance graph. it inherits most of the criticisms leveled at that language. Multiple inheritance is a C++ feature sometimes considered controversial. Recent releases of most popular C++ compilers support almost all of the C++ 1998 standard. or to behave differently in C++. but this is not strictly true. First.

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

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. It only holds for a 3x3 matrix.CHAPTER FOUR IMPLIMENTATION Having analyzed the existing system in the previous chapter. As was stated in chapter one.(4. I am going to manually solve a 4x4 system of linear equation with 4 unknown.1 MANUAL SOLUTION USING CRAMMER’S RULE For a 4x4 or higher matrices.3) 34 . However Sarous rule is not applicable in this case..w = 1 x + 2y – 2z + w = 1……………………………………(4. 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.1) Solution: I shall first consider the Crammer’s rule for the above equations and the Gaussian Elimination method. 4. 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.(4. Writing the matrix in compact form Ax=B we have it as followed 2 1 1 -2 -1 0 0 2 1 5 -1 -1 1 2 2 1 x y = z w 1 0 1 1 …………………………………. Problem: Solve the systems of linear equations 2x + y + z – 2w = 1 -x +2w = 0 x +5y – z .2) The determinant of the coefficient matrix det(A) 2 -1 1 1 1 1 -2 0 0 2 5 -1 -1 2 2 1 det(A) = ………………………………………….

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

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

0 -1/2 0 1/2 1/6 1/2 0 1 -1/18 1 /2 4 /9 0 0 2/3 1 R3 1/3 and add it to the fourth row Step 7: Multiplying the second row by - to get a new R4 as shown below. R4 R1 R2 R3 R4 absolute 2 1 1 -2 1 1/2 1/2 1/2 0 1 9/2 1/2 0 -3/2 0 3/2 3/2 1/2 0 2 Step 5: From R2 to R4. 0 0 0 -3/2 3/2 0 1/2 3/2 0 2 2 2 -1/6 1/2 1/3 R4 Also now we have a matrix of the form. so we interchange R3 with R2 and use R2 to eliminate R3 and R4 1 -2 -3/2 0 1/2 1 3/2 2 1 1/ 2 1/2 1/2 R1 R2 R3 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. 2 0 0 0 1 9/2 0 0 1 -2 3/2 0 2/3 1 2 2 1 1/2 4/9 1/3 R1 R2 R3 R4 Step 8: Once again we interchange R3 and R4.1/ 2 2 3/ 2 1 1 2 -1/2 1 1 /2 3/2 0 Now we have the matrix of the form. since R4 has the element with the largest absolute value and we use R3 to eliminate R4 37 . 2 0 0 0 1 9 /2 1/2 3 /2 value.-1 1 -1/2 2 . R3 has the element with the largest respectively.

.833333 …………. 0 0 0 2 0 0 0 1 9/2 0 0 0 -3/2 2/3 -3/2 1 1/3 -1/9 4/9 1/3 0 Thus giving us an augmented matrix of the form. which should be compatible with INTEL pentium (III) processor.11) SYSTEM REQUIREMENT CONFIGURATIONS AND MAINTENANCE In ordr to achieve the proposed software design. . . .(4.166667 …………. . . . . z and y into eqn. we have.(iii) . . 38 .(iii): 2z + 2(1) = 1/3 gives us z =-5/6 = -0. . . .9) Substituting z into eqn. .8) . . . .3 …………………(4. (i) : 2x +(-1/6) + (-5/6) – 2(1) = 1 Which gives us our x = 2 4. . (ii) : 9/2y . . .(4. . . .3/2(-5/6) = 1/2 this gives us y = -1/6 = -0. . . (i) . 2x + y + z – 2w = 1 9/2y . . 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. . . . .. . .10) Now substituting the values of w.. .(iv) we have w = 1 Substituting w into eqn. . . . there will be the need for at least a personal desktop computer. . . .3 /2 z = 1/2 1/3 w= 1/3 2z + 2w = 1/3 Now by using back-substitution from eqn. . .Step 9: Multiply the third row with -1/3 and add it to fourth row to get a new R4 as shown below.(ii) . . . 1 -2 3/2 0 2 2 1/3 0 1 1/2 1 /3 1/3 0 0 Making the above system of linear equations. . .(iv) ………(4.

39 .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.

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. 5. CONCLUSION AND RECOMMENDATION 5.3 RECOMMENDATION With the accuracy. 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. the constant use of this software application will anticipate steady progress in developing one’s capacity for abstract thought and careful reasoning. More so. 40 . complex effort and possibilities of errors prone nature of the manual manipulations. 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.2 CONCLUSION Considering the time consuming.CHAPTER FIVE SUMMARY. 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. 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. the software alternative has restored back the confidence of handling complex linear systems with the hope of getting a reliable solution with great speed.1 SUMMARY This project work has being created for the production of a software supplement to manual calculation of systems of linear equations.

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

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

11.Compute the determinant of (Az). Input the column matrix B 5. x3 and x4 16End PROGRAM LISTING FOR CRAMMER’S RULE /* Program: cramers rule. Delete the second column of matrix A and replace it with the column vector B and store it as Ay. Delete the first column of matrix A and replace it with the column vector B and store it as Ax. 7. 43 . 13Compute the determinant of (Aw). x2. Input the coefficient matrix A 4. 12 Delete the fourth column of matrix A and replace it with the column vector B and store it as Aw.APPENDIX ALGORITHM FOR CRAMMER’S RULE 1. Compute the determinant of (Ay). Start 2. Compute the determinant of (Ax).Delete the third column of matrix A and replace it with the column vector B and store it as Az. 14Divide each of the determinant det(Ax). 8. 10.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. det(Ay). det(Az) and det(Aw) by the determinant of A : det(Ax) 15Display the results of x1. Specify the order of the coefficienct matrix n 3. 9. Compute the determinant of the coefficient matrix: det(A) 6.

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

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

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

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

float *cvm_variable. cvm_size). return. float *cvm_constant. cvm_determinant_modified. cvm_coefficient. cvm_determinant_modified[0] = cvm_constant[0]. cvm_size). cvm_determinant_original.} void calculate_variable_matrix(float &cvm_determinant_original. cvm_constant. float cvm_coefficient[][MAX_SIZE]. pr_determinant_original). int cvm_size) { if (cvm_size == 1) { cvm_determinant_original = cvm_coefficient[0][0]. calculate_modified_determinant(cvm_determinant_modified. pr_determinant_original). float *cvm_determinant_modified. } void print_results(float *pr_variable. int pr_size. float { print_determinants(pr_determinant_modified. } else { calculate_determinant(cvm_determinant_original. } calculate_variables(cvm_variable. return. cvm_coefficient. pr_size. print_variables(pr_variable. } pr_determinant_original) 48 . cvm_size). pr_size. float *pr_determinant_modified.

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

j ++) temp_mat[i][j] = temp_mat[i][j] / temp_co. } void calculate_modified_determinant(float cmd_determinant_modified[]. for (i = 0. float *cmd_constant. j. float cmd_coefficient[][MAX_SIZE]. calculate_determinant(cmd_determinant_modified[i]. cmd_size). temp_mat. i < cmd_size. for (i = 0. } void calculate_variables(float *cv_variable. j < cmd_size. float cv_determinant_original. i < cv_size. i ++) 50 . j ++) temp_mat[j][i] = cmd_constant[j].1). temp_mat. i ++) { copy_matrix(cmd_coefficient. } return. j < cd_size. float *cv_determinant_modified. cmd_size). int cmd_size) { int i. l >= i. l --) temp_mat[k][l] = temp_mat[k][l] . } return. } for (k = i + 1. k ++) for (l = (cd_size . k < cd_size.(temp_mat[k][i] * temp_mat[i][l]). int cv_size) { int i. for (j = 0.for (j = i. float temp_mat[MAX_SIZE][MAX_SIZE].

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

matrix[initRow][j] = matrix[i][j]. j. j < cm_size. j ++) matrix2[i][j] = matrix1[i][j]. } return false. for (i = initRow. initCol = sr_i. i < sr_size. i ++) for (j = 0. } bool swap_rows(float matrix[][MAX_SIZE]. int sr_i. i. return. j. float temp. for (i = 0. matrix[i][j] = temp. i < cm_size. } OUTPUT LISTING OF THE CRAMMER’S RULE PROGRAM This program was written completely by KALIO ENOCH PRECIOUS for Students of MATHEMATICS/COMPUTER SCIENCE. int cm_size) { int i. float matrix2[][MAX_SIZE]. j < sr_size. } return true. i ++) if (matrix[i][initCol] != 0) { for (j = initCol.void copy_matrix(float matrix1[][MAX_SIZE]. 52 . j ++) { temp = matrix[initRow][j]. int sr_size) { int initRow = sr_i. This program will solve a square linear system using Cramer's Rule.

where A is a square (n x n) coefficient matrix and . When entering the rows.166667 x3 = -0. First. x is a variable (n x 1) matrix. enter n (the size of the square matrix): 4 Enter the coefficient matrix (A). 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).Ax = b. and b is a constant (n x 1) matrix.833333 x4 = 1 Do you want to solve another system (y/n)? 53 . 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. put one space between each term in the row.

1 has the largest absolute value among all the ai. 10. 6. End. multiply a1.1 to eliminate the remaining rows. double* vectorAlloc(int).j.j. Start 2. Find the row Rk such that ai. 7.2/a2.j 4.2 has the largest absolute value among the ai.j so that the pivoting element a2.j.1/a11 and add the row to ai. 8.1 and interchange the row with a1. Interchange rows excluding a1. Solve the triangular system by back substitution.1 of ai.3 in the sub-sub system. double** matrixAlloc(int.2 in the subsystem. Use the first row a1. 54 .j so that the pivoting element a3. Interchange rows excluding a2. int).3 and add the row to ai. For i > 3 multiply a3. For i > 2 multiply a2.2 and add the row to ai.3/a3.j by –ai.h> #include <iostream> #include <iomanip> #include <cmath> #include <new> using namespace std.j by –ai.j and in particular a1.ALGORITHM FOR GAUSSIAN ELIMINATION METHOD 1. 5. PROGRAM LISTING FOR THE GAUSSIAN ELIMINATION METHOD // C++ PROGRAM of Gaussian Elimination Method // Programmed by KALIO ENOCH PRECIOUS // Date : OCTOBER 2008 #include <stdlib.3 has the largest absolute value among the ai. Declare the array in rows and columns 3.j by –ai. 9. For i > 1.

a = matrixAlloc(n. cout << " Input number of equations (that the order n)= ". i < n. cout << " cout << " cout << " endl. int). cout << " endl << endl. for ( int i = 0.j)=" << endl. sum." << endl. j < n. j++) 55 ************ Gaussian Elimination Method *******" << THIS PROGRAM WAS CODED BY KALIO ENOCH OF MATHS/STATISTICS DEPARTMENT" << endl.void matrixFree(double**. for ( int j = 0. void vectorFree(double*). i++) { cout << "Enter row " << i + 1 << ": ". double* b. int). . int main() { double** a. double x. cout << "Input matrix coefficients a(i. void matrixPrint(double**. int n. cin >> n. b = vectorAlloc(n). void vectorPrint(double*. int). n). put one space between each term in the row. MATHS/COMPUTER SCIENCE OPTION" << endl << PRECIOUS " << endl. cout << "When entering the rows.

} cout << "Input right-hand side vector b(i)" << endl. n). i++) { cout << "Enter row " << i + 1 << ": ". exit(1). cout << "Coefficient matrix A:" << endl. n). cin >> b[i]. // convert to upper triangular form for (int k=0. } 56 .k++) { if ( fabs(a[k][k])>=1. for (int j=k+1. i < n.cin >> a[i][j].k<n-1. for (int i = 0.i<n. matrixPrint(a. b[i] = b[i] .e-6) { for (int i=k+1.b[k]*x. vectorPrint(b. } } else { cout << "zero pivot found in line:" << k << endl.i++) { x = a[i][k]/a[k][k]. cout << "Right-hand-side vector b:" << endl.j<n. } cout << endl << endl.j++) a[i][j] = a[i][j] -a[k][j]*x.

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

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

{ for ( int j = 0. cout << endl. j < n. i++) cout << setiosflags(ios::showpoint | ios::fixed | ios::right) << setprecision(4) << setw(12) << p[i]. 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 .j)= When entering the rows. i < n. } 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. cout << endl << endl. put one space between each term in the row. int n) { for ( int i = 0. j++) cout << setiosflags(ios::showpoint | ios::fixed | ios::right) << setprecision(4) << setw(12) << p[i][j]. } } void vectorPrint(double* p.

0000 1.0000 -1.0000 1.0000 0.0000 2.0000 1.0000 5.0000 -1.0000 Right-hand-side vector b: 60 .0000 0.0000 2.8333 1.0000 1.0000 2.0000 1.0000 0.0000 -1.0000 Solution: 2.0000 -2.1667 -0.0000 -0.0000 1.Enter row 1: 1 Enter row 2: 0 Enter row 3: 1 Enter row 4: 1 Coefficient matrix A: 2.0000 1.0000 1.

Sign up to vote on this title
UsefulNot useful