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.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
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.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Multiple-Subscripted Array
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
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.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
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.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
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.
© 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.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
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.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays.
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
Arrays.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
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.
Module 1: Array ITEI222 - Advance Programming Language.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
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;
Arrays. Arrays are objects that help us organize large amounts of information.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Two-Dimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Two Dimensional Array Mr. Jacobs.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Chapter 8 Multidimensional Arrays
Engineering Problem Solving with C++, Etter/Ingber
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

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 0, 1, 2, 3 or 4 Second dimension: which test score 0, 1, or 2

Declaring a 2D Array Give a second pair of square brackets to tell C++ you want a 2D array Example: int grades[5][3];

Creating a 2D Array Create array elements by telling how many ROWS and COLUMNS Example: int grades[5][3]; grades is a two-dimensional array, with 5 rows and 3 columns. One row for each student. One column for each test. C++ arrays are row major, which means that we always refer to the row first.

Initializing Elements // First student scores grades[0][0] = 78; grades[0][1] = 83; grades[0][2] = 82; Write assignment statements to fill-in the rest of the array.

Declare & Create & Initialize Short Cut Example: int grades[5][3] = { { 78, 83, 82 }, { 90, 88, 94 }, { 71, 73, 78 }, { 97, 96, 95 }, { 89, 93, 90 } }; A Two-D Array is an array of arrays. Each row is itself a One-D array.

Row, Column Indices Give both the ROW and COLUMN indices to pick out an individual element. The fourth student’s third test score is at ROW 3, COLUMN

What are the elements of the array table ? int table[3][4]; x = 1; for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) { table[row][col] = x; x++; } //for col column row

Exercise: Average Overall Find the average test score of all students’ test scores. Use rows to tell you how many rows in the 2D array. Use cols to tell you how many columns in the 2D array.

Exercise: Average Overall int sum = 0; for(int r = 0; r < rows; r++) for(int c = 0; c < cols; c++) sum = sum + grades[r][c]; int avg = sum / (rows*cols);

Write a function that prints out the elements in a 2-D array (as they appear in the matrix)

Exercise: print 2-D array void printArray(int matrix[][]) { for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) cout << (matrix[i][j] << ” “); cout << endl; }

Write a function that returns the maximum element in a 2-D array. Things work pretty much the same way with 3-D arrays (and 4-D and …)

Exercise: find maximum in a 2D array int findMax(int matrix[][COLS], int r, int c) { int max = matrix[0][0]; for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) if ( matrix[i][j] > max) max = matrix[i][j]; return max; }