0% found this document useful (0 votes)
4 views8 pages

Matrices

Uploaded by

Douda Samih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Matrices

Uploaded by

Douda Samih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Matrices

Arrays of Arrays
(And, yes, you can have:
arrays of arrays of arrays, and
arrays of arrays of arrays of arrays, and
arrays of arrays of arrays of arrays of
arrays, and….)
Arrays (review):

→ A data structure (yep!! Your first data structure!)

→ Characteristics:
→ Sequential (one item after another one)
→ So there’s an order to the items – a first one, a 32
second one, a third one, etc.
→ It consists of one type
→ So all the items are ints, or all are strings, or all
are floats, or all are Boolean values, etc.
→ It has a size (i.e., it has a specific number of items)
→ To create: int arr_var[10];
→ To put a value in: arr_var[8] = 32;
Array of Arrays: Matrix!!!
(Did not get those movies…)

→ You can create an array of arrays!

→ E.g., int mat_var[3][5];


→ This creates 3 arrays, each 5 ints in length. (See pic, right)
→ Yes, the first number is the number of rows and the second is, in
essence the number of columns. (counter-intuitive)
→ Yes, you COULD have each of the rows have different column length
(we’re not dealing with that yet)
→ No, the arrays can’t be of different types.
→ That’s python. We’re not python.
Adding values to a matrix:
0 1 2 3 4

0
→ Row first, then column:
1 42
→ mat_var[1][3] = 42;
2

0 1 2 3 4

You can use a loop to fill a row: 0

for (int j = 0; j < 5; j++) { 1 0 1 4 9 16


mat_var[1][j] = j*j;
2
}
Adding values to a matrix:
0 1 2 3 4
You can fill each row in the matrix:
for (int j = 0; j < 5; j++) {
0 0 1 2 3 4
mat_var[0][j] = j;
}
for (int j = 0; j < 5; j++) {
1 0 2 4 6 8
mat_var[1][j] = j*2;
} 2 0 1 4 9 16
for (int j = 0; j < 5; j++) {
mat_var[2][j] = j*j;
}
Better: Loop inside of a loop: 0 1 2 3 4
→ Outer loop: goes through each row
0 0 1 2 3 4
→ Inner loop – goes through each index (column) in the row

for (int i = 0; i< 3; i++) { 1 0 2 4 6 8


for (int j = 0; j < 5; j++) {
2 0 3 6 9 12
mat_var[i][j] = j *( i+1); // i is row, j is column
}
}
Printing out a matrix:
To print one row: 0 1 2 3 4
for (int j = 0; j < 5; j++) {
0 0 1 2 3 4
cout << mat_var[1][j] <<“, “;
} 1 0 2 4 6 8
cout << endl;
2 0 1 4 9 16
Note that the first bracket value is the row.
What would be printed is: 0, 2, 4, 6, 8,
To print entire matrix:
for (int i = 0; i< 3; i++) { // loops through each row
for (int j = 0; j < 5; j++) { // loops through each index in a row
cout << mat_var[i][j] << “ “; // i is row, j is column 0 1 2 3 4
}
0 0 1 2 3 4
cout << endl;
}
1 0 2 4 6 8
Prints out:
0 1 2 3 4 2 0 3 6 9 12
0 2 4 6 8
An example
(not a terribly useful one, but it still might help to see an example…)
char mat_var[5][9];
for (int i = 0; i< 5; i++) { // loops through each row
for (int j = 0; j < 9; j++) { // loops through each index in a row
if ((j == i) || ((8-i) == j)) {
mat_var[i][j] = '*'; // again, i is row, j is column
}
else {
mat_var[i][j] = '-';
}
cout << endl; Prints:
} * - - - - - - - *
}
- * - - - - - * -
for (int i = 0; i< 5; i++) { // loops through each row
for (int j = 0; j <9; j++) { // loops through each index in a row - - * - - - * - -
cout << mat_var[i][j] << " "; // again, i is row, j is column - - - * - * - - -
}
- - - - * - - - -
cout << endl;
}
Take-aways

→ A Matrix is an array in which each index in the array is also an array.

→ To declare: int mat[4][7]; // where 4 is the number of rows and 7 is the number of values in each row.

→ To access a value in a matrix:


cout << mat[2][4] << endl; // 2 is the row and 4 is the value
mat[3][1] = 22; // puts 22 in row 3, column 1 (remember, both start at index 0)

→ To loop through entire matrix:


for (int i = 0; i < 4; i++) { // loops through each row
for (int j = 0; j < 7; j++) { // loops through each column, or index in the row
cout << mat[i][j] << “, “;
}
cout << endl;
}

You might also like