Introduction to Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Programming

Description:

Introduction to Programming ... Declaring pointers int *ptr , x , a [ 10 ] ; Bubble Sort Swapping Swap temp = x ; x = y ; y = temp ; Example main ( ) ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 31
Provided by: Umer4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
Lecture 14
2
Code
  • calculateSalary ( int sal 2 , int lucky
    , int numEmps )
  • for ( i 0 i lt numEmps i )
  • // netSalary grossSalary tax
  • if ( sal i 0 lt 5000 )
  • sal i 1 sal i 0

3
Code
  • else
  • if ( sal i 0 lt 10000 )
  • sal i 1 sal i 0 - 0.05sal
    i 0

4
Code
  • else
  • if ( sal i 0 lt 20000 )
  • sal I 1 sal I 0 - 0.1 sal
    I 0

5
Code
  • else
  • sal i 1 sal i 0 - 0.15 sal
    i 0

6
  • if ( sal i 0 gt 0 sal i 0 lt
    5000 )
  • sal i 1 sal i 0
  • if ( sal i 0 gt 5000 sal i 0 lt
    10000 )
  • sal i 1 sal i 0 - 0.05 sal
    i 0
  • ...

7
  • if ( grossSalary gt sal i 0 netSalary
    lt sal i 1 )
  • This logic will fail

8
Code
  • void locateUnluckyIndividual ( int sal 2
    , int lucky , int numEmps )
  • int i , j
  • int grossSalary , netSalary
  • for ( i 0 i lt numEmp i )
  • grossSalary sal i 0
  • netSalary sal i 1
  • for ( j 0 j lt numEmp j )
  • if ( grossSalary gt sal j 0
    netSalary lt sal j 1 )
  • lucky i 1

9
Code
  • void displayOutput ( int sal 2 , int
    lucky , int numEmps )
  • for ( i 0 i lt numEmp i )
  • if ( lucky i 1 )
  • coutltlt Employee No. ltlt i1 ltlt is


    unlucky, Gross Salary ltlt sal i 0
    ltlt Net Salary ltlt sal i 1 ltlt \n

10
Pointers
11
Pointers
Location
x
10
60000
Address of x
12
Declaring Pointer to Integer
  • int myptr
  • myptr is pointer to an integer

13
Declaring Pointers
  • double x
  • char c

14
Example
  • int ptr
  • int x
  • x 10
  • ptr x

15
Dereferencing Operator
  • ptr is read as
  • The value of what ever ptr points to

16
  • z ptr 2

17
Initializing Pointers
  • ptr var
  • ptr 0
  • ptr NULL
  • 0 and NULL points to nothing

18
Example
  • main ( )
  • int numEmp
  • .
  • funct ( numEmp )
  • .
  • void funct ( int numEmp )
  • cin gtgt numEmp

19
Declaring pointers
  • int ptr1 , ptr2 , ptr3

20
Declaring pointers
  • int ptr , x

21
Declaring pointers
  • int ptr , x , a 10

22
Bubble Sort
5
1
3
6
9
2
4
8
1
5
3
6
2
9
4
8
23
Swapping
24
Swap
  • temp x
  • x y
  • y temp

25
Example
  • main ( )
  • int x 10 , y 20 , yptr , xptr
  • yptr y
  • xptr x
  • swap ( yptr , xptr )

26
Example
  • swap ( int yptr , int xptr )

27
const
  • int const myptr x
  • myptr is a constant pointer to an integer

28
const
  • const int x 10

29
const
  • const int myptr x
  • myptr is a pointer to a constant integer

30
Array
  • int a 10

a
1
2
3
4
5
6
7
8
9
10
Starting Address of Array
Write a Comment
User Comments (0)
About PowerShow.com