CS201- Introduction to Programming- Lecture 13
-
Upload
bilal-ahmed -
Category
Education
-
view
33 -
download
1
description
Transcript of CS201- Introduction to Programming- Lecture 13
Introduction to Introduction to ProgrammingProgramming
Lecture 13Lecture 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
Array Array ManipulatioManipulatio
nn
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
Addressing Array Addressing Array ElementsElements
a [rowIndex ] [ columnIndex ]a [rowIndex ] [ columnIndex ]
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 ] ;
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 ] ;
}}}}
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 ;
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
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
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
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]
Transpose of a Transpose of a MatrixMatrix
998877
665544
332211
Square MatrixSquare Matrix
arraySize = arraySize = rowscols
Number of rows are equal to number of columns
Square MatrixSquare Matrix
a a ijij = a = a jiji
i = rowsi = rows
j = columnsj = columns
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}}
}}
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 ;
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
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
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
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
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.
Interface Interface RequirementsRequirements
Distribution of the Distribution of the ProgramProgram
InputInput Salary calculationSalary calculation Identification of the Identification of the
unluckyunlucky
individualsindividuals OutputOutput
Detail DesignDetail DesignFunctions in the programFunctions in the program
getInput getInput calculateSalarycalculateSalarylocateUnluckyIndividuallocateUnluckyIndividualdisplayOutputdisplayOutput
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 ) ;
}}
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 ] ;
}}
[email protected]@vu.edu.pkk
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