Introduction to Programming

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Introduction to Programming Lecture 8. String Handling  Character is the building block of strings.  Characters are represented inside the computer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Introduction to Programming
Computer Skills2 for Scientific Colleges
Chapter 7 - Pointers Outline 7.1 Introduction
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Programming Fundamental
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Object-Oriented Programming Using C++
Chapter 7 from “C How to Program"
Programming fundamentals 2 Chapter 2:Pointer
Pointer Basics Psst… over there.
Chapter 9: Pointers.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
CS 2308 Exam I Review.
CS 2308 Exam I Review.
Introduction to Programming
Pointers Call-by-Reference CSCI 230
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Introduction to Problem Solving and Programming
5.1 Introduction Pointers Powerful, but difficult to master
C++ Pointers and Strings
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
C++ Programming Lecture 18 Pointers – Part II
Pointers and dynamic objects
Pointer Basics Psst… over there.
CISC181 Introduction to Computer Science Dr
C++ Pointers and Strings
Programming fundamentals 2 Chapter 3:Pointer
C Pointers Another ref:
Presentation transcript:

Introduction to Programming Lecture 14

Code { for ( i = 0 ; i < numEmps ; i ++ ) 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 { if ( sal [ i ] [ 0 ] <= 10000 ) else { if ( sal [ i ] [ 0 ] <= 10000 ) sal [ i ] [ 1 ] = sal [ i ] [ 0 ] - 0.05*sal [ i ] [ 0 ] ; }

Code else { if ( sal [ i ] [ 0 ] <= 20000 ) sal [ I ] [ 1 ] = sal [ I ] [ 0 ] - 0.1 * sal [ I ] [ 0 ] ; }

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

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 ] ) This logic will fail

Code 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 { for ( i = 0 ; i < numEmp ; i ++ ) if ( lucky [ i ] == 1 ) 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 Location x 60000 10 Address of x

Declaring Pointer to Integer int *myptr ; myptr is pointer to an integer

Declaring Pointers double *x ; char *c ;

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

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

z = *ptr * 2 ;

Initializing Pointers ptr = &var ; ptr = 0 ; ptr = NULL ; 0 and NULL points to nothing

Example main ( ) { int numEmp ; …. funct ( &numEmp ) ; } void funct ( int *numEmp ) cin >> *numEmp ;

Declaring pointers int *ptr1 , *ptr2 , *ptr3 ;

Declaring pointers int *ptr , x ;

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

Bubble Sort 5 1 3 6 9 2 4 8 1 5 3 1 6 5 4 3 2 1 6 5 9 8 3 6 2 9 4 2 9 8 4 8

Swapping

Swap temp = x ; x = y ; y = temp ;

Example int x = 10 , y = 20 , * yptr , * xptr ; yptr = &y ; main ( ) { int x = 10 , y = 20 , * yptr , * xptr ; yptr = &y ; xptr = &x ; swap ( yptr , xptr ) ; }

Example swap ( int *yptr , int *xptr ) { … … … }

myptr is a constant pointer to an integer int *const myptr = &x ; myptr is a constant pointer to an integer

const const int x = 10 ;

const const int *myptr = &x ; myptr is a pointer to a constant integer

Array int a [ 10 ] ; a Starting Address of Array 1 2 3 4 5 6 7 8 9 10