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.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Advertisements

Arrays part 3 Multidimensional arrays, odds & ends.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
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.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
Arrays.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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 ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional 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.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 5 Arrays Introducing Arrays
MSIS 655 Advanced Business Applications Programming
Single-Dimensional Arrays chapter6
Multi-Dimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
C++ Array 1.
Chapter 7 Multidimensional Arrays
Presentation transcript:

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

 Problem:  How to store numerous values of same data type.  E.g. store 100 numbers and find max. of those.  Can we store like:  int a, b, c, d, e……. z, A, B, C ……. Z, aa, ab, ac…. az, Aa, Ab, Ac…. Av;  Solution:  int a[100]; // an array

1. Single Dimensional Arrays 2. Multi Dimensional Arrays

 Form: datatype arrayname[arraySize];  Example: double myList[10];  Each element in the array is referred to by an index. myList[0] references the first element in the array. myList[9] references the last element in the array.

 Once an array is created, its size is fixed. It cannot be changed. You can find its size using sizeof(arrayName); For example, sizeof(myList) returns 10

 Using a loop: for (int i = 0; i < sizeof(myList); i++) myList[i] = i; // cin>>myList[i];  Declaring, creating, initializing in one step: double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.

double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double myList[]; myList = {1.9, 2.9, 3.4, 3.5};

 Example: double myList[10]; myList[0]myList[1]myList[2]myList[3]..... myList[9]

Arrays are easily printed using for loops. However, formatting the output may require some thought. Determine the output in each case shown below. Example 1: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int j = 0; j < 12; j++) cout << Grades[ j ] << endl; Example 2: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int j = 0; j < 12; j+=3) cout << Grades[ j ] << Grades[j+1] << Grades[j+2] << endl;

Example 3: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int Row = 0; Row <= 2; Row++) for (int Col = 0; Col <=3; Col++) cout << Grades[ 4*Row+Col ] << endl; Example 4: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int Row = 0; Row <= 2; Row++) { for (int Col = 0; Col <=3; Col++) cout << Grades[ 4*Row+Col ]; cout << endl; }

Write a program to accept a list of numbers (e.g. 10 numbers) from user and search for a specific number given by user.

So far we have used one-dimensional (1D) arrays, but we can also use arrays with 2 or more dimensions. 2D arrays essentially correspond to matrices. Example: int A;// single variable int B[6];// 1D array int C[3][4];// 2D array (matrix) int D[3][4][5];// 3D array int E[3][4][5][3];// 4D array //etc How many variables are stored in each case? ArrayNumber of variables B C D E 15

Multi-dimensional Arrays One subscript for each dimension Use square brackets for each subscript Each subscript begins with 0 A 2D array is typically represented as a matrix Example: int A[3][4];// 2D array (matrix) with 3 rows and 4 columns Col 0 Col 1 Col 2 Col 3 Row 0 Row 1 Row 2 A[0][0] A[1][2] A[2][3] 16

Specifying values in arrays As with 1D arrays, values can be loaded into multidimensional arrays: explicitly (referring to each element by its indices) or using a list Example: Loading values into a matrix explicitly int A[2][3];// 2D array (matrix) with 2 rows and 3 columns A[0][0] = 1; A[0][1] = 2; A[0][2] = 3; A[1][0] = 4; A[1][1] = 5; A[1][2] = 6; Results for matrix A: 17

Visualizing multi-dimensional arrays We often use sketches to help us visualize arrays, such as: 1D array - single row or column 2D array – matrix 3D array – cube of cells 4D array – row or column of cubes? Example: int A, B1[6], B2[6], C[3][4], D[3][4][5], E[3][4][5][3]; A B1 B2 CD E 18

Visualizing multi-dimensional arrays Although we might find the visualizations on the previous slide to be useful, note that C++ stores the variables linearly, with the last index varied first (then the second index from the last, etc). This is important when loading an array with a list. Example: int C[3][4], D[2][3][4]; C D C[0][0] C[0][1] C[0][2] C[0][3] C[1][0] C[1][1] C[1][2] C[1][3] C[2][0] C[2][1] C[2][2] C[2][3] D[0][0][0] D[0][0][1] D[0][0][2] D[0][0][3] D[0][1][0] D[0][1][1] D[0][1][2] D[0][1][3] D[0][2][0] D[0][2][1] D[0][2][2] D[0][2][3] D[1][0][0] D[1][0][1] D[1][0][2] D[1][0][3] D[1][1][0] D[1][1][1] D[1][1][2] D[1][1][3] D[1][2][0] D[1][2][1] D[1][2][2] D[1][2][3] Note that matrices are filled in by row when using a list. Note that the indices increase numerically for any array. In this case: (..,111,112,113,120,..) 19

Loading values into a matrix using a list Example: Fill out the matrices indicated below. int A[2][3] = {3,6,9,12,15,18}, B[2][3] = {7,8,9}, C[][2] = {2,4,6,8,10,12,14,16} ; // Note: If the leftmost index is omitted, the array is sized according // to the number of list elements. // Recall from 1D arrays the effect of listing fewer values than the // size of the array. Matrix A: Matrix B: Matrix C: 20

Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int matrix[10][10]; matrix[0][0] = 3; for (int i=0; i<10; i++) for (int j=0; j<10; j++) { matrix[i][j] = i+j; } double[][] x;

You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example, int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; This is equivalent to the following statements: int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;

int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; array.length array[0].length array[1].length array[2].length

Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };

 Objective: Use two-dimensional arrays to create two matrices, and then add and multiply the two matrices. TestMatrixOperationRun

c ij = a i1  b 1j +a i2  b 2j +a i3  b 3j +a i4  b 4j +a i5  b 5j