Informatics 2 - 7,8,9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Lesson 7, 8, 9

Two-dimensional arrays.

Tablice dwuwymiarowe
The Agenda
1. Multidimensional arrays
2. Two-dimensional arrays
3. Initializing and displaying two-dimensional arrays
4. Examples
5. Dynamic two-dimensional arrays
6. Examples
Multidimensional arrays
Multidimensional arrays can be described as "arrays of arrays".

For example, a bidimensional array can be imagined as a bidimensional table


made of elements, all of them of a same uniform data type.

jimmy represents a bidimensional array of 3 per 5 elements of type int. The way
to declare this array in C++ would be:
int jimmy[3][5];
For example, the way to reference the second element vertically and fourth
horizontally in an expression would be:
jimmy[1][3];

Remember that array indices always begin by zero.


Multidimensional arrays
Multidimensional arrays are not limited to two indices (i.e., two dimensions).
They can contain as many indices as needed. But be careful! The amount of
memory needed for an array rapidly increases with each dimension. For
example:
char century[100][365][24][60][60];

Declares an array with a char element for each second in a century, that is more
than 3 billion chars. So this declaration would consume more than 3 gigabytes of
memory!
Two-dimensional arrays
Multidimensional arrays are just an abstraction for programmers, since we can
obtain the same results with a simple array just by putting a factor between its
indices:
int jimmy[3][5]; //is equivalent to
int jimmy[15]; //(3*5=15)
Two-dimensional arrays
0 1 2 3 4

0 e1 e2 e3 e4 e5

1 e6 e7 e8 e9 e10

2 e11 e12 e13 e14 e15

Pseudo-multidimensional array
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12 e13 e14 e15


Initializing and displaying two-dimensional arrays
1. #include <iostream>
2. using namespace std;
3. int main() {
4. // Initializing a 3x3 two-dimensional array with random values
5. int twoDArray[3][3] = {
6. {1, 2, 3},
7. {4, 5, 6},
8. {7, 8, 9}
9. };
10. // Accessing and displaying the elements of the two-dimensional array
11. for (int i = 0; i < 3; i++)
12. {
13. for (int j = 0; j < 3; j++)
14. {
15. cout << twoDArray[i][j] << " ";
16. }
17. cout << endl;
18. }
19. return 0;
20. }
Problems
1. You are given a 4x5 two-dimensional integer array representing the scores
of students in a class. Each row represents a student, and each column
represents a subject. Write a C++ program to calculate and display the
average score of each student and the average score of each subject.
2. You are given a 3x3 two-dimensional integer array representing a matrix.
Write a C++ program to calculate and display the sum of all the elements in
the matrix.
3. You are given a 4x4 two-dimensional integer array representing a tic-tac-
toe board, where each cell can contain one of three values: 0 (empty), 1
(player X), or 2 (player O). Write a C++ program to check if any player has
won the game. Your program should take the 4x4 array as input and
output the winner (if any) or "No winner" if the game is still ongoing.
Problems
1. You are given a 5x5 two-dimensional integer array representing a Sudoku
puzzle. Each cell can contain a number from 1 to 9, or 0 to represent an
empty cell. Write a C++ program to check if the given Sudoku puzzle is valid
according to the Sudoku rules.
Sudoku Rules:
Each row must contain all the digits from 1 to 9 without repetition.
Each column must contain all the digits from 1 to 9 without repetition.
Each of the nine 3x3 sub-grids (also known as boxes) must contain all the
digits from 1 to 9 without repetition.
Homework
1. Write a C++ program to find the maximum element in a 3x3 two-dimensional
integer array.
2. Write a C++ program to transpose a 3x3 two-dimensional integer array. The
transpose of a matrix is obtained by swapping its rows with its columns.
3. Write a C++ program that takes a 4x4 two-dimensional integer array
representing a square matrix as input and checks if the matrix is symmetric. A
square matrix is symmetric if it remains unchanged when transposed.
4. Write a C++ program that takes a 3x3 two-dimensional integer array as input and
checks if the array forms a magic square. A magic square is a square matrix in
which the sum of each row, each column, and both main diagonals are the same.
2 9 4
7 5 3 It is a magic square matrix
6 1 8
Dynamic two-dimensional arrays
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;

cout << "Enter the number of columns: ";


cin >> cols;

// Allocating memory for the array of pointers to rows


int** dynamicArray = new int*[rows];

// Allocating memory for each row


for (int i = 0; i < rows; i++) {
dynamicArray[i] = new int[cols];
}
Dynamic two-dimensional arrays
// Inputting data into the array
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> dynamicArray[i][j];
}
}

// Displaying the elements of the array


cout << "The elements of the array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << dynamicArray[i][j] << " ";
}
cout << endl;
}
Dynamic two-dimensional arrays
// Freeing the memory occupied by the array
for (int i = 0; i < rows; i++) {
delete[] dynamicArray[i];
}
delete[] dynamicArray;

return 0;
}

You might also like