CS201-14 - Introduction to Programming Introduction to...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Introduction to Programming Lecture 14 Code Code calculateSalary ( int sal [ ] [ 2 ] , int lucky [ ] , int numEmps ) { for ( i = 0 ; i < numEmps ; i ++ ) { // netSalary = grossSalary – tax if ( sal [ i ] [ 0 ] <= 5000 ) { sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ; } Code Code else { if ( sal [ i ] [ 0 ] <= 10000 ) { sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ­ 0.05*sal [ i ] [ 0 ] ; } Code Code else { if ( sal [ i ] [ 0 ] <= 20000 ) { } sal [ I ] [ 1 ] = sal [ I ] [ 0 ] ­ 0.1 * sal [ I ] [ 0 ] ; Code Code else { } sal [ i ] [ 1 ] = sal [ i ] [ 0 ] ­ 0.15 * sal [ i ] [ 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 ] ; } if ( sal [ i ] [ 0 ] > 5000 && sal [ i ] [ 0 ] < 10000 ) { 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 fail void locateUnluckyIndividual ( int sal [ ] [ 2 ] , int lucky [ ] , int numEmps ) { int i , j ; int grossSalary , netSalary ; for ( i = 0 ; i < numEmp ; i ++ ) { grossSalary = sal [ i ] [ 0 ] ; netSalary = sal [ i ] [ 1 ] ; for ( j = 0 ; j < numEmp ; j ++ ) { if ( grossSalary > sal [ j ] [ 0 ] && netSalary < sal [ j ] [ 1 ] ) { lucky [ i ] = 1 ; } } Code Code } } Code Code void displayOutput ( int sal [ ] [ 2 ] , int lucky [ ] , int numEmps ) { for ( i = 0 ; i < numEmp ; i ++ ) { if ( lucky [ i ] == 1 ) { cout<< “Employee No.” << i+1 << “ is unlucky, Gross Salary = ” << sal [ i ] [ 0 ] << “ Net Salary = ” << sal [ i ] [ 1 ] << “\n” ; } } } Pointers Pointers Pointers Pointers Location x 60000 Address of x 10 Declaring Pointer to Declaring Pointer to Integer int *myptr ; myptr is pointer to an integer Declaring Pointers Declaring Pointers double *x ; char *c ; Example Example int *ptr ; int x ; x = 10 ; ptr = &x ; Dereferencing Dereferencing Operator * *ptr is read as “The value of what ever ptr points to” z = *ptr * 2 ; z = *ptr * 2 ; Initializing Pointers Initializing Pointers ptr = &var ; ptr = 0 ; ptr = NULL ; 0 and NULL points to nothing main ( ) { int numEmp ; …. funct ( &numEmp ) ; …. } Example Example void funct ( int *numEmp ) { cin >> *numEmp ; } Declaring pointers Declaring pointers int *ptr1 , *ptr2 , *ptr3 ; Declaring pointers Declaring pointers int *ptr , x ; Declaring pointers Declaring pointers int *ptr , x , a [ 10 ] ; Bubble Sort Bubble Sort 5 1 3 6 9 2 4 8 1 5 3 6 2 9 4 8 1 3 5 6 2 4 8 9 1 2 3 4 5 6 8 9 Swapping Swapping Swap Swap temp = x ; x = y ; y = temp ; Example Example main ( ) { } int x = 10 , y = 20 , * yptr , * xptr ; yptr = &y ; xptr = &x ; swap ( yptr , xptr ) ; Example Example swap ( int *yptr , int *xptr ) { … … … } int *const myptr = &x ; myptr is a constant pointer to an integer const const const const const int x = 10 ; const const const int *myptr = &x ; myptr is a pointer to a constant integer Array Array int a [ 10 ] ; Starting Address of Array a 1 2 3 4 5 6 7 8 9 10 ...
View Full Document

Ask a homework question - tutors are online