Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Week 7 - Monday.  What did we talk about last time?  Pointers to pointers  Lab 6.
Debugging Introduction to Computing Science and Programming I.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Chapter 10.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Introduction to Computer Science I.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Program Input and the Software Design Process ROBERT REAVES.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
General Programming Introduction to Computing Science and Programming I.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Chapter 8 Arrays and Strings
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
Problem Solving for Programming Session 8 Static Data Structures.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Files Tutor: You will need ….
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 15 Strings as Character Arrays
Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart.
Programming Fundamentals. Today’s Lecture Multidimensional Arrays Arrays as Class Member Data Arrays of Objects C-Strings.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Exception Handling How to handle the runtime errors.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Introduction to Computing Science and Programming I
Learning to Program D is for Digital.
Programming Fundamentals
Arrays Arrays exist in almost every computer language.
Getting Started with C.
Popping Items Off a Stack Lesson xx
Introduction to Primitive Data types
String What it is Why it’s useful
Selection Statements Chapter 3.
Chapter 1 c++ structure C++ Input / Output
Introduction to Primitive Data types
Presentation transcript:

Chapter 7 Continued Arrays & Strings

Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this, “partaray.cpp”.

Arrays as Class Member Data Arrays can be used as data items in classes. We will look at an example that models a common computer data structure: the stack.

Arrays as Class Member Data A stack works like the spring loaded devices that hold trays in cafeterias. When you put the tray on the top, the stack sinks down a little; when you take a tray off, it pops up. The last tray placed on the stack is always the first tray removed.

Arrays as Class Member Data Hardware and Software stacks offer a useful storage device in certain programming situations. First in last out (FILO) data structure. Queue is a first in first out (FIFO) data structure. Our next example, “stakaray.cpp” creates a simple stack class.

Arrays of Objects We can also create an array of objects. Let’s go back to our example of distances, this time an array of such objects, “englaray.cpp”.

Array Bounds In our last example, the program uses a do loop to get input from the user. This way the user can input data for as many structures of type part, up to MAX, the size of the array. Even though it is hard to imagine anyone entering over 100 distances, what would happen if they did?

Array Bounds If the user was to enter more than 100, something unpredictable and bad will happen. There is no bounds checking. If the user enters too much data for an array to hold, neither the compiler or runtime system will complain.

Array Bounds This extra data makes the array “too full” and in turn will probably write over the top of other data or the program code itself. This may cause the program to do strange things and even crash the system completely.

Array Bounds In the end it is up to the programmer to deal with array bounds. If it seems that the user will input too much data, the programmer should warn the user that the array is full. This can be accomplished with a simple if statement.

Accessing Objects in an Array We define an array of objects like this: Distance dist[MAX]; Here the data type of the dist array is Distance, and it has MAX elements. We access an array element (an object in the array) like this: dist[j].showdist();

Array of Cards Our next example shows an array of objects. This program will simulate a deck of cards. Let’s look at “cardaray.cpp”.

Graphics Characters There are several graphics characters in the range below ASCII code 32. In the display() member function of card we used codes 5, 4, 3, and 6 to access the characters for a club, diamond, a heart, and a spade.

Graphics Characters Casting these numbers to type char causes the << operator to print them as characters rather than as numbers. static_cast (5)

The Card Deck The array of structures that make up the deck of cards is defined in the statement: card deck[52]; This statement creates an array called deck, consisting of 52 objects of type card. To display the jth card in the deck, we call the display() member function: deck[j].display();

C-Strings There are 2 kinds of strings commonly used in C++. These 2 types are C-strings and strings that are objects of the string class. First we will look at C-strings, these are arrays of type char. We call these C-strings because they were the only kind of strings available in the C language.

C-Strings Even though strings created with the string class is the best way to represent a string in C++, we will study them because there are many C library functions in use today, and because the are more primitive and easier to understand. For years to come C style strings will show up in old code.

C-String Variables Just like other data types, strings can be variables or constants. First let’s look at an example that defines a single string variable, asks the user to enter a string, and places this string in the string variable. Let’s look at “stringin.cpp”.

C-String Variables Each character occupies 1 byte of memory. An important part of C-strings is that they must terminate with a byte containing 0. This is represented by the character constant ‘\0’, which is the ASCII value of 0. This terminating zero is called the null character. When the << operator displays a string, it continues until it finds a null character.

Avoiding Buffer Overflow In our last example the user inputted a string. What happens if the user enters a string that is longer than the array used to hold it? There is no built in mechanism in C++ to keep a over active typist to keep typing “off the array”, which would end up crashing the system.

Avoiding Buffer Overflow Thankfully there is a way to tell the >> operator to limit the number of characters it places in an array. Let’s look at “safetyin.cpp” for a way to do this.

String Constants You can initialize a string to a constant value when you define it. Let’s look at an example that does this, “strinit.cpp”.

Reading Embedded Blanks If you tried our last program with strings that contained more than one word, you would have printed only one word of your sentence. It turns out that the >> operator considers a space to be a terminating character. So it will read strings consisting of a single word, but anything typed after a space is thrown away.

Reading Embedded Blanks To read text containing blanks we use another function, cin.get(). This syntax means a member function get() of the stream class of which cin is an object. We will look at an example of how it’s used, “blanksin.cpp”.

Reading Multiple Lines Now we have solved the problem of reading strings with embedded blanks, but what about strings with multiple lines? There is a third argument in the cin::get() function that can help us out in this. This 3 rd argument specifies the character that tells the function to stop reading.

Reading Multiple Lines The default value for this argument is the newline (‘\n’) character, but if you call the function with some other character for this argument, the default will be overridden by the specified character. Let’s look at an example of this, “linesin.cpp”.

Copying a String the Hard Way The best way to understand the true nature of strings is to deal with them character by character. The next program does this, “strcopy1.cpp”.

Copying a String the Easy Way Of course you don’t have to copy strings that hard way, there is a built in easy way to do it. Let’s look at this easy way, “strcopy2.cpp”.

Arrays of Strings If there are arrays of arrays then of course there are arrays of strings. In our next example we will see this, “straray.cpp”.