Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Similar documents
Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

(Refer Slide Time: 00:26)

Loops. Repeat after me

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

ENGR 1181 MATLAB 09: For Loops 2

Repetition and Loop Statements Chapter 5

(Refer Slide Time 01:41 min)

Lecture 10. Daily Puzzle

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

CS110D: PROGRAMMING LANGUAGE I

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Programming in C++ PART 2

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #49. Structures in C -1

Iteration. CSE / ENGR 142 Programming I. while loops. Chapter 5. Motivating Loops. Loop to Add 5 Numbers 1996 UW CSE H - 1

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Repetition Structures II

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture Transcript While and Do While Statements in C++

Multiple-Subscripted Arrays

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Unit 3 Decision making, Looping and Arrays

Module 2: Choice and Iteration

n Group of statements that are executed repeatedly while some condition remains true

Decision Making and Loops

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Introduction. C provides two styles of flow control:

Dept. of CSE, IIT KGP

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Iterative Languages. Scoping

Technical Questions. Q 1) What are the key features in C programming language?

REPETITION CONTROL STRUCTURE LOGO

The for Loop. Lesson 11

Lecture 7 Tao Wang 1

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Ch. 7: Control Structures

Fundamentals of Computer Programming Using C

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Loops / Repetition Statements

Lab 09: Advanced SQL

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Chapter Goals. Contents LOOPS

Decision Making in C

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

COMP 208 Computers in Engineering

Programming in OOP/C++

Name: THE SIMPLEX METHOD: STANDARD MAXIMIZATION PROBLEMS

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Recap: Assignment as an Operator CS 112 Introduction to Programming

Shift Register: Exercise # 1: Shift Register Example VI. 1. Build the following front panel. Figure (8.1): Shift register exercise front panel

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Computer Vision. Matlab

CS1150 Principles of Computer Science Loops (Part II)

Flow Control. CSC215 Lecture

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

CS 112 Introduction to Programming

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

EGR 102 Introduction to Engineering Modeling. Lab 10A Nested Programming II Iterative Nesting

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

Looping. Arizona State University 1

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Fundamentals of Programming Session 13

CS100R: Matlab Introduction

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

CS1114: Matlab Introduction

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

IV Unit Second Part STRUCTURES

Unit 3. Operators. School of Science and Technology INTRODUCTION

Loops. CSE 114, Computer Science 1 Stony Brook University

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Lab 10: Alternate Controls

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

6.001 Notes: Section 4.1

Lecture 13: Two- Dimensional Arrays

Learning the Language - V

Solutions to Problem Set 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #19. Loops: Continue Statement Example

Control structures in C. Going beyond sequential

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Transcription:

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem. So, remember that we have seen while loop and we have seen a do while loop. Inside while loops we have written nested loops or double loops. So, let us look at a for loop which problem, where the solution involves a nested loop. (Refer Slide Time: 00:25). So, the for loops are a good choice when the number of iterations is known in advance. So, a good example of such a condition is when you program for matrices, because the dimensions of a matrices are known in advance. So, let us consider a sample problem. So, the first line of the input has a number n now the matrix size is n X n and there are n floating point numbers in the matrix given row by row, each line contains a distinct row. Now, the problem is to compute the trace of the matrix, the trace of the matrix is the sum of the diagonal elements. So, it is defined as. Notice that the matrix row indexing starts from 0, similarly the matrix column indexing also starts from 0.

(Refer Slide Time: 01:34) So, let us write a c program to solve this problem. Now, you should be familiar with how we compute the trace of a matrix. So, for example, if that matrix is given as let say 1 2 3 4 5 6 7 8 9. So, the way we do it by hand is, look at the first row only this element goes into the trace. So, it is trace is 1 +, no other element of the row goes into the trace, in the second row the second element goes into the trace. So, it is 5 + and then no other remaining element goes into that trace and you go to the third row. And the third element goes in to the trace so, 1 + 5 + 9, this is how we do it by hand. You go row by row and then pick out for each row pick some element which goes into that trace only the diagonal element will go into the trace. Let us try to code this. So, in this we have two variables i and j which I will use to iterate over the row indices and the column indices n is the designator for the size of the matrix. For example, the dimension of the matrix is n X n. Now, a is the variable in to which I will read the current entry and then trace is the sum of the diagonal elements seen so far. I assume that it is an integer matrix, it is not general enough you can use a float variable as well. I will first scanf the size of the matrix n, the matrix is of dimension n X n. Once I have done that, here is what I was talking about in the previous slide, once you scan the number n you know that the matrix is n X n. So, the number of times that you are going to iterate is known in advance. So, the number of times that you have to iterate is known before you enter the for loop. In such cases the for loop is more convenient to write than the while loop. So, the outer loop is for each row from i = 0, to i = n excluding i = n you increment the

row. Similarly, for j = 0 to n you increment the column index so, j is supposed to be the column index. Now, you scan the number a now if i = j remember that we wanted to add the only the diagonal elements. So, the diagonal elements will be when the row index is the same as the column index. So, when the row index is the same as the column index, you should add the corresponding numbers to the trace. So, once j becomes n - 1, you will fail the test j < n. So, we will exit out of the inner for loop, and you will go to the outer for loop. In the outer for loop you have i iterating over the row indices. So, you will go to the next row and do the same processing for the next row, until you hit row index n at which point you will exit out of the outer for loop. (Refer Slide Time: 05:23) So, let us look at a sample input let say that you have 1 2 3 1 3 3 and -1 0-1. Here, is a particular convenience that c gives you which I have used in this code. So, notice that this if block I did not put the braces. So, it could have been necessary to put the braces according to the syntax that we have discussed so, far. But if there is only a single statement in the if block then, we do not need to put the braces and it is syntactically correct to do so.

(Refer Slide Time: 06:05) So, let us just run the program on a sample input. So, we have some sample array 2 0-1 1 3 4-1 0 1. So, initially there is this number 3. So, you know that it is a 3 X 3 matrix. So, once you do that you know that n is 3 so, representing that it is a 3 X 3 matrix. So, then you start with i = 0 and go on until i less than n incrementing i by 1 each time i is 0 i < n, because n is 3. So, you enter the outer loop the first statement of the outer loop is itself a for loop, you start with j = 0 j < 3. So, you enter the inner loop you scan a number a, which is a floating point number and if i = j. So, remember that we are looking for diagonal elements. So, we are currently at this point and i = 0 and j = 0. So, we are entering we are scanning the zeroth element of the zeroth column of the zeroth row. So, that element has to go into the trace. So, i = j is true and then you say that trace = trace + a trace was initialize to 0 so, trace becomes now 2. Once you do that, you iterate the inner for loop. So, you go to the updates statement in a inner for loop j becomes j + 1. So, you go to the next column and the j < 3. So, you scan the next number 0 if i = j that is false now, because i is 0 and j =1. So, you do not execute the if statement and go to the update statement. So, j becomes 2, 2 < 3. So, you scan 1 more number which is -1, i is not j. So, you update again j becomes 3, now 3 is not less than 3, so, you exit out of the inner loop. When you exit out of the inner loop there are no more statements to execute. So, you go directly to the update statement in the outer loop which becomes i = i + 1. So, you are reading the first row, row number 1 you are finished reading row number 0. Again you scan the numbers when j = 1 that is the second number in the second row, you will see that i = j, because i is 1 and j =1. So, you will add it to the

trace. So, that is 2 + 3 which is 5. So, trace gets updated and after you do that you scan the remaining entry in the same row, but it does not go to the trace, and then you have done with the row. After that again you go to the outer loop you update the row index of the row index is less is 2 which is less than 3. So, you exit so, you enter the if condition and you execute the inner loop when i = 2 and j = 2 you will find an element which is -1 which will go in to the trace. So, the elements that will be added to that trace are when 2 3 and -1. Once you are done you get out of the inner loop and then you go into the outer loop and update it, but then i becomes 3 it is no longer true that 3 is less than 3. So, you have done reading all the rows. So, you exit the program when you exit the program you have the correct trace which is 4.