Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 8 Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 8 Arrays and Strings
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Arrays Chapter 8.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C-Strings, Arrays, and String Class Review.
Lecture: Arrays. 7.1 Arrays Hold Multiple Values.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
+ Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 8: Arrays by Tony.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Copyright 2003 Scott/Jones Publishing Alternate Version of Starting Out with C++, Third Edition Chapter 8 Arrays.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 7: Arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
ARRAYS (C) KHAERONI, M.SI. OVERVIEW Introduction to Arrays Arrays in Functions Programming with Arrays Multidimensional Arrays.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Alternate Version of Starting Out with C++, Third Edition
Chapter 7: Arrays.
Standard Version of Starting Out with C++, 4th Edition
Chapter 7: Arrays.
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
New Structure Recall “average.cpp” program
Chapter 7: Arrays.
7 Chapter Arrays.
Chapter 8: Arrays Starting Out with C++ Early Objects Eighth Edition
Standard Version of Starting Out with C++, 4th Edition
Standard Version of Starting Out with C++, 4th Edition
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Presentation transcript:

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays

Chapter 8 slide 2 Topics 8.1 Arrays Hold Multiple Values 8.2 Accessing Array Elements 8.3 Inputting and Displaying Array Contents 8.4 Array Initialization 8.5 Processing Array Contents 8.7 The typedef Statement

Chapter 8 slide 3 Topics (continued) 8.8 Arrays as Function Arguments 8.9 Two-Dimensional Arrays 8.10 Arrays with Three or More Dimensions 8.14 Arrays of Class Objects

Chapter 8 slide Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations Declared using [] operator int tests[5];

Chapter 8 slide 5 Array Storage in Memory The definition int tests[5]; allocates the following memory 0 First element

Chapter 8 slide 6 Array Terminology In the definition int tests[5]; int is the data type of the array elements tests is the name of the array 5 is the size declarator. It shows the number of elements in the array.

Chapter 8 slide 7 Array Terminology Examples Examples: int tests[5]; // holds 5 ints; double volumes[10]; // holds 10 doubles

Chapter 8 slide Accessing Array Elements Each array element has a subscript, used to access the element. Subscripts start at 0 subscripts

Chapter 8 slide 9 Accessing Array Elements Array elements (accessed by array name and subscript) can be used as regular variables tests[0] = 79; cout << tests[0]; cin >> tests[1]; tests[4] = tests[0] + tests[1]; cout << tests; // illegal due to // missing subscript tests

Chapter 8 slide 10 Array Subscripts Array subscript can be an integer constant, integer variable, or integer expression Examples: Subscript is cin >> tests[3]; int constant cout << tests[i]; int variable cout << tests[i+j]; int expression

Chapter 8 slide Inputting and Displaying Array Contents To access a single element of an array, use a subscript (as previously shown) int tests[5]; // Define 5-element array cout << “Enter first test score ”; cin >> tests[0];

Chapter 8 slide 12 Inputting and Displaying All Array Elements To access each element of an array –Use a loop –Let the loop control variable be the array subscript –A different array element will be referenced each time through the loop for (i = 0; i < 5; i++) cout << tests[i] << endl;

Chapter 8 slide 13 Getting Array Data from a File int sales[5]; datafile.open(filename); if (!datafile) cout << "Error opening data file\n"; else { // Input daily sales for (int day = 0; day < 5; day++) datafile >> sales[day]; datafile.close(); }

Chapter 8 slide 14 No Bounds Checking There are no checks in C++ that an array subscript is in range An invalid array subscript can cause program to overwrite other memory Example: int i = 4; int num[3]; num[i] = 25; num [0] [1] [2] 25

Chapter 8 slide Processing Array Contents Array elements can be –treated as ordinary variables of the same type as the array –used in arithmetic operations, in relational expressions, etc. Example: if (principalAmt[3] >= 10000) interest = principalAmt[3] * intRate1; else interest = principalAmt[3] * intRate2;

Chapter 8 slide 16 Sum of Array Elements Use a simple loop to add together array elements float average, sum = 0; for (int tnum = 0; tnum < 5; tnum++) sum += tests[tnum]; Once summed, average can be computed average = sum/5;

Chapter 8 slide 17 Largest Array Element Use a loop to examine each element and find the largest element (i.e., one with the largest value) int largest = tests[0]; for (int tnum = 1; tnum < 5; tnum++) { if (tests[tnum] > largest) largest = tests[tnum]; } cout << “Highest score is ” << largest; A similar algorithm exists to find the smallest element

Chapter 8 slide The typedef Statement Creates an alias for a simple or structured data type Format: typedef existingType newName; Example: typedef unsigned int Uint; Uint tests[5]; // array of // unsigned ints

Chapter 8 slide 19 Uses of typedef Used to make code more readable Can be used to create alias for array of a particular type // Define yearArray as a data type // that is an array of 12 ints typedef int yearArray[12]; // Create two of these arrays yearArray highTemps, lowTemps;

Chapter 8 slide Arrays as Function Arguments Can use typedef to simplify function prototype and heading // Make intArray an integer array // of unspecified size typedef int intArray[]; // Function prototype void showScores(intArray, int); // Function header void showScores(intArray tests, int size)

Chapter 8 slide 21 Passing an Array Element Passing a single array element to a function is no different than passing a regular variable of that data type Function does not need to know the value it receives is coming from an array displayValue(score[i]); // call void displayValue(int item) // header { cout << item << endl; }

Chapter 8 slide 22 Modifying Arrays in Functions Array parameters in functions are similar to reference variables Changes made to array in a function are made to the actual array in the calling function Must be careful that an array is not inadvertently changed by a function

Chapter 8 slide Two-Dimensional Arrays Can define one array for multiple sets of data Like a table in a spreadsheet Use two size declarators in definition int exams[4][3]; Number of rows Number of cols

Chapter 8 slide 24 Two-Dimensional Array Representation int exams[4][3]; Use two subscripts to access element exams[2][2] = 86; exams[0][0]exams[0][1]exams[0][2] exams[1][0]exams[1][1]exams[1][2] exams[2][0]exams[2][1]exams[2][2] exams[3][0]exams[3][1]exams[3][2] columns rowsrows

Chapter 8 slide 25 Using typedef with a Two-Dimensional Array Can use typedef for simpler notation typedef int intExams[][2];... // Function prototype void getExams(intExams, int); // Function header void getExams(intExams exams, int rows)

Chapter 8 slide Arrays of Class Objects Class objects can also be used as array elements class Square { private: int side; public: Square(int s = 1) { side = s; } int getSide() { return side; } }; Square shapes[10]; // Create array of 10 // Square objects

Chapter 8 slide 27 Arrays of Class Objects Use subscript to access a specific object in the array Then use dot operator to access members of that object for (i = 0; i < 10; i++) cout << shapes[i].getSide() << endl;