1020: Introduction to Programming Mohamed Shehata November 7, 2016

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Computer Programming -1-
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Lecture 11 Multi-dimensional Arrays
I/O Streams File I/O 2-D array review
REPETITION CONTROL STRUCTURE
EGR 2261 Unit 10 Two-dimensional Arrays
Two Dimensional Array Mr. Jacobs.
For loops, nested loops and scopes
A Lecture for the c++ Course
MULTI-DIMENSIONAL ARRAY
Multi-dimensional Array
Passing Arguments to a Function
( Iteration / Repetition / Looping )
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Counted Loops.
Lecture 4B More Repetition Richard Gesick
FOR LOOPS.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Engineering Problem Solving with C++, Etter/Ingber
CS1201: Programming Language 2
While Loop Design ENGI 1020 Fall 2018.
Programming II Pointers and arrays by aalosaimi.
1020: Introduction to Programming Mohamed Shehata November 22, 2017
Loops A loop is a repetition control structure.
CSE 1342 Programming Concepts
Pointers & Functions.
Control Structures Part 2
Let’s all Repeat Together
Multidimensional Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS1201: Programming Language 2
CHAPTER 2 Arrays and Vectors.
CHAPTER 2 Arrays and Vectors.
Suggested self-checks:
Pointers & Functions.
CS31 Discussion 1D Winter19: week 4
Chapter 4 Repetition Structures
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Programming Fundamental
Presentation transcript:

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

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

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.

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? 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50

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: * ** *** **** ***** ******

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)

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

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

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

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;

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

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

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

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

#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

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.

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;

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

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