© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Advertisements

Combinations Examples
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Probability Key. You are given a standard deck of 52 playing cards, which consists of 4 suits: diamonds, hearts, clubs, and spades. Each suit has 13 cards.
2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. GÖSTERİCİLER ( Pointers ) 7.1Giriş Göstericiler –Güçlü.
Whiteboardmaths.com © 2004 All rights reserved
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 12.2 Theoretical Probability
CPT: Ptr+Str/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Two Way Tables Venn Diagrams Probability. Learning Targets 1. I can use a Venn diagram to model a chance process involving two events. 2. I can use the.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2000 Deitel & Associates, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf 1 5.1Introduction Pointers –Powerful, but difficult to master.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
1 CSC 211 Data Structures Lecture 5 Dr. Iftikhar Azim Niaz 1.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 5 - Pointers and Strings
Structure, Unions, typedef and enumeration
Chapter 7 - Pointers Outline 7.1 Introduction
File Processing (Cont.) and Structures
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 7 from “C How to Program"
Chapter 5 - Pointers and Strings
Introduction to Programming
Chapter 5 - Pointers and Strings
7 C Pointers.
Chapter 5 - Pointers and Strings
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For example: an array of strings char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; –Strings are pointers to the first character –char * – each element of suit is a pointer to a char –The strings are not actually stored in the array suit, only pointers to the strings are stored –suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]Hearts \0 Diamonds Clubs Spades

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.10Case Study: A Card Shuffling and Dealing Simulation Card shuffling program –Use array of pointers to strings –Use double scripted array (suit, face) –The numbers 1-52 go into the array Representing the order in which the cards are dealt deck[ 2 ][ 12 ] represents the King of Clubs Hearts Diamonds Clubs Spades AceTwoThreeFourFiveSixSevenEightNineTenJackQueenKing Clubs King

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.10Case Study: A Card Shuffling and Dealing Simulation Pseudocode –Top level: Shuffle and deal 52 cards –First refinement: Initialize the suit array Initialize the face array Initialize the deck array Shuffle the deck Deal 52 cards

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.10Case Study: A Card Shuffling and Dealing Simulation –Second refinement Convert shuffle the deck to For each of the 52 cards Place card number in randomly selected unoccupied slot of deck Convert deal 52 cards to For each of the 52 cards Find card number in deck array and print face and suit of card

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.10Case Study: A Card Shuffling and Dealing Simulation –Third refinement Convert shuffle the deck to Choose slot of deck randomly While chosen slot of deck has been previously chosen Choose slot of deck randomly Place card number in chosen slot of deck Convert deal 52 cards to For each slot of the deck array If slot contains card number Print the face and suit of the card

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_24.c (Part 1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_24.c (Part 2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_24.c (Part 3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_24.c (Part 4 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Nine of Hearts Five of Clubs Queen of Spades Three of Spades Queen of Hearts Ace of Clubs King of Hearts Six of Spades Jack of Diamonds Five of Spades Seven of Hearts King of Clubs Three of Clubs Eight of Hearts Three of Diamonds Four of Diamonds Queen of Diamonds Five of Diamonds Six of Diamonds Five of Hearts Ace of Spades Six of Hearts Nine of Diamonds Queen of Clubs Eight of Spades Nine of Clubs Deuce of Clubs Six of Clubs Deuce of Spades Jack of Clubs Four of Clubs Eight of Clubs Four of Spades Seven of Spades Seven of Diamonds Seven of Clubs King of Spades Ten of Diamonds Jack of Hearts Ace of Hearts Jack of Spades Ten of Clubs Eight of Diamonds Deuce of Diamonds Ace of Diamonds Nine of Spades Four of Hearts Deuce of Hearts King of Diamonds Ten of Spades Three of Hearts Ten of Hearts

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.11Pointers to Functions Pointer to function –Contains address of function –Similar to how array name is address of first element –Function name is starting address of code that defines function Function pointers can be –Passed to functions –Stored in arrays –Assigned to other function pointers

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Pointers to Functions Example: bubblesort –Function bubble takes a function pointer bubble calls this helper function this determines ascending or descending sorting –The argument in bubblesort for the function pointer: int ( *compare )( int a, int b ) tells bubblesort to expect a pointer to a function that takes two ints and returns an int –If the parentheses were left out: int *compare( int a, int b ) Defines a function that receives two integers and returns a pointer to a int

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_26.c (Part 1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_26.c (Part 2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_26.c (Part 3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig07_26.c (Part 4 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order Data items in ascending order Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order Data items in descending order

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3Pointer Operators