Download presentation
Presentation is loading. Please wait.
1
Multi-dimensional Array
Array – Part 2
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]
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.
4
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;
5
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.
6
#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;
7
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;
8
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
9
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
10
STL Vectors Chapter 7 section 11
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.
12
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.