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
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 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.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Arrays Chapter 6.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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.
 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.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Basic Elements of C++ Chapter 2.
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.
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.
Chapter 8 Arrays and Strings
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
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.
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.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
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.
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.
Computer Programming for Engineers
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
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.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
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
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
© 2016 Pearson Education, Ltd. All rights reserved.
One-Dimensional Array Introduction Lesson xx
7 Arrays.
EKT150 : Computer Programming
7 Arrays.
Arrays Arrays A few types Structures of related data items
C++ Array 1.
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].

Rules of declaring 1D arrays which type of data are the elements - int? float? or char? how many data or elements? – size of array. how to instruct the computer to remember an array in question? – name of the array. examine an array to be declared below, float a[3]; –type: float –size: 3, which means the array contains 3 elements. –name: a[]. Note that square brackets are used.

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; }

Further explanation of the rules Don’t miss “ ;” in array declaration. int a(10) is wrong. 1 st element index is a[0] instead of a[1]. int a[3]; is different from a[3];. size of arrays is an integer constant, never a variable, e.g., int n=3; a[n]; is wrong. However const int n=3; a[n]; is right.

Address of an array #include main() { float a[3]; cout << "&a[0]= " << &a[0] <<endl; cout << "&a= " << &a <<endl; cout << "a= " << a <<endl; } The addresses of array elements can be obtained by adding a sign & to the element, e.g., &a[0]. Run the following codes we can get the result: a=&a = &a[0].

Initialization of an array Method 1- full initialization when declaring an array – int a[4] = {3, 1, 0, 9}; –It gives the result of initialization, a[0]=3, a[1]=1, a[2]=0, a[3]=9. Method 2- partial initialization when declaring the array – int a[4] = {3, 1}; –It gives a[0]=3, a[1]=1, a[2]=0, a[3]=0. –This means that the missed element values are assigned with zeros automatically.

Initialization of an array Method 3- hidden initialization – int a[4]; –In this case all the elements will be automatically assigned with zeros. So the elements hold values as follows, –a[0]=0, a[1]=0, a[2]=0, a[3]=0. Method 4- initialization with the hidden array size –int a[] = {1,3,5,10}; –this array declaration is equivalent to –int a[4] = {1,3,5,10}; –This means the size of the array is determined by the compiler according to the actual number of elements.

Initialization of an array { float sale[7], key_in; int i; for (i=0; i < 7; i++) { cout <<"key_in= "; cin >> key_in; sale[i] = key_in; cout << "sale[" << i << "]= " << key_in <<endl; } } Method 5- initialization with a loop statement for…

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.

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; }

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).

Initialization of 2D arrays Method 1: – a[2][3]={{2, 4, 3},{7, 8, 1}}; method 2: – a[2][3]={2, 4, 3, 7, 8, 1};

Rules of declaring 2-D arrays Three basic issues: –which type of data - int? float? or char? –how many data? – sizes of array, the number of rows and the number of columns. –how to make the computer identify an array in question? - name float a[2][3]; –This array has 2 rows and 3 columns

A string as an array of characters The text bad! in the last example can be assigned, as a string “bad!”, to an array ex1[]. This method is more straightforward than the last one. Only thing to remember is that an additional character \0 is implicitly added to the string to terminate the string. So the array size should be one more. For this example, the text size is 4 and hence the array size is 5. { char ex1[5]="bad!";; cout<<ex1<<endl; }

Declaration of a 1D array for a string You may have found that the array name can also represent and output the string. Another string array sample without the declaration of size is as follows. In this case, the size of the array is automatically calculated, 11 (text size is 10). char ex2 [ ] = “30/09/2004”;

Input strings from the keyboard Input a string containing no spaces { char name[10]; cin >> name; } Input a string containing spaces { char name[10]; cin.get(name, n); // n is the number of characters }