Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Advertisements

CHAPTER 10 ARRAYS II Applications and Extensions.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
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.
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.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Chapter 8 Arrays and Strings
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.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 6 Arrays in C++ 2nd Semester King Saud University
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
Computer Programming BCT 1113
Chapter 7: Introduction to Classes and Objects
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
EKT120 : Computer Programming
Basic Array Definition
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 8: Arrays Starting Out with C++ Early Objects Eighth Edition
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
EKT150 : Computer Programming
Lecture 9 Objectives Learn about arrays.
EKT120: Computer Programming
Starting Out with Programming Logic & Design
Standard Version of Starting Out with C++, 4th Edition
Array Data Structure Chapter 6
C++ Array 1.
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Presentation transcript:

Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

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 The Range-Based for loop 8.6 Processing Array Contents 8.7 Using Parallel Arrays

Topics (continued) 8.8 The typedef Statement 8.9 Arrays as Function Arguments 8.10 Two-Dimensional Arrays 8.11 Arrays with Three or More Dimensions 8.12 Vectors 8.13 Arrays of Objects

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

Declaring arrays USING typedef const int SIZE = 10; typedef double ListType[SIZE]; ListType a; ListType mylist; which is equivalent to double a[10]; double mylist[10];

8.6 Processing One-Dimensional Arrays Some basic operations performed on a one-dimensional array are initialize, input data, output data stored in an array, find the largest and/or smallest element. Each of these operations requires the ability to step through the elements of the array. Stepping-through the elements of an array is easily accomplished by a loop.

Consider a list declared as an array of the SIZE 100 The following for loop steps-through each element of the array list starting at the first element of list. for(i = 0; i < SIZE; i++) process list[i] If processing list requires inputting data into list, the statement in Line 2 takes the from of an input statement, such as the cin statement. for(i = 0; i < SIZE; i++) cin>>list[i];

Finding the largest element in the array maxIndex = 0; for(index = 1; index < SIZE; index ++) if(list[maxIndex] < list[index]) maxIndex = index; largest = list[maxIndex];

Array Problems In C++, there is no guard against indices that are out of bounds. Assignment, comparison of arrays, reading data into an array and printing the contents of an array must be done component-wise. list1 = list2; // illegal In order to copy one array into another array, use a loop. for(j = 0; j < arraySize; j++) y[j] = x[j];

8.9 Arrays as Parameters to Functions By Reference Only: In C++, arrays are passed by reference only. do not use the symbol & when declaring an array as a formal parameter. void initialize(ListType list, int SIZE) { int count; for(count = 0; count < SIZE; count++) list[count] = 0; }

8.7 Using Parallel Arrays Two (or more) Arrays are called parallel if their corresponding components hold related information. const int MAXSTUDENTS = 50; typedef int IdList [MAXSTUDENTS]; typedef char GradesList [MAXSTUDENTS]; IdList studentIds; GradeList courseGrades;

8.10 Two-Dimensional Arrays A two-dimensional array is a collection of a fixed number of components arranged in two dimensions, and all components are of the same type. The syntax for declaring a two-dimensional array is: typedef int Arraytype [intexp1][intexp2]; where intexp1 and intexp2 are expressions yielding positive integer values. The two expressions intexp1 and intexp2 specify the number of rows and the number of columns, respectively, in the array. typedef double SalesArrayType [STORES][WEEKS];

Accessing Array Components Declare the array SalesArrayType sales; To access a component: sales[5][3] = 25.75; If int i = 5; int j = 3; then the previous statement is equivalent to sales[i][j] = 25.75;

Processing Two-dimensional Arrays A two-dimensional array can be processed in three different ways. 1. Process the entire array. 2. Process a particular row of the array, called row processing. 3. Process a particular column of the array, called column processing. When processing a particular row or column of a two-dimensional array we employ algorithms similar to when we process one-dimensional arrays.

Suppose that we want to process row number, say 5 of matrix Suppose that we want to process row number, say 5 of matrix. The components of row number 5 of matrix are:

for(col = 0; col < COLUMNS; col++) processMatrix[row][col]; //In these components the first index is fixed at 5. row = 5; //The second index ranges from 0 to 5. for(col = 0; col < COLUMNS; col++) processMatrix[row][col];

Similarly, suppose that we want to process column number 2 of matrix, that is, the third column of matrix. The components of this column are:

for(row = 0; row < ROWS; row++) process matrix[row][col] The second index is fixed at 2 and the first index ranges from 0 to 6. The following for loop processes column 2 of matrix. col = 2; for(row = 0; row < ROWS; row++) process matrix[row][col]

Print The following nested for loops print the components of matrix, one row per line. for(row = 0; row < ROWS; row++) { for(col = 0; col < COLUMNS; col++) cout<<setw(5)<<matrix[row][col]<<" "; cout<<endl; }

Largest Element in Each Row and Each Column for(row = 0; row < ROWS; row++) { largest = matrix[row][0]; for(col = 1; col < COLUMNS; col++) if(largest < matrix[row][col]) largest = matrix[row][col]; cout<<"Largest element of row "<<row+1 <<" = "<<largest<<endl; }

Passing Two-dimensional Arrays as Parameters to Functions Two-dimensional arrays can be passed as parameters to a function and they are passed by reference. When storing a two-dimensional array in the computer’s memory, C++ uses the row order form. That is, first the first row is stored, followed by the second row, which is followed by the third row and so on. void initialize(TableType table) { int row; int col; for(row = 0; row < ROWS; row++) for(col = 0; col < COLUMNS; col++) table[row][col] = 0; }

8.11 Arrays with Three or More Dimensions Array: An array is a collection of a fixed number of elements (called components) arranged in n dimensions (n >= 1), called an n-dimensional array. The general syntax of declaring an n-dimensional array is: typedef dataType arrayName[intExp1][intExp2]...[intExpn]; where intExp1, intExp2, … , and intExpn are constant expressions yielding positive integer values.

The statement typedef double CarDealersType[10][5][7]; CarDealersType carDealers; declares carDealers to be a three-dimensional array. Size of the first dimension is 10 and it ranges from 0 to 9. The size of the second dimension is 5 and it ranges from 0 to 4. The size of the third dimension is 7 and it ranges from 0 to 6. The base address of the array carDealers is the address of the first array component, that is the address of carDealers[0][0][0]. The total number of components in the array carDealers is 10*5*7 = 350.

carDealers[5][3][2] = 15564.75; for(i = 0; i < 10; i++) for(j = 0; j < 5; j++) for(k = 0; k < 7; k++) carDealers[i][j][k] = 0.0; initializes the entire array to 0.0.

8.5 The Range-Based for Loop NOTE: This is a C++ 11 feature This loop simplifies array processing. It uses a variable that will hold a different array element for each iteration Format: for (data_type var : array) statement; data_type : the type of the variable var: the variable statement; : the loop body

Range-Based for Loop - Details data_type must be the type of the array elements, or a type that the array elements can be automatically converted to var will hold the value of successive array elements as the loop iterates. Each array element is processed in the loop statement; can be a single statement or a block of statements enclosed in { }

Range-Based for Loop - Example // sum the elements of an array int [] grades = {68,84,75}; int sum = 0; for (int score : grades) sum += score; See pr8-09.cpp and pr8.10.cpp

Range-Based for Loop - Example // modify the contents of an array const int ISIZE = 3; int [ISIZE] grades; for (int &score : grades) { cout << "Enter a score: "; cin >> score; }

Comparison: Range-Based for Loop vs. Regular for Loop The range-based for loop provides a simple notation to use to process all of the elements of an array. However, it does not give you access to the subscripts of the array elements. If you need to know the element locations as well as the element values, then use a regular for loop.

Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda