CS201- Introduction to Programming- Lecture 14

30
Introduction to Programming Introduction to Programming Lecture 14 Lecture 14

description

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

Transcript of CS201- Introduction to Programming- Lecture 14

Page 1: CS201- Introduction to Programming- Lecture 14

Introduction to ProgrammingIntroduction to Programming

Lecture 14Lecture 14

Page 2: CS201- Introduction to Programming- Lecture 14

CodeCodecalculateSalary ( int sal [ ] [ 2 ] , int lucky [ ] , int numEmps )calculateSalary ( int sal [ ] [ 2 ] , int lucky [ ] , int numEmps ){{

for ( i = 0 ; i < numEmps ; i ++ )for ( i = 0 ; i < numEmps ; i ++ ){{// netSalary = grossSalary – tax// netSalary = grossSalary – taxif ( sal [ i ] [ 0 ] <= 5000 )if ( sal [ i ] [ 0 ] <= 5000 ){{sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ;sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ;}}

Page 3: CS201- Introduction to Programming- Lecture 14

CodeCodeelseelse

{{

if ( sal [ i ] [ 0 ] <= 10000 )if ( sal [ i ] [ 0 ] <= 10000 )

{{sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - sal [ i ] [ 1 ] = sal [ i ] [ 0 ] -

0.05*sal [ i ] [ 0 ] ;0.05*sal [ i ] [ 0 ] ;

}}

Page 4: CS201- Introduction to Programming- Lecture 14

CodeCodeelseelse

{{

if ( sal [ i ] [ 0 ] <= 20000 )if ( sal [ i ] [ 0 ] <= 20000 )

{{

sal [ I ] [ 1 ] = sal [ I ] [ 0 ] sal [ I ] [ 1 ] = sal [ I ] [ 0 ] - 0.1 * sal [ I ] [ 0 ] ;- 0.1 * sal [ I ] [ 0 ] ;

}}

Page 5: CS201- Introduction to Programming- Lecture 14

CodeCodeelseelse

{{sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.15 * sal [ i ] sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.15 * sal [ i ] [ 0 ] ;[ 0 ] ;

}}

}}

}}

}}

Page 6: CS201- Introduction to Programming- Lecture 14

if ( sal [ i ] [ 0 ] >= 0 && sal [ i ] [ 0 ] <= 5000 )if ( sal [ i ] [ 0 ] >= 0 && sal [ i ] [ 0 ] <= 5000 )

{{

sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ;sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ;

}}

if ( sal [ i ] [ 0 ] > 5000 && sal [ i ] [ 0 ] < 10000 )if ( sal [ i ] [ 0 ] > 5000 && sal [ i ] [ 0 ] < 10000 )

{{

sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.05 * sal [ i ] [ 0 sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.05 * sal [ i ] [ 0 ] ;] ;

}}

... … …... … …

Page 7: CS201- Introduction to Programming- Lecture 14

if ( grossSalary > sal [ i ] [ 0 ] && netSalary < sal [ i ] [ 1 ] )if ( grossSalary > sal [ i ] [ 0 ] && netSalary < sal [ i ] [ 1 ] )

This logic will failThis logic will fail

Page 8: CS201- Introduction to Programming- Lecture 14

CodeCodevoid locateUnluckyIndividual ( int sal [ ] [ 2 ] , int lucky [ ] , int void locateUnluckyIndividual ( int sal [ ] [ 2 ] , int lucky [ ] , int

numEmps )numEmps ){ {

int i , j ;int i , j ;int grossSalary , netSalary ;int grossSalary , netSalary ;

for ( i = 0 ; i < numEmp ; i ++ )for ( i = 0 ; i < numEmp ; i ++ ) {{

grossSalary = sal [ i ] [ 0 ] ;grossSalary = sal [ i ] [ 0 ] ; netSalary = sal [ i ] [ 1 ] ;netSalary = sal [ i ] [ 1 ] ;

for ( j = 0 ; j < numEmp ; j ++ )for ( j = 0 ; j < numEmp ; j ++ ){{

if ( grossSalary > sal [ j ] [ 0 ] && netSalary < sal [ j ] if ( grossSalary > sal [ j ] [ 0 ] && netSalary < sal [ j ] [ 1 ] )[ 1 ] )

{{lucky [ i ] = 1 ;lucky [ i ] = 1 ;

}}}}

}}}}

Page 9: CS201- Introduction to Programming- Lecture 14

CodeCodevoid displayOutput ( int sal [ ] [ 2 ] , int lucky [ ] , int void displayOutput ( int sal [ ] [ 2 ] , int lucky [ ] , int numEmps )numEmps )

{{

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

{{

if ( lucky [ i ] == 1 )if ( lucky [ i ] == 1 )

{{

cout<< “Employee No.” << i+1 << “ cout<< “Employee No.” << i+1 << “ is is unlucky, Gross Salary = ” << sal [ i ] [ unlucky, Gross Salary = ” << sal [ i ] [ 0 ] 0 ] << “ Net Salary = ” << sal [ i ] [ 1 ] << “ Net Salary = ” << sal [ i ] [ 1 ] << “\n” ;<< “\n” ;

}}

}}

}}

Page 10: CS201- Introduction to Programming- Lecture 14

PointersPointers

Page 11: CS201- Introduction to Programming- Lecture 14

PointersPointers

10

x

60000

Location

Address of x

Page 12: CS201- Introduction to Programming- Lecture 14

Declaring Pointer Declaring Pointer to Integerto Integer

int *myptr ;int *myptr ;

myptr is pointer to an integermyptr is pointer to an integer

Page 13: CS201- Introduction to Programming- Lecture 14

Declaring Declaring PointersPointers

double *x ;double *x ;

char *c ;char *c ;

Page 14: CS201- Introduction to Programming- Lecture 14

ExampleExampleint *ptr ;int *ptr ;int x ;int x ;x = 10 ;x = 10 ;ptr = &x ;ptr = &x ;

Page 15: CS201- Introduction to Programming- Lecture 14

Dereferencing Dereferencing Operator *Operator *

*ptr is read as *ptr is read as ““The value of what ever ptr points to”The value of what ever ptr points to”

Page 16: CS201- Introduction to Programming- Lecture 14

z = *ptr * 2 ;z = *ptr * 2 ;

Page 17: CS201- Introduction to Programming- Lecture 14

Initializing Initializing PointersPointers

ptr = &var ;ptr = &var ;

ptr = 0 ;ptr = 0 ;

ptr = NULL ; ptr = NULL ;

0 and NULL points to nothing0 and NULL points to nothing

Page 18: CS201- Introduction to Programming- Lecture 14

ExampleExamplemain ( )main ( ){{

int numEmp ;int numEmp ;……..funct ( &numEmp ) ;funct ( &numEmp ) ;……..

}}

void funct ( int *numEmp )void funct ( int *numEmp ){{

cin >> *numEmp ;cin >> *numEmp ;}}

Page 19: CS201- Introduction to Programming- Lecture 14

Declaring Declaring pointerspointers

int *ptr1 , *ptr2 , *ptr3 ;int *ptr1 , *ptr2 , *ptr3 ;

Page 20: CS201- Introduction to Programming- Lecture 14

Declaring Declaring pointerspointers

int *ptr , x ;int *ptr , x ;

Page 21: CS201- Introduction to Programming- Lecture 14

Declaring Declaring pointerspointers

int *ptr , x , a [ 10 ] ;int *ptr , x , a [ 10 ] ;

Page 22: CS201- Introduction to Programming- Lecture 14

Bubble SortBubble Sort55

11

33

66

99

22

44

88

11

55

33

66

44

88

22

99

3

1

6

5

4

2

9

8

4

3

2

1

6

5

9

8

Page 23: CS201- Introduction to Programming- Lecture 14

SwappingSwapping

Page 24: CS201- Introduction to Programming- Lecture 14

SwapSwap

temp = x ;temp = x ;

x = y ;x = y ;

y = temp ; y = temp ;

Page 25: CS201- Introduction to Programming- Lecture 14

ExampleExamplemain ( )main ( )

{{

int x = 10 , y = 20 , * yptr , * int x = 10 , y = 20 , * yptr , * xptr ;xptr ;

yptr = &y ;yptr = &y ;

xptr = &x ;xptr = &x ;

swap ( yptr , xptr ) ;swap ( yptr , xptr ) ;

}}

Page 26: CS201- Introduction to Programming- Lecture 14

ExampleExampleswap ( int *yptr , int *xptr )swap ( int *yptr , int *xptr )

{{

… … …… … …

}}

Page 27: CS201- Introduction to Programming- Lecture 14

constconstint *const myptr = &x ;int *const myptr = &x ;

myptr is a constant pointer to myptr is a constant pointer to an integeran integer

Page 28: CS201- Introduction to Programming- Lecture 14

constconst

const int x = 10 const int x = 10 ;;

Page 29: CS201- Introduction to Programming- Lecture 14

constconst

const int *myptr = &x const int *myptr = &x ;;

myptr is a pointer to a constant myptr is a pointer to a constant integerinteger

Page 30: CS201- Introduction to Programming- Lecture 14

ArrayArrayint int aa [ 10 ] ;[ 10 ] ;

11

22

33

44

55

66

77

88

99

1010

aStarting Address of Array