Introduction to Programming

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

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.
2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
Introduction to Programming Lecture 39. Copy Constructor.
© 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.
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.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
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[]
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
 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.
Introduction to Programming Lecture 8. String Handling  Character is the building block of strings.  Characters are represented inside the computer.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
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.
CLICK THE NUMBERS IN SEQUENCE
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Skip Counting Practice
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 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.
Things that Come in Arrays
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 5 - Pointers and Strings
Structure, Unions, typedef and enumeration
11/7/16 Entry 173 – Objective I will review and continue developing my understanding of uniform probability models, including the complement of an event.
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)
Multi-dimensional Array
Chapter 5 - Pointers and Strings
Command Line Arguments
NUMBERS one two three four five six seven eight
Chapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings
Probability Problems Solved with
Task 2 Implementation help
Section 12.2 Theoretical Probability
CS250 Introduction to Computer Science II
CLICK THE NUMBERS IN SEQUENCE
Section 12.2 Theoretical Probability
Pointers and dynamic objects
Chapter 5 - Pointers and Strings
Homework Due Friday.
CLICK THE NUMBERS IN SEQUENCE
Section 12.2 Theoretical Probability
Presentation transcript:

Introduction to Programming Lecture 16

In Today Lecture Conclude the last discussion Multi-dimensional Arrays Pointers to Pointers

Example 1 char myName [ ] = “Amir” ; char *myNamePtr = “Amir” ;

Multi-dimensional Arrays char multi [ 5 ] [ 10 ] ;

Multi-dimensional Array in Memory Placed sequentially in the memory [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] 1 2 3 4 10 7 9 11 14 10 17 25 39 45 58 1st row 1st col 2nd row 1st col 3rd row 1st col

Dereferencing array element multi [ 2 ] [ 3 ]

*multi ?

Example 2 #include<iostream.h> main ( ) { char multi [ 5 ] [ 10 ] ; cout << multi << endl ; cout << *multi ; cout << **multi ; }

multi + 3 *( multi + 3 ) *( multi + 3 ) + 3 *(*( multi + 3 ) + 3 )

Example 3 main ( ) { int multi [ 5 ] [ 6 ] ; int row , col ; int *ptr ; ptr = *multi ; for ( row = 0 ; row < 5 ; row ++ ) for ( col = 0 ; col < 10 ; col ++ ) multi [ row ] [ col ] = row * col ; }

Example 3 for ( row = 0 ; row < 5 ; row ++ ) { for ( col = 0 ; col < 10 ; col ++) cout << *( ptr ++ ) << “ ”; } cout << endl ;

Pointer to a Pointer

Array of Pointers

myArray is an array of 10 pointer to character Array of Pointers char *myArray [ 10 ] ; myArray is an array of 10 pointer to character

char *myArray [ ] = { “Amir ” , “ Jahangir ” } ; Initialization char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;

Storing Pointers in Array of Pointers int *p1 , *p2 , *p3 ; int *b [ 3 ] ; b [ 0 ] = p1 ; b [ 1 ] = p2 ; b [ 2 ] = p3 ;

Command Line Arguments argc argv ‘argc’ stands for a count of the number of arguments ‘argv’ stands for a vector of arguments

Example 4 main ( int argc , char **argv ) { cout << argc << "\n“ << *argv ; }

Example 5 main ( ) { const char *suit [ 4 ]= { "Spades“ , "Hearts“ , "Diamonds“ , "Clubs“ } ; const char *face [ 13 ] = { "Ace“ , "Deuce“ , "Three“ , "Four", "Five“ , "Six“ , "Seven“ , "Eight“ , "Nine“ , "Ten“ , "Jack“ , "Queen“ , "King" } ; int deck [ 4 ] [ 13 ] = { 0 } ; srand ( time ( 0 ) ) ; shuffle ( deck ) ; deal ( deck , face , suit ) ; }

Shuffle Functions void shuffle ( int wDeck [ ] [ 13 ] ) { int row , column , card ; for ( card = 1 ; card <= 52 ; card ++ ) do row = rand ( ) % 4 ; column = rand ( ) % 13 ; } while( wDeck[ row ]| column ] != 0 ) ; wDeck [ row ] [ column ] = card ;

Deal Function void deal ( const int wDeck [ ] [ 13 ] , const char *wFace [ ] , const char *wSuit [ ] ) { int card , row , column ; for ( card = 1 ; card <= 52 ; card ++ ) for ( row = 0 ; row <= 3 ; row++ ) for ( column = 0 ; column <= 12 ; column ++ ) if ( wDeck [ row ] [ column ] == card ) // Print the face and suit of the card // break out of loops }

What we have done today Multi-dimensional Arrays Pointers to Pointers Arrays of Pointers Command Line Arguments Comprehensive Example