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.

Slides:



Advertisements
Similar presentations
Arrays.
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.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
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.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Arrays.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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
Pointers CSE 5100 Data Structures and Algorithms.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 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.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
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.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
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.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
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.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
For Friday Read No quiz Program 6 due. Program 6 Any questions?
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.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
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.
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.
7 Arrays.
Arrays Kingdom of Saudi Arabia
Multidimensional 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.
7 Arrays.
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.
C++ Array 1.
Presentation transcript:

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 in a difficult to maintain and error-prone program. Instead use arrays Array = a named location in memory for many values of the same type, stored sequentially and accessed through an index Compare: variable = a named location in memory for one value.

2 Arrays Think of an array a sequence of boxes, each one holding a value Example: An array named scores that holds the quiz scores of 5 students: element 0element 1element 2element 3element scores

3 Arrays Array declaration syntax element_type array_name [ size ] ; element_type is the type of the elements stored in the array. array_name is the name of the array. Naming rules are the same as with variables. size must be a constant (known at compile time) no variables allowed you cannot change the array size at runtime use a #define directive to define the size.

4 Arrays Declaring and initializing an array Example 1: int scores[5]; the elements of scores have not been initialized. They may be initialized one at a time (usually in a for loop: for (int i = 0; i < 5; i++) { cout << “Enter score: “; cin >> scores[i]; } Example 2: int scores[5] = {11, 10, 12, 9, 11}; the elements are initialized at declaration time. When this is done, the size of the array may be omitted from the declaration.

5 Arrays Accessing the elements of an array. To access the element at index i specify the name of the array and the index enclosed in brackets. Example: scores[3] = 11; CAUTION! Indexing always starts at zero and ends at size-1 int scores[5]; scores[0] = 11; scores[1] = 12; scores[2] = 10; scores[3] = 9; scores[4] = 11; The compiler will NOT catch a wrong index. Typing scores[10] or scores[-1] can corrupt your data.

6 Arrays How exactly do they work? int scores[5]; says: I need a chunk of memory big enough to hold 5 integer variables. That’s a total of 5*4 = 20 bytes. scores itself represents the memory address where this chunk of memory begins. Think of it as a special kind of constant that holds the address of a location in memory. Example: int main () { int arr[2]; cout << arr << endl; return 0; } The output of this program is: 0xbfffb240 (the address of arr in hexadecimal)

7 Arrays How exactly do they work? scores[i] represents the contents in box #i starting at address scores. In other words, it represents the contents at address scores + i * sizeof(int)

8 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a function as one of its arguments. int main () { int nums[3] = {3, 6, 5}; int sq; sq = square( nums[2] ); cout << nums[2] << “squared is “ << sq << endl; return 0; } int square( int x ) { return x * x; } copy (pass by value) Output : 5 squared is 25

9 Arrays & functions To pass the entire array as an argument: In the function prototype specify that the argument is an array by placing brackets after the name. Do NOT put the size of the array inside the brackets. Example: void printArray ( int arr[ ] ) ; Call the function using ONLY the array name as an argument. Example: int nums[5] = {1,2,3,4,5}; printArray( nums );

10 Arrays & functions int main () { int scores[5] = {11,12, 10, 9, 11}; printArray( scores, 5 ); return 0; } void printArray(int arr[], int size) { for (int i=0; i<size; i++) cout << arr[i] << “ “; cout << endl; } The parameters are again passed by value. However, the value of scores (which is copied onto arr ) is the address where the array begins. As a result, both arr and scores refer to the same location in memory! This allows a function to modify the elements of an array that is passed as an argument.

11 Arrays & functions int main () { int scores[5] = {11,12, 10, 9, 11}; cleanArray( scores, 5); printArray( scores, 5 ); return 0; } void printArray(int arr[], int size) { for (int i=0; i<size; i++) cout << arr[i] << “ “; cout << endl; } void cleanArray(int arr[], int size) { for (int i=0; i<size; i++) arr[i] = 0; } Output : This actually modifies the values stored in scores.

12 Multidimensional arrays 1D array: 2D array: 3D array: 1 row of 5 elements 3x5 array. Think of it as an array of arrays ( = an array of three elements which are arrays of five elements) 3x3x1 array.

13 Multidimensional arrays How are they stored in memory? What are the indices of the elements? row 1 row 2 row 3 3x2 array 0,00,10,2 1,01,11,21,2 0,3 1,3 2x4 array rowcolumn

14 Multidimensional arrays Declaration: similar to 1D, but specify #rows, #columns Example: double stats[10][4]; // a 10x4 array of doubles Initialization: similar to 1D. Use nested for-loops or initialize at declaration. Example 1: Example 2: for (int i = 0; i < 10; i++) for (int j = 0; j < 4; j++) stats[ i ][ j ] = 0.0; int grid[3][2] = { {0, 1}, {1, 1}, {0, 0} }; row 1 row 2 row 3

15 Multidimensional arrays CAUTION! When a multidimensional array is an argument to a function, the size of all but the first subscript MUST be specified. Example: void initArray( int arr[][10], int numrows); // prototype... int main() { int nums[5][10]; initArray(nums, 5); // function call... }