Multi-dimensional Array

Slides:



Advertisements
Similar presentations
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Advertisements

 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
C++ for Engineers and Scientists Third Edition
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
ITEC 320 C++ Examples.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
1 C++ Classes and Data Structures Course link…..
Chapter 4 Repetition Structures
Review 1.
I/O Streams File I/O 2-D array review
MT262A Review.
Programming application CC213
Chapter 7 – Arrays.
multi-dimensional arrays
Command Line Arguments
Chapter 5 Function Basics
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Engineering Problem Solving with C++, Etter
Programming fundamentals 2 Chapter 1:Array
Arrays Part-1 Armen Keshishian.
Introduction to C++ October 2, 2017.
New Structure Recall “average.cpp” program
Chapter 7: Arrays.
C++ Arrays.
לולאות קרן כליף.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 2 Elementary Programming
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
Data type List Definition:
Starting Out with C++: From Control Structures through Objects
Chapter 8 – Searching and Sorting Arrays
Pass by Reference.
Starting Out with Programming Logic & Design
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Multidimensional Arrays
Let’s all Repeat Together
Multidimensional Arrays
Arrays of Two-Dimensions
CHAPTER 2 Arrays and Vectors.
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
CS31 Discussion 1D Winter19: week 4
Chapter 4 Repetition Structures
Tic-Tac-Toe Game Engine
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
4.1 Introduction Arrays A few types Structures of related data items
Data Structures & Programming
Presentation transcript:

Multi-dimensional Array Array – Part 2

Two-dimensional Array A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets. double scores[3][4]; scores[0][0] scores[0][1] scores[0][2] scores[0][3] scores[1][0] scores[1][1] scores[1][2] scores[1][3] scores[2][0] scores[2][1] scores[2][2] scores[2][3]

Accessing to two-dimensional array’s elements The best way is to use a nested loops. Best practices: Outer loop traces rows and inner loop traces columns Example: Write a program that asks user to insert 12 different numbers into a two-dimensional 3 by 4 array and shows all elements.

int main() { int a[3][4]; for (int i = 0 ; i < 3; i++) { for (int j = 0 ; j < 4; j++) { cout << "insert a value for item at row " << i + 1 << ", column " << j + 1 << ": "; cin >> a[i][j]; } cout << a[i][j] << " "; cout <<endl; system("pause"); return 0;

Passing two-dimensional arrays to functions When a two-dimensional array is passed to a function, the parameter type must contain a size declarator for the number of columns. Example: write a program that uses a function to show all elements in a two-dimensional array.

#include <iostream> using namespace std; const int COLS = 4; //number of columns void displayTwoDimensionalArray(int numbers[][COLS], int rowSize); int main(int argc, const char * argv[]) { int a[3][COLS]; for (int i = 0 ; i < 3; i++) { for (int j = 0 ; j < COLS; j++) { cout << "insert a value for item at row " << i + 1 << ", column " << j + 1 << ": "; cin >> a[i][j]; } displayTwoDimensionalArray(a, 3); system("pause"); return 0; void displayTwoDimensionalArray(int numbers[][COLS], int rowSize){ for (int i = 0 ; i < rowSize; i++) { cout << numbers[i][j] << " "; cout <<endl;

Summing all the elements of a two-dimensional array #include <iostream> using namespace std; const int COLS = 4; //number of columns void displayTwoDimensionalArray(int numbers[][COLS], int rowSize); int total(int numbers[][COLS], int rowSize); int main(int argc, const char * argv[]) { int a[3][COLS]; for (int i = 0 ; i < 3; i++) { for (int j = 0 ; j < COLS; j++) { cout << "insert a value for item at row " << i + 1 << ", column " << j + 1 << ": "; cin >> a[i][j]; } int sumOfAllElements = total(a, 3); cout << "Sume = " << sumOfAllElements << endl; system("pause"); return 0; int total(int numbers[][COLS], int rowSize){ int sum = 0; for (int i = 0 ; i < rowSize; i++) { sum += numbers[i][j]; return sum;

Multi-dimensional arrays Three-dimensional array int employeeInfo[5][4][7]; 5 employees Each employee has 4 weeks of work Each week has 7 days

Day 1 Day 2 Day 3 Day 4 Day 5 Week 1 Day 1 Day 2 Week 2 Day 3 Week 3 Day 4 Employee 1 Week 4 Day 5 Day 1 … Day 5 Day 1 … Day 5

STL Vectors Chapter 7 section 11

Homework twodimensionalfuncs Write a program that creates a two dimensional array initialized with users input. The program should have the following functions, and should ask user to insert a number to call each function. If that function required an extra value (3,4, and 5) then it should ask the user for the second number. Then it should call the function and shows the output. then it should ask user if he/she wants to continue, if users inserts ‘y’ then the program should ask user to select another menu item (no array initialization) and continue this process until user inserts ‘n’. getTotal: This function should accept a two-dimensional array as its argument and return the total of all the values in the array. getAverage: This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be subscript of a row in the array. The function should return total of the values in the specified row. getColumnTotal: This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be subscript of a column in the array. The function should return the total of the values in the specified column. getHighestInRow: This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the highest value in the specified row of the array. contains: This function should accept a two-dimensional array as its first argument and an integer as its second argument. If the array contains the second argument it should return true, else it should return false.

Homework Tic-Tac-Tow Game Extra Credit Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initizlized with an asterisk (*). The program should run a loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. Show the new content of the array. Allows player 2 to select a location on the board for an O. The program should ask the user to enter row and column number. Show the new content of the array. Determines whether a player has won, or a tie has occurred. If a player has won the program should declare that player the winner and end. If a tie has occurred, the program should say so and end.