Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.

Slides:



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

Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Arrays Chapter 6.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
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.
Arrays.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and 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.
Module 1: Array ITEI222 - Advance Programming Language.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS 1430: Programming in C++.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
New Structure Recall “average.cpp” program
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Counting Loops.
Introduction To Programming Information Technology , 1’st Semester
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Presentation transcript:

Arrays Dr. Jose Annunziato

Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances of data of the same type Arrays contain (or refer) to several pieces data stored in contiguous memory spaces IndexValue IndexValue 0'D' 1'G' 2'R' IndexValue "String1" "String2" "String3"

Declaring and Sizing Arrays Arrays are declared using square brackets arrayType arrayName [ arraySize ]; Here are several arrays of various primitive types char letters [ 25 ]; int miles [ 100 ]; float temperatures [ 12 ]; double distances [ 200 ]; The total amount of space allocated is the size of the array times the size of type

Reading and Writing to Arrays Array content can be referenced by a zero-based index or subscript Use the assignment operator to write to the array temperatures [ 12 ] = 77.34; Use the array in right hand expression to read float tonightsTemperature = temperatures [ 12 ]; Index01234 Value'H''E''L' 'O'

Input and Output to/from Arrays Individual array elements behave as variables int balances [ DAYS_IN_WEEK ]; cin >> balances [ 0 ]; cin >> balances [ 1 ]; … cin >> balances [ 4 ]; cout << balances [ 0 ]; … cout << balances [ 4 ];

Iterating Over Arrays Since indices are integers, we can iterate over the size of the array int balance [ DAYS_IN_WEEK ]; for ( int d = 0; d < DAYS_IN_WEEK; d++ ) cin >> balance [ d ]; for ( int d = 0; d < DAYS_IN_WEEK; d++ ) cout << balance [ d ];

Avoid Edge Condition Errors Note that array indices are 0 based The first 1st index of an array of size of 100 is 0 and the last index is 99 int a [ 100 ]; int a [ 0 ] = 123; … int a [ 99 ] =234; When iterating make sure to stay within bounds for ( int i = 0; i < 100; i++ ) count << a [ i ] << endl;

Reading An Account Into an Array We can use an array to represent each of the columns in an array Consider reading the first 10 rows of an account stringdate [ 10 ]; stringtype [ 10 ]; stringdescription [ 10 ]; floatamount [ 10 ]; floatbalance [ 10 ];

Get Each Row and Parse Into Array accountFile.open ("checking.csv"); string row; for( int r = 0; r < 10; r++ ) { getline (accountFile,row); date [ r ] = parseCsvRow ( row, 0 ); type [ r ] = parseCsvRow ( row, 1 ); description [ r ] = parseCsvRow ( row, 2 ); amount [ r ] = atof ( parseCsvRow ( row, 3 ).c_str() ); balance [ r ] = atof ( parseCsvRow ( row, 4 ).c_str() ); } accountFile.close();

ParseCsvRowDemo.cpp

Writing Arrays Into An Account ofstream accountFile; accountFile.open(fileName, ios::out | ios::app); for ( int r = 0; r < 10; r++ ) { accountFile << date[ r ] << ","; accountFile << type [ r ] << ","; accountFile << description [ r ] << ","; accountFile << amount [ r ] << ","; accountFile << balance [ r ] << endl; } accountFile.close();

Array Initialization Arrays can be initialized when they are declared int days [ MONTHS ] = {31, 28, 31, 30, …, 31}; string planets [ 9 ] = { "Mercury", … "Pluto?" }; float coins [ 5 ] = { 0.05, 0.1, 0.25, 0.5, 1.0 }; Arrays can be partially initialized int numbers[ 7 ] = { 1, 2, 4, 8 }; Uninitialized elements will be set to 0

Implicit Sizing When an array is initialized, the size can be inferred from the initialization double ratings [] = { 1.0, 1.5, 2.0, 2.5, 3.0 }; You can't skip over initializers int array [ 6 ] = { 2, 4,, 8,, 12 };

Copying Arrays You can't assign an array to another array :( int array1 [ 5 ] = { 1, 2, 3, 4, 5 }; int array2 [ 5 ]; array1 = array2; But u can assign individual elements to each other array1 [ 2 ] = array2 [ 2 ]; To copy an array, just iterate over both for ( int j = 0; j < 5; j++ ) array1 [ j ] = array2 [ j ];

Printing An Array To print an array you need to iterate over it for ( int j = 0; j < 5; j++ ) cout << array1 [ j ] << endl; Unfortunately you can't just stream the array cout << array1 << endl;

Summing Up an Array Iterate over the array and keep track of a running total int total = 0; for ( int j = 0; j < 5; j++ ) total += array1 [ j ];

Computing the Average of an Array Add up the total array and divide the number of values int total = 0; for ( int j = 0; j < 5; j++ ) total += array1 [ j ]; float average = total / 5.0;

Finding the Max and Min Keep track of the max and min so far as you iterate over the array int max = array1 [ 0 ]; int min = array1 [ 0 ]; for ( int j = 0; j < 5; j++ ) { if ( max > array1 [ j ] ) max = array1 [ j ]; if ( min < array1 [ j ] ) min = array1 [ j ]; }

Comparing Arrays To compare arrays compare each element, the first discrepancy means arrays are different bool arraysAreEqual = true; int array1 [ 5 ], array2 [ 5 ]; for ( int k = 0; k < 5; k++ ) if ( array1 [ k ] != array2 [ k ] ) { arraysAreEqual = false; break; }

Arrays as Function Arguments bool areArraysEqual ( int a[], int b[], int size ) { for ( int k = 0; k < size; k++ ) O(n) if ( a [ k ] != b [ k ] ) return false; return true; } int main () { int x[ 4 ] = {1, 2, 3, 4}; int y[ 4 ] = {1, 2, 3, 4}; cout << areArraysEqual ( x, y, 4 ) << endl; }

arrayMax ( int [] ) int arrayMax ( int array[], size ) { O(n) int max = array [ 0 ]; for ( int j = 0; j < 5; j++ ) { if ( max > array [ j ] ) max = array [ j ]; return max; } int main() { int[] a = {1, 2, 3, 4, 5}; cout << arrayMax ( a, 5 ) << endl; }

Multidimentional Arrays Arrays can have more than one dimension int scores [ rows ] [ columns ]; Use an index per dimension to refer to each element scores [ 3 ] [ 4 ] = 97;

Initializing Multidimensional Arrays Multidimensional arrays are just arrays of arrays int scores[][] = { { 89, 78, 67, 78, 87 }, { 98, 87, 78, 98, 90 }, {76, 87, 89, 87, 89 } }

Iterating Over Dimensions Use nested for loops to iterate over each dimension for ( int row = 0; row < ROWS; row++ ) { O(n 2 ) for ( int col = 0; col < COLS; col++ ) { int value = scores [ row ] [ col ];

ArraysDemo.cpp