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.

Slides:



Advertisements
Similar presentations
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Advertisements

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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Multiple-Subscripted Array
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
CSE202: Lecture 14The Ohio State University1 Arrays.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
A First Book of ANSI C Fourth Edition
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
Chapter 8 Arrays and Strings
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 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.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
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.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
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.
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.
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
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.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
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.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional 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.
Windows Programming Lecture 03. Pointers and Arrays.
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.
EGR 2261 Unit 10 Two-dimensional Arrays
Lecture 18 Arrays and Pointer Arithmetic
7 Arrays.
Presentation transcript:

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 0, 1, 2, 3,.... These numbers are called index values or subscripts of the array. The term “subscript” is used because as a mathematical sequence, an array would be written with subscripts: a0, a1, a2, …. The subscripts locate the element’s position within the array, thereby giving direct access into the array. If the name of the array is a, then a[0] is the name of the element that is in position 0, a[1] is the name of the element that is in position 1, etc. In general, the ith element is in position i–1. So if the array has n elements, their names are a[0], a[1], a[2], …, a[n-1].

PROCESSING ARRAYS An array is a composite object: it is composed of several elements with independent values. In contrast, an ordinary variable of a primitive type is called a scalar object. The first example shows that array elements can be assigned and accessed the same as ordinary scalar objects. int main() { double a[3]; a[2] = 55.55; a[0] = 11.11; a[1] = 33.33; cout << "a[0] = " << a[0] << endl; cout << "a[1] = " << a[1] << endl; cout << "a[2] = " << a[2] << endl; }

Processing Arrays EXAMPLE Printing a Sequence in Order This program reads five numbers and then prints them in reverse order: int main() { const int SIZE=5; // defines the size N for 5 elements double a[SIZE]; // declares the array’s elements as type double cout << "Enter " << SIZE << " numbers:\t"; for (int i=0; i<SIZE; i++) cin >> a[i]; cout << "In reverse order: "; for (int i=SIZE-1; i>=0; i--) cout << "\t" << a[i]; }

INITIALIZING AN ARRAY This program initializes the array a and then prints its values: int main() { float a[] = { 22.2,44.4, 66.6 }; int size = sizeof(a)/sizeof(float); for (int i=0; i<size; i++) cout << "\ta[" << i << "] = " << a[i] << endl; } The first line declares a to be the array of 3 elements described above. The second line uses the sizeof() function to compute the actual number of elements in the array. The value of sizeof(float) is 4 because on this machine a float value occupies 4 bytes in memory. The value of sizeof(a) is 12 because the complete array occupies 12 bytes in memory. Therefore, the value of size is computed to be 12/4 = 3.

Initializing an Array with Trailing Zeros This program initializes the array a and then prints its values: int main() { float a[7] = { 22.2,44.4,66.6 }; int size = sizeof(a)/sizeof(float); for (int i=0; i<size; i++) cout << "\ta[" << i << "] = " << a[i] << endl; }

ARRAY INDEX OUT OF BOUNDS In some programming languages, an index variable will not be allowed to go beyond the bounds set by the array’s definition. For example, in Pascal, if an array a is defined to be indexed from 0 to 3, then the reference a[6] will crash the program. This is a security device that does not exist for arrays in C++ (or C). As the next example shows, the index variable may run far beyond its defined range without any error being detected by the computer. This program has a run-time error: it accesses a part of memory that is not allocated: int main() { const int SIZE=4; float a[SIZE] = { 33.3, 44.4,55.5,66.6 }; for (int i=0; i<7; i++) // ERROR: index is out of bounds! cout << "\ta[" << i << "] = " << a[i] << endl; }

PASSING AN ARRAY TO A FUNCTION The code float a[] that declares an array a in the previous examples tells the compiler two things: the name of the array is a, and the array’s elements have type float. The symbol a stores the array’s memory address. So the code float a[] provides all the information that the compiler needs to declare the array. The size of the array (i.e., the number of elements in the array) does not need to be conveyed to the compiler. C++ requires the same information to be passed to a function that uses an array as a parameter.

EXAMPLE Passing an Array to a Function that Returns its Sum int sum(int[],int); int main() { int a[] = { 11,33, 55,77 }; int size = sizeof(a)/sizeof(int); cout << "sum(a,size) = " << sum(a,size) << endl; } int sum(int a[],int n) { int sum=0; for (int i=0; i<n; i++) sum += a[i]; return sum; }

Input and Output Functions for an Array This program uses a read() function to input values into the array a interactively. Then it uses a print() function to print the array: void read(int[],int&); void print(int[],int); int main() { const int MAXSIZE=100; int a[MAXSIZE]={0},size; read(a,size); cout << "The array has " << size << " elements: "; print(a,size); } void read(int a[],in t& n) { cout << "Enter integers. Terminate with 0:\n"; n = 0; do { cout << "a[" << n << "]: "; cin >> a[n]; } while (a[n++] != 0 && n < MAXSIZE); --n; // don't count the 0 } void print(int a[],i nt n) { for (int i=0; i<n; i++) cout << a[i] << " "; }

Printing the Memory Location of an Array This program prints the value of the address stored in an array name. int main() { int a[] = { 22,44, 66,88 }; cout << "a = " << a; // the address of a[0] }

THE LINEAR SEARCH ALGORITHM Computers are probably used more for the storage and retrieval of information than for any other purpose. Data is often stored in a sequential structure such as an array. The simplest way to find an object in an array is start at the beginning and inspect each element, one after the other,until the object is found. This method is called the Linear Search algorithm.

EXAMPLE The Linear Search This program tests a function that implements the Linear Search algorithm: int index(int,int[],int); int main() { int a[] = { 22,44, 66,88,44,66,55 }; cout << "index(44,a,7) = " << index(44,a,7) << endl; cout << "index(50,a,7) = " << index(50,a,7) << endl; } int index(int x,int a[],int n) { for (int i=0; i<n; i++) if (a[i] == x) return i; return n; // x not found }

MULTIDIMENSIONAL ARRAYS The arrays we have used previously have all been one- dimensional. This means that they are linear; i.e., sequential. But the element type of an array can be almost any type, including an array type. An array of arrays is called a multidimensional array. A one- dimensional array of one-dimensional arrays is called a two-dimensional array; a one-dimensional array of two- dimensional arrays is called a three-dimensional array; etc. The simplest way to declare a multidimensional array is like this: double a[32][10][4]; This is a three-dimensional array with dimensions 32, 10, and 4. The statement a[25][8][3] = would assign the value to the element identified by the multi-index (25,8,3).

Reading and Printing a Two- Dimensional Array This program shows how a two- dimensional array can be processed: void read(int a[][5]); void print(cont int a[][5]); int main() { int a[3][5]; read(a); print(a); } void read(int a[][5]) { cout << "Enter 15 integers,5 per row:\n”; for (int i=0; i<3; i++) { cout << "Row " << i << ": "; for (int j=0; j<5; j++) cin >> a[i][j]; } void print(const int a[][5]) { for (int i=0; i<3; i++) { for (int j=0; j<5; j++) cout << " " << a[i][j]; cout << endl; }