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.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Computer Science 1620 Loops.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
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.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
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.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
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.
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.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Programming application CC213
Computer Programming BCT 1113
Multidimensional Arrays Vectors of Vectors
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
4.9 Multiple-Subscripted Arrays
Multidimensional Arrays
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Multidimensional Arrays
Arrays Arrays A few types Structures of related data items
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

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 student int grades[4]; store a list of hockey players player players[50]; however, sometimes data has more structure than this

Example: Store the assignment grades for multiple students essentially a 'table' of information Mark 1 Mark 2 Mark 3 Mark 4 Student 1 Student 2 Student 3

Example: Store the assignment grades for 5 different students Solution 1: Store 5 different arrays int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4];

Example: Store the assignment grades for 50 different students Solution 1: Store 50 different arrays int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4]; int grades6[4]; int grades7[4]; int grades8[4]; int grades9[4]; int grades10[4]; int grades11[4]; int grades12[4]; int grades13[4]; int grades14[4]; int grades15[4]; int grades16[4]; int grades17[4]; int grades18[4]; int grades19[4]; int grades20[4]; int grades21[4]; int grades22[4]; int grades23[4]; int grades24[4]; int grades25[4]; int grades26[4]; int grades27[4]; int grades28[4]; int grades29[4]; int grades30[4]; int grades31[4]; int grades32[4]; int grades33[4]; int grades34[4]; int grades35[4]; int grades36[4]; int grades73[4]; int grades38[4]; int grades39[4]; int grades40[4]; int grades41[4]; int grades42[4]; int grades43[4]; int grades44[4]; int grades45[4]; int grades46[4]; int grades47[4]; int grades48[4]; int grades49[4]; int grades50[4];

Multi-Dimensional Arrays Solution 2: 'flatten' the table into one array int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10};

what happens when I want the second mark of the second student? we must translate index in array = row * (# of columns) + column index in array = 1 * (4) + 1 = 5 Multi-Dimensional Arrays int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10};

A Table of Information what do you notice about this declaration? int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4]; grades1, grades2, grades3, grades4, and grades5 all have the same type an array of 4 integers Question: since they all have the same type, can I store them all in the same array? have an array of array of 4 integers?

2D Arrays you can declare an "array of arrays" this creates 3 arrays, all of length 4 when a 2D array is declared, it is often referred to using table syntax each array is often referred to as a row the number of elements in each row refers to the number of columns in the structure int a[3][4];

Declaring a 2D array use two subscript notations first subscript: number of rows second subscript: number of columns int a[3][4]; 3 rows 4 columns

Indexing a 2D array use the same subscript notation as for a single-dimensional array use two! remember: indices always start at 0, not 1 a[1][2] = 17; refers to second row refers to third column

2D Arrays vs. Multiple Arrays store the assignment grades for 50 different students int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4]; int grades6[4]; int grades7[4]; int grades8[4]; int grades9[4]; int grades10[4]; int grades11[4]; int grades12[4]; int grades13[4]; int grades14[4]; int grades15[4]; int grades16[4]; int grades17[4]; int grades18[4]; int grades19[4]; int grades20[4]; int grades21[4]; int grades22[4]; int grades23[4]; int grades24[4]; int grades25[4]; int grades26[4]; int grades27[4]; int grades28[4]; int grades29[4]; int grades30[4]; int grades31[4]; int grades32[4]; int grades33[4]; int grades34[4]; int grades35[4]; int grades36[4]; int grades73[4]; int grades38[4]; int grades39[4]; int grades40[4]; int grades41[4]; int grades42[4]; int grades43[4]; int grades44[4]; int grades45[4]; int grades46[4]; int grades47[4]; int grades48[4]; int grades49[4]; int grades50[4];

2D Arrays vs. Multiple Arrays store the assignment grades for 50 different students int grades[50][4];

2D Arrays vs. 1D Arrays what happens when I want the second mark of the second student? we must translate index in array = row * (# of columns) + column index in array = 1 * (4) + 1 = 5 Multi-Dimensional Arrays int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10};

2D Arrays vs. 1D Arrays what happens when I want the second mark of the second student? with a 2D array, each index is separate Multi-Dimensional Arrays int a[3][4]; // code for initializing a[1][1] = 15; // this sets the 2 nd student's 2 nd mark to

Initializing a 2D array 2D arrays can be initialized just as a 1D array can int one[12] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[3][4] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} }; use braces to separate individual rows row 1row 2row 3 int one[12] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[3][4] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} };

int one[12] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[3][4] = { 18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10 }; Initializing a 2D array inner braces can be omitted: when inner braces omitted, C++ fills table one row at a time, from left to right

Initializing a 2D array the first subscript value can be omitted, just as with single-dimension arrays the second subscript value cannot be omitted int one[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[][4] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} }; int three[][] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} }; Error! Must include column size.

2D arrays and loops our previous array discussions showed strong relationship with loops initialize an index variable to 0 loop up to, but not including, size of array increment index variable by 1 process array element inside loop int a[5]; // some code goes here int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result;

2D arrays and loops to process each element in a 2D array, use a nested loop outer loop: loop through rows inner loop: loop through columns structure of the loops is exactly the same initialize an index variable to 0 loop up to, but not including, size of array increment index variable by 1 process array element inside loop NOTE: this is a guideline, but not the rule

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl;

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Outer Loop: 1. Initialize i to 0

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Outer Loop: 2. Loop up to, but not including, number of rows

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Outer Loop: 3. Increment index by 1

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Each pass through this loop accesses a different row. Now write an inner loop to process each row.

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Inner Loop: 1. Initialize j to 0

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Inner Loop: 2. Loop up to, but not including, size of row (number of columns)

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Inner Loop: 3. Increment j by 1

Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { sum += a[i][j]; } cout >> "Sum = " << sum << endl; Add value to accumulation variable (sum).

2D Arrays and Information Access a 2D array affords us the same advantages as any other table e.g. How would I find the average grade of the first student? sum all elements in first row, divide by 4

Find first student's average int grades[3][4] = {{18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10}}; int sum = 0; for (int j = 0; j < 4; j++) { sum += grades[0][j]; } cout >> "First student average = " << sum/4.0 << endl;

2D Arrays and Information Access a 2D array affords us the same advantages as any other table e.g. How would I find the average grade of the first assignment? sum all elements in first row, divide by 3

Find first student's average int grades[3][4] = {{18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10}}; int sum = 0; for (int i = 0; i < 3; i++) { sum += grades[i][0]; } cout >> "First assignment average = " << sum/3.0 << endl;

2D Arrays as function parameters just like 1D arrays, 2D arrays can be passed as function parameters however, just like in initialization, the column size of the array MUST BE INCLUDED!

Example: given a 2D array, write a function that returns sum of elements in array int sumArray(int a[4][5]) { int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { sum += a[i][j]; } return sum; }

Example: given a 2D array, write a function that returns sum of elements in array int sumArray(int a[][5], int size) { int sum = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < 5; j++) { sum += a[i][j]; } return sum; }

Example: given a 2D array, write a function that returns sum of elements in array int sumArray(int a[][], int size, int size2) { int sum = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size2; j++) { sum += a[i][j]; } return sum; } This won't compile!

Beyond two dimensions you can include as many dimensions as you like int a[3][4][5]; a[1][2][3] = 6; Rules: when initializing arrays, the sizes of all dimensions except the first must be declared explicitly int a[][4][5] = {1, 2, 3 … when passing a multi-dimensional array as a parameter, sizes of all dimensions except the first must be declared!

Example: Write a function that takes a 2D array of integers as a parameter, and prints out the array in 2D format put each number in a column of size

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { Step 1: Print each row in the matrix, following each with a new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { Step 1: For each row in the matrix Step 1.1 Print out row Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { Step 1: For each row in the matrix Step 1.1 Print out row Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 Print out row Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 Print out row Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 for each column in row Step print item at that column Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 for each column in row Step print item at that column Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step print item at that column } Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step print item at that column } Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { cout << setw(5) << a[i][j]; } Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { cout << setw(5) << a[i][j]; } Step 1.2 Print new line }

Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { cout << setw(5) << a[i][j]; } cout << endl; }

Example 2: Write an add function for a 2D array, that adds the two arrays component by component (assume arrays are same size) =

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: Add each item from a and b, place result in C }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: Add each corresponding item from a and b, place result in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each corresponding item in a and b Step 1.1 Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each corresponding item in a and b Step 1.1 Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each row of a (and b) Step 1.1 for each column of a (and b) Step Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each row of a (and b) Step 1.1 for each column of a (and b) Step Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { Step 1.1 for each column of a (and b) Step Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { Step 1.1 for each column of a (and b) Step Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step Place the sum of these items in c's corresponding location }

Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { c[i][j] = a[i][j] + b[i][j]; }