1020: Introduction to Programming Mohamed Shehata November 22, 2017

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
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.
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.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
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.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
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.
Two-Dimensional Arrays Lesson xx
For loops, nested loops and scopes
A Lecture for the c++ Course
MULTI-DIMENSIONAL ARRAY
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Multi-dimensional Array
Passing Arguments to a Function
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Counted Loops.
Pointers Psst… over there.
Pointers Psst… over there.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Engineering Problem Solving with C++, Etter/Ingber
While Loop Design ENGI 1020 Fall 2018.
Chapter 13 Vector of Vectors (2D Arrays)
CSE 1342 Programming Concepts
Pointers & Functions.
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Functions Pass By Value Pass by Reference
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional array
Multidimensional Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
CHAPTER 2 Arrays and Vectors.
EECE.2160 ECE Application Programming
Lets Play with arrays Singh Tripty
EECE.2160 ECE Application Programming
CHAPTER 2 Arrays and Vectors.
Managing 2D Arrays Simple Dynamic Allocation
Pointers & Functions.
EECE.2160 ECE Application Programming
C++ Array 1.
Chapter 4 Repetition Structures
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

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

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

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

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

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

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

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 matrix 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 matrix 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

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