Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Arrays.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Input & Output: Console
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
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.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Lecture DS & Algorithms:09 Abstract Data Types. Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
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.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 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.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
 2005 Pearson Education, Inc. All rights reserved Arrays.
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.
Windows Programming Lecture 03. Pointers and Arrays.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
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.
© 2016 Pearson Education, Ltd. All rights reserved.
Data Structures and Abstract Data Types
7 Arrays.
EKT150 : Computer Programming
7 Arrays.
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.
C++ Array 1.
Presentation transcript:

Arrays

C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation is direct access to each element in the array so values can be retrieved from or stored in this element.

C++ Style Data Structures: Arrays (2) Properties:  Ordered so there is a first element, a second one, etc.  Fixed number of elements — fixed capacity  Elements must be the same type (and size);  use arrays only for homogeneous data sets.  Direct access: Access an element by giving its location The time to access each element is the same for all elements, regardless of position. in contrast to sequential access (where to access an element, one must first access all those that precede it.)

Declaring arrays in C++ where element_type is any type array_name is the name of the array — any valid identifier CAPACITY (a positive integer constant ) is the number of elements in the array score[0] score[1] score[2] score[3] score[99] element_type array_name[CAPACITY]; e.g., double score[100]; The elements (or positions) of the array are indexed 0, 1, 2,..., CAPACITY - 1. Can't input the capacity, Why? The compiler reserves a block of “consecutive” memory locations, enough to hold CAPACITY values of type element_type.

indices numbered 0, 1, 2,..., CAPACITY - 1 How well does C/C++ implement an array ADT? As an ADTIn C++ ordered fixed size same type elements direct access element_type is the type of elements CAPACITY specifies the capacity of the array subscript operator []

an array literal Array Initialization Example: double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21}; Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0. double rate[5] = {0.11, 0.13, 0.16}; Note 2: It is an error if more values are supplied than the declared size of the array. How this error is handled, however, will vary from one compiler to another. In C++, arrays can be initialized when they are declared. Numeric arrays: element_type num_array[CAPACITY] = {list_of_initial_values};

Note 1: If fewer values are supplied than the declared size of the array, the zeroes used to fill un-initialized elements are interpreted as the null character '\0' whose ASCII code is 0. const int NAME_LENGTH = 10; char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'}; char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; Character Arrays: Character arrays may be initialized in the same manner as numeric arrays. declares vowel to be an array of 5 characters and initializes it as follows:

Addresses When an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the base address of the array. Each array reference must be translated into an offset from this base address. For example, if each element of array score will be stored in 8 bytes and the base address of score is 0x1396. A statement such as cout << score[3] << endl; requires that array reference score[3] be translated into a memory address: 0x * sizeof (double ) = 0x * 8 = 0x13ae The contents of the memory word with this address 0x13ae can then be retrieved and displayed. An address translation like this is carried out each time an array element is accessed. score[3]  [0] [1] [2] [3] [99] score  0x1396 0x13ae What will be the time complexity

The value of array_name is actually the base address of array_name array_name + index is the address of array_name[index]. An array reference array_name[index] is equivalent to For example, the following statements of pseudocode are equivalent: print score[3] print *(score + 3) Note: No bounds checking of indices is done! * is the dereferencing operator *ref returns the contents of the memory location with address ref *(array_name + index) What will happen incase of going overboard

Problems with Arrays 1. The capacity of Array can NOT change during program execution. What is the problem? Memory wastage Out of range errors 2. Arrays are NOT self contained objects What is the problem? No way to find the last value stored. Not a self contained object as per OOP principles.

C++ Style Multidimensional Arrays Most high level languages support arrays with more than one dimension. 2D arrays are useful when data has to be arranged in tabular form. Higher dimensional arrays appropriate when several characteristics associated with data. For storage and processing, use a two-dimensional array. Example: A table of test scores for several different students on several different tests.

Declaring Two-Dimensional Arrays Standard form of declaration: element_type array_name[NUM_ROWS][NUM_COLUMNS]; Example: const int NUM_ROWS = 30, NUM_COLUMNS = 4; double scoresTable[NUM_ROWS][NUM_COLUMNS]; Initialization  List the initial values in braces, row by row;  May use internal braces for each row to improve readability. Example: double rates[][] = {{0.50, 0.55, 0.53}, // first row {0.63, 0.58, 0.55}}; // second row... [0] [1] [2] [3] [29] [0] [[1] [2] [3]

Processing Two-Dimensional Arrays  Remember: Rows (and) columns are numbered from zero!!  Use doubly-indexed variables: scoresTable[2][3] is the entry in row 2 and column 3   row index column index  Use nested loops to vary the two indices, most often in a rowwise manner. Counting from 0

Higher-Dimensional Arrays The methods for 2D arrays extend in the obvious way to 3D arrays. Example: To store and process a table of test scores for several different students on several different tests for several different semesters const int SEMS = 10, STUDENTS = 30, TESTS = 4; typedef double ThreeDimArray[SEMS][STUDENTS][TESTS]; ThreeDimArray gradeBook; gradeBook[4][2][3] is the score of 4 th semester for student 2 on test 3 // number of semesters, students and tests all counted from zero!!

Arrays of Arrays double scoresTable[30][4]; Declares scoresTable to be a one-dimensional array containing 30 elements, each of which is a one-dimensional array of 4 real numbers; that is, scoresTable is a one-dimensional array of rows, each of which has 4 real values. We could declare it as typedef double RowOfTable[4]; RowOfTable scoresTable[30];... [0] [1] [2] [3] [29] [0] [[1] [2] [3]... [0] [1] [2] [3] [29]

scoresTable[i] is the i-th row of the table Address Translation: The array-of-arrays structure of multidimensional arrays explains address translation. Suppose the base address of scoresTable is 0x12348 : scoresTable[10] 0x *( sizeof RowOfTable) In general, an n-dimensional array can be viewed (recursively) as a one-dimensional array whose elements are (n - 1)-dimensional arrays. In any case: scoresTable[i][j] should be thought of as ( scoresTable[i])[j] that is, as finding the j-th element of scoresTable[i].  scoresTable[10][3]  base( scoresTable[10 ]) + 3*( sizeof double ) scoresTable[10][4] [3] [0] [1] [9] [10]... = 0x * (4 * 8) + 3 * 8 = 0x124a0 = 0x * (4 * 8)

Implementing Multidimensional Arrays More complicated than one dimensional arrays. Memory is organized as a sequence of memory locations, and is thus 1D How to use a 1D structure to store a MD structure? ABCDEFGHIJKLABCDEFGHIJKL A character requires a single byte Compiler instructed to reserve 12 consecutive bytes Two ways to store consecutively i.e. rowwise and columnwise.

Implementing Multidimensional Arrays ABCDEFGHIJKLABCDEFGHIJKL RowWise A B C D E F G H I J K L ColumnWise A E I B F J C G K D H L ABCDEFGHIJKLABCDEFGHIJKL ABCDEFGHIJKLABCDEFGHIJKL