CS201- Introduction to Programming- Lecture 13

29
Introduction to Introduction to Programming Programming Lecture 13 Lecture 13

description

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

Transcript of CS201- Introduction to Programming- Lecture 13

Page 1: CS201- Introduction to Programming- Lecture 13

Introduction to Introduction to ProgrammingProgramming

Lecture 13Lecture 13

Page 2: CS201- Introduction to Programming- Lecture 13

Today’s Today’s LectureLecture

Manipulation of Two Manipulation of Two dimensional arraysdimensional arrays

Analyzing and solving a real Analyzing and solving a real world problemworld problem

Page 3: CS201- Introduction to Programming- Lecture 13

Array Array ManipulatioManipulatio

nn

Page 4: CS201- Introduction to Programming- Lecture 13

Example 1Example 1

998877

665544

332211Row 1

Row 2

Row 3

332211

665544

998877

Row 1

Row 2

Row 3998877

665544

332211Row 1

Row 2

Row 3

Input

Output

Memory

Page 5: CS201- Introduction to Programming- Lecture 13

Addressing Array Addressing Array ElementsElements

a [rowIndex ] [ columnIndex ]a [rowIndex ] [ columnIndex ]

Page 6: CS201- Introduction to Programming- Lecture 13

Example 1Example 1int row ;int row ;int col ;int col ;const maxRows = 3 ;const maxRows = 3 ;const maxCols = 3 ;const maxCols = 3 ;int a [ maxRows ] int a [ maxRows ] [ maxCols ] ;[ maxCols ] ;

Page 7: CS201- Introduction to Programming- Lecture 13

Example 1Example 1for ( row = 0 ; row < maxRows ; row ++ )for ( row = 0 ; row < maxRows ; row ++ ){{

for ( col = 0 ; col < maxCols ; col ++ )for ( col = 0 ; col < maxCols ; col ++ ){{

cout << “Please enter value of cout << “Please enter value of element number ”<<row<< element number ”<<row<<

“,” << col ;“,” << col ;cin >> a [ row ] [ col ] ;cin >> a [ row ] [ col ] ;

}}}}

Page 8: CS201- Introduction to Programming- Lecture 13

Example 2Example 2

332211[0][0]

[1][1]

[2][2]

Index of Start

Index of Last Row = maxRows - 1

maxRows = 3 ;maxRows = 3 ;

maxCols = 3 ;maxCols = 3 ;

Page 9: CS201- Introduction to Programming- Lecture 13

Example 2Example 2for ( row = maxRows - 1 ; row >= 0 ; row -- )for ( row = maxRows - 1 ; row >= 0 ; row -- )

{{

for ( col = 0 ; col < maxCols ; col ++ )for ( col = 0 ; col < maxCols ; col ++ )

……

}}

332211

665544

998877

Row 1

Row 2

Row 3

998877

665544

332211Row 1

Row 2

Row 3

Decrement Operator

Page 10: CS201- Introduction to Programming- Lecture 13

Example 2: Formatted Example 2: Formatted OutputOutput

cout << “The original matrix is” ;cout << “The original matrix is” ;

for ( row = 0 ; row < maxRows ; row ++ )for ( row = 0 ; row < maxRows ; row ++ )

{{

for ( col = 0 ; col < maxCols ; col +for ( col = 0 ; col < maxCols ; col ++ )+ )

{{

cout << a [ row ] [ col ] ;cout << a [ row ] [ col ] ;

}}

}}

<< ‘\t‘ ;

15 42

Page 11: CS201- Introduction to Programming- Lecture 13

Example 2: Formatted Example 2: Formatted OutputOutput

for ( row = 0 ; row < maxRows ; row ++ )for ( row = 0 ; row < maxRows ; row ++ )

{{

for ( col = 0 ; col < maxCols ; col ++ )for ( col = 0 ; col < maxCols ; col ++ )

{{

cout << a [ row ] [ col ] << ‘\t’ ;cout << a [ row ] [ col ] << ‘\t’ ;

}}

cout << ‘ \n ’ ;cout << ‘ \n ’ ;

}}15 42 26 7

Page 12: CS201- Introduction to Programming- Lecture 13

ExerciseExerciseEnter the values in a matrix and Enter the values in a matrix and print it in reverse Column orderprint it in reverse Column order

998877

665544

332211

[0] [1] [2]

778899

445566

112233

[2] [1] [0]

Page 13: CS201- Introduction to Programming- Lecture 13

Transpose of a Transpose of a MatrixMatrix

998877

665544

332211

Page 14: CS201- Introduction to Programming- Lecture 13

Square MatrixSquare Matrix

arraySize = arraySize = rowscols

Number of rows are equal to number of columns

Page 15: CS201- Introduction to Programming- Lecture 13

Square MatrixSquare Matrix

a a ijij = a = a jiji

i = rowsi = rows

j = columnsj = columns

Page 16: CS201- Introduction to Programming- Lecture 13

Square MatrixSquare Matrixint a [ row ] [ col ] ;int a [ row ] [ col ] ;int arraySize ;int arraySize ;for ( row = 0 ; row < arraySize ; row ++ )for ( row = 0 ; row < arraySize ; row ++ ){{

for ( col = 0 ; col < arraySize ; col ++ )for ( col = 0 ; col < arraySize ; col ++ ){{

//Swap values//Swap values}}

}}

Page 17: CS201- Introduction to Programming- Lecture 13

Swap Swap MechanismsMechanisms

temp = a [ row ] [ col ] ;temp = a [ row ] [ col ] ;

a [ row ] [ col ] = a [ col ] [ row ] ;a [ row ] [ col ] = a [ col ] [ row ] ;

a [ col ] [ row ] = temp ;a [ col ] [ row ] = temp ;

Page 18: CS201- Introduction to Programming- Lecture 13

Practical Practical ProblemProblem

Problem statementProblem statement Given tax brackets and given Given tax brackets and given

employee gross salaries , determine employee gross salaries , determine those employees who actually get those employees who actually get less take home salary than others less take home salary than others with lower initial incomewith lower initial income

Page 19: CS201- Introduction to Programming- Lecture 13

Rule for tax Rule for tax deductiondeduction

0 0 –> 5,000 –> 5,000 No taxNo tax

5001 – >10,0005001 – >10,000 5% Income Tax5% Income Tax

10,001 – >20,00010,001 – >20,000 10% Income Tax10% Income Tax

20,001 and more 20,001 and more 15% Income tax15% Income tax

Page 20: CS201- Introduction to Programming- Lecture 13

ExampleExampleNet salary = Rs Net salary = Rs 10,00010,000

Tax = Tax = 5%5%

Amount Deducted = 5% of 10,000Amount Deducted = 5% of 10,000

= = 500500

Net amount after deduction = 10,000 - 500Net amount after deduction = 10,000 - 500

= = 9,5009,500

Net salary = Rs Net salary = Rs 10,00110,001

Tax = Tax = 10%10%

Amount Deducted = 10% of 10,001Amount Deducted = 10% of 10,001

= = 1,000.11,000.1

Net amount after deduction = 10,001 - 1,000.1Net amount after deduction = 10,001 - 1,000.1

= = 9,000.99,000.9

Page 21: CS201- Introduction to Programming- Lecture 13

Storage Storage RequirementRequirement

One- dim arrays of One- dim arrays of integerinteger

lucky = 0 lucky = 0

lucky = 1lucky = 1

00

00

00

00

00

00

00

Page 22: CS201- Introduction to Programming- Lecture 13

Storage of Storage of salarysalary

11 5,0005,000 5,0005,000

22 10,00010,000 9,5009,500

33

44

55

66

77

88

99

1010

GrowSalary

Net Salary After Deduction

No ofEmp.

Page 23: CS201- Introduction to Programming- Lecture 13

Interface Interface RequirementsRequirements

Page 24: CS201- Introduction to Programming- Lecture 13

Distribution of the Distribution of the ProgramProgram

InputInput Salary calculationSalary calculation Identification of the Identification of the

unluckyunlucky

individualsindividuals OutputOutput

Page 25: CS201- Introduction to Programming- Lecture 13

Detail DesignDetail DesignFunctions in the programFunctions in the program

getInput getInput calculateSalarycalculateSalarylocateUnluckyIndividuallocateUnluckyIndividualdisplayOutputdisplayOutput

Page 26: CS201- Introduction to Programming- Lecture 13

CodeCode#include<iostream.h>#include<iostream.h>

void getinput ( int [ ] [ 2 ] , int ) ;void getinput ( int [ ] [ 2 ] , int ) ;main ( )main ( ){{

const int arraySize = 100 ;const int arraySize = 100 ;int sal [ arraySize ] [ 2 ] ;int sal [ arraySize ] [ 2 ] ;int lucky [ arraySize ] = { 0 } ;int lucky [ arraySize ] = { 0 } ;int numEmps ;int numEmps ;cout << “Enter the number of cout << “Enter the number of

employess “ ;employess “ ;cin >> numEmps ;cin >> numEmps ;getInput ( sal , numEmps ) ;getInput ( sal , numEmps ) ;

}}

Page 27: CS201- Introduction to Programming- Lecture 13

CodeCodegetInput ( int sal [ ] [2] , int getInput ( int sal [ ] [2] , int

numEmps )numEmps )

{{

for ( i = 0 ; i < numEmps ; i ++ )for ( i = 0 ; i < numEmps ; i ++ )

cin >> sal [ i ] [ 0 ] ;cin >> sal [ i ] [ 0 ] ;

}}

Page 28: CS201- Introduction to Programming- Lecture 13

[email protected]@vu.edu.pkk

Page 29: CS201- Introduction to Programming- Lecture 13

ExerciseExercise

Suppose you are given a Suppose you are given a square matrix of size n x n , square matrix of size n x n , write a program to determine write a program to determine if this is an identity matrix if this is an identity matrix