cs201- introduction to programming- 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
Introduction to ProgrammingIntroduction to Programming
Lecture 14Lecture 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 ] ;}}
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 ] ;
}}
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 ] ;
}}
CodeCodeelseelse
{{sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.15 * sal [ i ] sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.15 * sal [ i ] [ 0 ] ;[ 0 ] ;
}}
}}
}}
}}
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 ] ;] ;
}}
... … …... … …
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
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 ;
}}}}
}}}}
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” ;
}}
}}
}}
PointersPointers
PointersPointers
10
x
60000
Location
Address of x
Declaring Pointer Declaring Pointer to Integerto Integer
int *myptr ;int *myptr ;
myptr is pointer to an integermyptr is pointer to an integer
Declaring Declaring PointersPointers
double *x ;double *x ;
char *c ;char *c ;
ExampleExampleint *ptr ;int *ptr ;int x ;int x ;x = 10 ;x = 10 ;ptr = &x ;ptr = &x ;
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”
z = *ptr * 2 ;z = *ptr * 2 ;
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
ExampleExamplemain ( )main ( ){{
int numEmp ;int numEmp ;……..funct ( &numEmp ) ;funct ( &numEmp ) ;……..
}}
void funct ( int *numEmp )void funct ( int *numEmp ){{
cin >> *numEmp ;cin >> *numEmp ;}}
Declaring Declaring pointerspointers
int *ptr1 , *ptr2 , *ptr3 ;int *ptr1 , *ptr2 , *ptr3 ;
Declaring Declaring pointerspointers
int *ptr , x ;int *ptr , x ;
Declaring Declaring pointerspointers
int *ptr , x , a [ 10 ] ;int *ptr , x , a [ 10 ] ;
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
SwappingSwapping
SwapSwap
temp = x ;temp = x ;
x = y ;x = y ;
y = temp ; y = temp ;
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 ) ;
}}
ExampleExampleswap ( int *yptr , int *xptr )swap ( int *yptr , int *xptr )
{{
… … …… … …
}}
constconstint *const myptr = &x ;int *const myptr = &x ;
myptr is a constant pointer to myptr is a constant pointer to an integeran integer
constconst
const int x = 10 const int x = 10 ;;
constconst
const int *myptr = &x const int *myptr = &x ;;
myptr is a pointer to a constant myptr is a pointer to a constant integerinteger
ArrayArrayint int aa [ 10 ] ;[ 10 ] ;
11
22
33
44
55
66
77
88
99
1010
aStarting Address of Array