Multiple Dimension Arrays

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
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 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Java Unit 9: Arrays Declaring and Processing Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
Arrays- Part 2 Spring 2013Programming and Data Structure1.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright © Curt Hill Multiple Dimension Arrays Extending Java Arrays.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
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.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Copyright Curt Hill Arrays in C/C++ More on usage.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 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.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
ARRAYS.
Arrays.
Arrays Chapter 7.
EGR 2261 Unit 10 Two-dimensional Arrays
UNIT 5 C Pointers.
Two-Dimensional 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.
Computer Programming BCT 1113
Hassan Khosravi / Geoffrey Tien
Numeric Arrays Numeric Arrays Chapter 4.
JavaScript: Functions.
Arrays in C.
C Passing arrays to a Function
Chapter 10: Pointers 1.
Multidimensional Arrays
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Arrays in Java What, why and how Copyright Curt Hill.
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Multidimensional array
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.
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
Array Data Structure Chapter 6
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.
Arrays and Pointers.
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

Multiple Dimension Arrays Extending C++ Arrays Copyright © 1998-2014 Curt Hill

Suppose We are recording test scores: int t1[MAX], t2[MAX], t3[MAX]; These are parallel arrays The value t1[2] and t2[2] both refer to same student, but different tests Parallel arrays are useful, but usually when the base type is different A multiple dimension array or an array of structs or classes is usually better Copyright © 1998-2014 Curt Hill

What is a multiple dimension array? An array of arrays An array with multiple brackets One dimension is a column or list Two dimensions is a table or rectangle Three dimensions is rectangular solid Four dimensions is hard to visualize Copyright © 1998-2014 Curt Hill

Multidimensional arrays Declaration and allocation: int t [8] [4]; for(m=0;m<8;m++) for(n=0;n<4;n++) t[m][n] = 0; Eight students with four tests Copyright © 1998-2014 Curt Hill

Organization In the above: t is a constant pointer to a table – a two dimensioned array of ints Main use is to pass as a parameter t[1] is a pointer to a row of ints Could be passed anywhere a single dimensioned array of ints could be passed t[1][1] is a simple int Copyright © 1998-2014 Curt Hill

More than 2 No real restrictions on the number of dimensions, other than memory consumption int ar[4][7][11][3][5]; Requires 4*7*11*3*5 = 4620 integers This is 18480 bytes of storage The size is the product of dimensions times size of individual Copyright © 1998-2014 Curt Hill

Initialization Brace notation may be used as well Either a single set of values within braces or braces in braces Either int ar[3][2] = {1,2,3,4,5,6}; Or int ar[3][2] = {{1,2},{3,4},{5,6}}; No significant difference Copyright © 1998-2014 Curt Hill

Storage In most cases a two dimensional array is no different than a single but other cases require that we know how it is stored Consider: int ar[3][2] = {0,1,2,3,4,5}; It is no surprize that the first element is ar[0][0] and it will be initialized to zero But does the one get stored in a[1][0] or a[0][1]? Turns out the first subscript varies most slowly so next element is a[0][1] Copyright © 1998-2014 Curt Hill

Location This is important in several places: Location of initialized values Parameter passage So if int ar[3][2] = {0,1,2,3,4,5}; Then a[0][1] is 1 and a[1][0] is 2 etc It is also acceptable to use the following intialization: int ar[3][2]={{0,1},{2,3},{4,5}}; While this is not: int ar[3][2]={{0,1,2},{3,4,5}}; Copyright © 1998-2014 Curt Hill

Parameter Passage We have a problem A function cannot determine the length of an array How then can it handle a multi-dimension array? First we have to consider address calculation in arrays Copyright © 1998-2014 Curt Hill

Single Dimension Array Consider the following declaration Item var[MAX]; address(var) + sizeof(Item) * ndx It needs the base address of the array This is actually the constant pointer The name of the variable To this it must add the product of The length of one Item thing The subscript Copyright © 1998-2014 Curt Hill

Double Consider the following declaration Item var[M][N]; var[i][j] address(var) + sizeof(Item)*N*i + sizeof(item)*j The starting point is the base address The next is the length of a row Finally the position in a row Copyright © 1998-2014 Curt Hill

Difference Did you notice the difference? Item var[MAX]; var[ndx] address(var) + sizeof(Item) * ndx Item var[M][N]; var[i][j] address(var) + sizeof(Item)*N*i + sizeof(item)*j Where does MAX, M or N occur in the formulas? Copyright © 1998-2014 Curt Hill

Discussion The singly dimensioned array address calculation does not consider the length of the array This allows a function that processes singly dimensioned arrays the freedom to accept any size array The second size does appear in the formula What does this mean? We must always specify the second size Copyright © 1998-2014 Curt Hill

Multiple Dimension Arrays Parameters When passing multidimensional arrays we must include every length except the first The first may be included, but is not required Thus double x(int a[][]); will not work, we must say instead: double x (int a[ ][4]); Const is also available Copyright © 1998-2014 Curt Hill

Calling Suppose: double x (int a[ ][4]); int ar1[10][4], ar2[1000][4]; The legal calls of x include: x(ar1); x(ar2); In this case the function x must have some means of knowing sizes other than by parameters Copyright © 1998-2014 Curt Hill

Libraries One of the beauties of C/C++ was the ability to make libraries of functions that could manipulate arrays of any size This seems to not be the case in regards to multiple dimension arrays Or is it? Lying is now in order Copyright © 1998-2014 Curt Hill

Matrix Manipulation Matrices are very important concept We want C libraries that handle them So we lie We pass things as a single dimension array We also pass the physical sizes as well as the used sizes Other multiple dimension arrays library routines use similar techniques Copyright © 1998-2014 Curt Hill

Matrix routines written to take: Any two dimensional double array Using any subset of the available sizes To do this they declare the array as a single dimension They take in as parameters the actual and used dimensions Do the actual address calculations themselves Copyright © 1998-2014 Curt Hill

Example We look at a project that does the lie thing with matrices It will do declarations like this: void mat_display ( const double m[], int actual, int used){… With calls like this: mat_display(&small_ar[0][0], SMALL,4); Copyright © 1998-2014 Curt Hill

Strongly Worded Warning You can lie and make a two dimensioned array look like a singly dimensioned one However, for all assignments in this class: don’t do it Any assignment in the class expects you to pass multiple dimensioned arrays by specifying all the subscripts but the first one! Copyright © 1998-2014 Curt Hill