Multiple-Subscripted Array

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

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.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Array What it is. How to use it How to declare it How to initialize it.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
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.
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.
 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.
Building Java Programs Chapter 7.5
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
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.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
Systems Programming Concepts
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing 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.
Two Dimensional Array Mr. Jacobs.
Chapter 7: Arrays.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
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.
Array Data Structure Chapter 6
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.
Array What it is. How to use it How to declare it How to initialize it.
Presentation transcript:

Multiple-Subscripted Array What it is How to use it How to declare it How to initialize it

Multiple-Subscripted Arrays Use more than one subscript (can have up to 12) Double-subscripted array (2 subscripts)

Double-subscripted array good for representing tables in memory one subscript is the row second subscript is the column

Double-subscripted array column 0 column 1 column 2 column 3 row 0 a[0][0] a[0][1] a[0][2] a[0][3] row 1 a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] row 2 column subscript row subscript array name = a a double-subscripted array with 3 rows and 4 columns 3 by 4 array

Referencing elements in a double-subscripted array To set the element in row 2 and column 3 to the value 4 use: a [2] [3] = 4; column subscript row subscript name of the array

Declaring a 3 by 4 array int a [3] [4]; must tell compiler name, size (2 sizes) and type all elements of the array must be the same type any data type is fine (int, float, char, etc.)

Declaring a double-subscripted array Must use constants to declare row and column sizes. int a [3] [4]; char letters [2] [26]; #define ROW 3 #define COLUMN 4 int a [ROW] [COLUMN]; const int row = 3; const int column = 4; int a [row] column]; const int array_size 5 defines array_size to be a constant variable that cannot be changed. It will always hold the number 5. constant variables may be used to declare the size of an array since they don’t change.

Initializing a double-subscripted array const int row = 3; const int column = 4; int a [row] [column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { a [i][j] = 0; }

Initializing a double-subscripted array with declaration const int row = 3; const int column = 4; int a[row] [column] = {{1,2,3,4}, {1,2,3,4}, {1,2,3,4}}; 1 2 3 4 3 1 2 4 3 4 1 2

Initializing a double-subscripted array with declaration const int row = 3; const int column = 4; int a[row] [column] = {{1,2,3}, {1,2}, {1,2,3,4}}; 1 2 3 1 2 3 4 1 2

Setting entire array to 0 const int row = 3; const int column = 4; int a [row] [column] = {0};

Exercises Answer the following questions regarding an array called table that is a double-subscripted array with 2 rows and 3 columns. 1) Define constant variables numRows and numCols to be used to declare the array. 2) Declare the array to be an integer array. 3) Use a for loop to initialize each element of the array to be the sum of it’s subscripts 4) Write a code segment to print the array as a table.

How double-subscripted arrays are stored in memory table 1 2 3 4 5 6 7 8 9 10 11 12 table 1 2 3 4 5 6 7 8 9 10 11 12

passing double-subscripted array to a function Must tell function size of second subscript so it can find the beginning of each row in memory. void PrintTable (int table [ ] [4], int numRows, int numCols) { for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { cout << setw(4) << table [i] [j]; } cout << endl;

passing double-subscripted array to a function passed as reference parameter Use const to prevent function changing the array. void PrintTable (const int table [ ] [4], int numRows, int numCols);

Exercises Write a main program as follows: Use the results of the last set of exercises to declare an array of type integers with 2 rows and 3 columns and initialize each element to the sum of it’s subscripts. Call the PrintTable function to print the array. Create a function called PrintTable to print each element of the array in tabular form.