CS201- Introduction to Programming- Lecture 43

Post on 18-Dec-2014

15 views 1 download

Tags:

description

Virtual University Course CS201- Introduction to Programming Lecture No 43 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Transcript of CS201- Introduction to Programming- Lecture 43

Introduction to Introduction to ProgrammingProgramming

Lecture # 43Lecture # 43

Math Library

Complex number Matrix Quadratic equation and their solution

…………….…

Design RecipeDesign Recipe To design a program properly, we must :To design a program properly, we must :

– Analyze a problem statement, typicallyAnalyze a problem statement, typically expressed as a word problemexpressed as a word problem– Express its essence, abstractly and with Express its essence, abstractly and with examplesexamples– Formulate statements and comments in a Formulate statements and comments in a precise language i.e. codeprecise language i.e. code– Evaluate and revise the activities in light Evaluate and revise the activities in light

of checks and tests andof checks and tests and– PAY ATTENTION TO DETAILPAY ATTENTION TO DETAIL

Matrix• Matrix is nothing but a two dimensional array of numbers• Normally, represented in the form of :

• Rows• Columns

Example1 2 3 4

5 6 7 8 9 10 11 12

A =

Three RowsFour Columns

i & j are two Integers

i representing the Row numberj representing the Column number

Operations Performed with Matrix

• Addition of two matrices.• Addition of a scalar and a matrix• Subtraction of two matrices• Subtraction of a scalar from a matrix• Multiplication of two matrices• Multiplication of a scalar with a matrix• Division of a scalar with a matrix• Transpose of a matrix

Interface

Addition of two Matrices

Aij+Bij Cij=

Size of two matrices must be same

Number of rows and columns must be identical for the matrices to be addable

Addition of two Matrices

Example1 2 3

5 6 7

9 10 11

-3 6 8

7 4 7

9 10 1

=-2 -4 -5

-2 2 0

0 0 10

Aij - BijCij =

Ordinary number added to every element of the matrix

Adding a Scalar to the Matrix

Ordinary number subtracted from every element of the matrix

Subtracting a Scalar from a Matrix

Divide every element of Matrix by a scalar number

Division of Matrix by a Scalar

Example

Let :X be a Scalar numberA be a Matrix

AijCij

=

X

Multiplication of a scalar with a Matrix :

ExampleLet :

X is a Scalar numberA is a Matrix

A ij Cij=X *

X * A

Multiply two Matrices

1 25 6 =*

2 41 2

( 1 ) ( 2 ) + ( 2 ) ( 1 ) ( 1 ) ( 4 ) + ( 2 ) ( 2 )

( 5 ) ( 2 ) + ( 6 ) ( 1 ) ( 5 ) ( 4 ) + ( 6 ) ( 2 )

Number of columns of the 1st Matrix =Number of rows of the 2nd Matrix

Rules Regarding Matrix Multiplication

Rules regarding Matrix Rules regarding Matrix MultiplicationMultiplication

First matrix hasFirst matrix has– M rowsM rows– N columnsN columns

Second matrix has Second matrix has – N rowsN rows– P columnsP columns

Resultant matrix will haveResultant matrix will have– M rowsM rows– P columnsP columns

Transpose of a Matrix

Interchange of rows and columns

Transpose of a MatrixExample

1 2 3 5 6 7 9 10 11

1 5 9 2 6 10 3 7 11

Transpose of a Non Square Matrix

A AT

3 ( Rows ) * 4 ( Columns ) Before

4 ( Rows ) * 3 ( Columns ) After

Size of matrix change after transpose

Next Phase of Analysis

• Determine the Constants• Memory Allocation• What is it’s user interface

Interface

InterfaceInterfaceConstructor : Parameters are Constructor : Parameters are

Number of rows Number of rows Number of columns Number of columns

Display functionDisplay functionPlus operator : member operator of the class Plus operator : member operator of the class Subtraction operator : member operator of the Subtraction operator : member operator of the class class Plus operator : friend of the class Plus operator : friend of the class Subtraction operator : friend of the class Subtraction operator : friend of the class

Plus Operator

A + X

X + A

Subtraction Operator

A - X

X – A

*

Interface Interface

Multiplication Operator : Member of the Class Multiplication Operator : Member of the Class

Multiplication Operator : Friend of the Class Multiplication Operator : Friend of the Class

Division Operator : Member of the ClassDivision Operator : Member of the Class

Transpose Function : Member of the ClassTranspose Function : Member of the Class

Assignment Operator : Member of the ClassAssignment Operator : Member of the Class

+= , -= : Members of the Class += , -= : Members of the Class

Multiplication Operator

A * X

X * A

Assignment Operator

A = B( Member Operator )

>> Extraction Operator : Friend Operator

<< Stream Insertion Operator : Friend Operator

Interface

Copy Constructor

Copy ConstructorCopy Constructor Assignment OperatorAssignment Operator Memory AllocationMemory Allocation Memory DeallocationMemory Deallocation