Presentation is loading. Please wait.

Presentation is loading. Please wait.

1020: Introduction to Programming Mohamed Shehata November 7, 2016

Similar presentations


Presentation on theme: "1020: Introduction to Programming Mohamed Shehata November 7, 2016"— Presentation transcript:

1 1020: Introduction to Programming Mohamed Shehata November 7, 2016
Nested For Loops 1020: Introduction to Programming Mohamed Shehata November 7, 2016

2 Nested for loops Another for loop
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 for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) { cout<<“*"; } Output: ****** All of the statements in the outer loop's body are executed 3 times ( i starts at 0 and ends when I reaches 3). The inner loop runs 2 times for each of those 3 times, for a total of 6 stars printed.

4 More nested for loops All of the statements in the outer loop's body are executed 5 times. The inner loop runs 10 times for each of those 5 times, for a total of 50 numbers printed. for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { cout<<(i * j); cout<< " "; } cout<<endl; // to make new line What is the output?

5 Nested for loop exercise
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: * ** *** **** ***** ******

6 2D matrices Assume we have a 2D array 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 array called date[r,c] Let’s write a pseudo code to traverse the array We need to traverse the array (i.e., access each element in it in an ordered manner)

7 for ( i=0 ; i<rows; i++) for(j=0; j<cols; j++) print data[i,j]

8 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 array to 1D array using simple techniques

9 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

10

11 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 array as follows: i = p / cols; j = p % cols;

12 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 array using nested for-loops

13 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

14 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

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

16 #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

17 Common nested loop bugs
It is a common bug to accidentally type the wrong loop counter variable, which can lead to incorrect behavior. What is the output of the following nested loops? for (int i = 1; i <= 10; i++) { for (int j = 1; i <= 5; j++) { cout<<j; } cout<<endl; for (int j = 1; j <= 5; i++) { Both cases produce infinite loops.

18 Forgetting the block brackets
What is the output of the following nested loops? for (int i = 1; i <= 10; i++) for (int j = 1; j <= 5; j++) { cout<<j; } cout<<endl;

19 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; }

20 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 7, 2016"

Similar presentations


Ads by Google