CS201- Introduction to Programming- Lecture 43

34
Introduction to Introduction to Programming Programming Lecture # 43 Lecture # 43

description

Virtual University Course CS201- Introduction to Programming Lecture No 43 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 43

Page 1: CS201- Introduction to Programming- Lecture 43

Introduction to Introduction to ProgrammingProgramming

Lecture # 43Lecture # 43

Page 2: CS201- Introduction to Programming- Lecture 43

Math Library

Complex number Matrix Quadratic equation and their solution

…………….…

Page 3: CS201- Introduction to Programming- Lecture 43

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

Page 4: CS201- Introduction to Programming- Lecture 43

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

• Rows• Columns

Page 5: CS201- Introduction to Programming- Lecture 43

Example1 2 3 4

5 6 7 8 9 10 11 12

A =

Three RowsFour Columns

Page 6: CS201- Introduction to Programming- Lecture 43

i & j are two Integers

i representing the Row numberj representing the Column number

Page 7: CS201- Introduction to Programming- Lecture 43

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

Page 8: CS201- Introduction to Programming- Lecture 43

Interface

Page 9: CS201- Introduction to Programming- Lecture 43

Addition of two Matrices

Aij+Bij Cij=

Page 10: CS201- Introduction to Programming- Lecture 43

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

Page 11: CS201- Introduction to Programming- Lecture 43

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 =

Page 12: CS201- Introduction to Programming- Lecture 43

Ordinary number added to every element of the matrix

Adding a Scalar to the Matrix

Page 13: CS201- Introduction to Programming- Lecture 43

Ordinary number subtracted from every element of the matrix

Subtracting a Scalar from a Matrix

Page 14: CS201- Introduction to Programming- Lecture 43

Divide every element of Matrix by a scalar number

Division of Matrix by a Scalar

Page 15: CS201- Introduction to Programming- Lecture 43

Example

Let :X be a Scalar numberA be a Matrix

AijCij

=

X

Page 16: CS201- Introduction to Programming- Lecture 43

Multiplication of a scalar with a Matrix :

ExampleLet :

X is a Scalar numberA is a Matrix

A ij Cij=X *

X * A

Page 17: CS201- Introduction to Programming- Lecture 43

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 )

Page 18: CS201- Introduction to Programming- Lecture 43

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

Rules Regarding Matrix Multiplication

Page 19: CS201- Introduction to Programming- Lecture 43

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

Page 20: CS201- Introduction to Programming- Lecture 43

Transpose of a Matrix

Interchange of rows and columns

Page 21: CS201- Introduction to Programming- Lecture 43

Transpose of a MatrixExample

1 2 3 5 6 7 9 10 11

1 5 9 2 6 10 3 7 11

Page 22: CS201- Introduction to Programming- Lecture 43

Transpose of a Non Square Matrix

A AT

3 ( Rows ) * 4 ( Columns ) Before

4 ( Rows ) * 3 ( Columns ) After

Size of matrix change after transpose

Page 23: CS201- Introduction to Programming- Lecture 43

Next Phase of Analysis

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

Page 24: CS201- Introduction to Programming- Lecture 43

Interface

Page 25: CS201- Introduction to Programming- Lecture 43

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

Page 26: CS201- Introduction to Programming- Lecture 43

Plus Operator

A + X

X + A

Page 27: CS201- Introduction to Programming- Lecture 43

Subtraction Operator

A - X

X – A

Page 28: CS201- Introduction to Programming- Lecture 43

*

Page 29: CS201- Introduction to Programming- Lecture 43

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

Page 30: CS201- Introduction to Programming- Lecture 43

Multiplication Operator

A * X

X * A

Page 31: CS201- Introduction to Programming- Lecture 43

Assignment Operator

A = B( Member Operator )

Page 32: CS201- Introduction to Programming- Lecture 43

>> Extraction Operator : Friend Operator

<< Stream Insertion Operator : Friend Operator

Interface

Page 33: CS201- Introduction to Programming- Lecture 43

Copy Constructor

Page 34: CS201- Introduction to Programming- Lecture 43

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