Presentation is loading. Please wait.

Presentation is loading. Please wait.

1020: Introduction to Programming Mohamed Shehata November 22, 2017

Similar presentations


Presentation on theme: "1020: Introduction to Programming Mohamed Shehata November 22, 2017"— Presentation transcript:

1 1020: Introduction to Programming Mohamed Shehata November 22, 2017
Matrices 1020: Introduction to Programming Mohamed Shehata November 22, 2017

2 Recall: Nested for loops
A for loop can contain any kind of statement in its body, including another for loop. The inner loop must have a different name for its loop counter variable so that it will not conflict with the outer loop. nested loop: Loops placed inside one another, creating a loop of loops. for(init loop1; cond loop1; update loop1) { body of loop1 } Another for loop

3 Example What is the output of the following nested for loop?
for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { cout<<"*"; } cout<<endln; Output: * ** *** **** ***** ******

4 2D matrices Assume we have a 2D matrix called date[r,c]
c starts from begin to end (0 to total num of columns-1) r r,c 0,0 0,1 0,2 0,3 0,4 0,5 1,0 1,1 1,2 1,3 1,4 1,5 2,0 2,1 2,2 2,3 2,4 2,5 r,c r,c Assume we have a 2D matrix called date[r,c] Let’s write a pseudo code to traverse the matrix (i.e., access each element in it in an ordered manner) The matrix has total number of rows = rows and total number of columns=cols

5 for ( int i=0 ; i<rows; i++) for(int j=0; j<cols; j++) print on the screen data[i,j]; Notice here that I used here data[i,j] to indicate the elements in the matrix data

6 Matrices Can we do multi-dimensional arrays in C++?
Yes but: It is complicated beyond the scope of this class More efficient and faster to deal with 1D array Ultimately, we can convert a 2D matrix to 1D array using simple techniques

7 Nested for loops and arrays
This matrix is 4 rows of 3 columns each. By simply slicing the matrix into rows and arranging them one after the other in a long row, we have a 1D array again. rows =4 cols =3

8

9 We can also go the other way
We can also go the other way. Given an element with position p in the 1D array, we can compute its position (i,j) in the 2D matrix as follows: i = p / cols; j = p % cols;

10 Moving Forward What we want to do is to be able to store the data in a 1D array but still be able to treat it as a 2D matrix using nested for-loops

11 c starts from begin to end
(0 to total num of columns-1) 2D array example revisited r r,c 0,0 0,1 0,2 0,3 0,4 0,5 1,0 1,1 1,2 1,3 1,4 1,5 2,0 2,1 2,2 2,3 2,4 2,5 We need to traverse the array (i.e., access each element in it in an ordered manner) r,c r,c The purple block starts at the beginning and then for each row, the purple block traverse all the elements in that row in an order from column 0 to total num of columns-1

12 We need to loop over all the rows (outer loop) such that for each row (a pass in the outer loop), we will loop over all the columns in that row (inner loop). for (int i = 0; i < sRows; i++) { for (int j = 0; j < sCols; j++) { do something with array[i,j] } 0,j 0,0 0,1 0,2 0,3 0,4 0,5 1,0 1,1 1,2 1,3 1,4 1,5 2,0 2,1 2,2 2,3 2,4 2,5 r,c r,c

13 Exercise Let’s write a function to print the output of the 1D array on the screen formatted as 2D

14 #include <iostream> using namespace std; void printArray(int pic[], int rows, int cols); int main(){ int rows=4; int cols=3; int myArr[12]={4,7,9,-6,-3,0,11,2,17,-11,14,-8}; printArray(myArr, rows, cols); return 0; } void printArray(int pic[], int rows, int cols){ for(int r=0;r<rows;r++){ for(int c=0; c<cols; c++){ int pos_1D_array = r*cols+c; cout << pic[pos_1D_array]<<" "; cout<< endl; // go to new line at the end of the row

15 Example Write a program to scale every element of a 2D matrix by a constant void scale(double matrix[], int rows, int cols, double scale){ for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) matrix[r*cols + c] *= scale; }

16 Example Write a program to add the elements of matrix m2 to the equivalent elements of matrix m1 and store the result in m1 void add(double m1[], double m2[], int rows, int cols){ for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) m1[r*cols + c] += m2[r*cols + c]; }


Download ppt "1020: Introduction to Programming Mohamed Shehata November 22, 2017"

Similar presentations


Ads by Google