Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

1 Unit 8 'for' Loops

Computer Programming

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

Computer Programming

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

Lecture Transcript While and Do While Statements in C++

Computer Programming

C++ Final Exam 2017/2018

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

Decision Making and Loops

REPETITION CONTROL STRUCTURE LOGO

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Why Is Repetition Needed?

Chapter 17 - Notes Recursion

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)


Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

CS 161 Intro to CS I. Finish Pointers/Start Recursion

CSCE 121 ENGR 112 List of Topics for Exam 1

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

EP241 Computing Programming

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Lecture 7: General Loops (Chapter 7)

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

Other Loop Options EXAMPLE

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Write a program that displays all the even integers between 1 and 100, inclusive

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Quiz Determine the output of the following program:

Introduction to Programming

LAB 4.1 Relational Operators and the if Statement

C/C++ Programming Lecture 7 Name:

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

EP578 Computing for Physicists

Chapter 3 - Functions

Score score < score < score < 65 Score < 50

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

Increment and the While. Class 15

Data Structures and Algorithms

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Lecture 10: for, do, and switch

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Control Structures of C++ Programming (2)

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

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

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Computer Science II Lecture 1 Introduction and Background

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Add Subtract Multiply Divide

Iteration and Arrays Dr. Abdallah Mohamed

CS103L SPRING 2017 UNIT 8: RECURSION

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

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

FORM 2 (Please put your name and form # on the scantron!!!!)

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 7 Functions. As an example, let us write some code to make a virtual cup of coffee.

Local and Global Variables

do { statements } while (condition);

DECISION CONTROL AND LOOPING STATEMENTS

Function with default arguments

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Lectures 20, 21, 22 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

A Generic Iteration Construct General structure of program with iteration Part of program before iteration Iteration initialization (setting up initial values, etc) Iterate/Repeat as long as a logical condition stays true { Block of statements Optional instructions to execute at end of every iteration } Part of program after iteration Loop Body Loop Condition Loop Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 2

while Statement in C++ Part of program before iteration Part of program before execution Iteration initialization (setting up initial values, etc) while (loop condition) { Block of statements Block of statements (Body of while loop) Optional instructions to execute at end of every iteration } Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 3

do while Statement in C++ Part of program before iteration Part Iteration of program initialization before (setting execution up initial values, etc) do { Block of statements Optional Block of instructions statements to execute (Body at of end do-while of every iteration loop) } while (loop condition) Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

for Statement in C++ Semi-colons not to denote end of executable statements But to separate three parts inside for (.. ) Part of program before iteration for (iteration initialization ; loop condition ; instructions to execute at end of every iteration) { Block of statements ( for loop body ) } Part of program after iteration Note absence of semi-colon Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Recap Quiz on Lectures 20, 21, 22 Q1. Why do we need iteration/looping constructs? Why not simply repeat code as many times as we need? A. Possible source of errors B. Sometimes number of iterations is not known C. Simply wasteful, code cannot be reused D. All of the above 6 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Q2. In which of the following looping constructs in C++, the loop must iterate at least once? A. do-while loop B. while loop C. for loop D. None Recap Quiz on Lectures 20, 21, 22 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Q3. In a simple conversion of a do-while loop to a while loop, which of the following are true (more than one may be true)? A. The loop body must be replicated twice B. The loop condition must be replicated twice C. Both A and B Recap Quiz on Lectures 20, 21, 22 D. Either A or B, but not both 8 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Recap Quiz on Lectures 20, 21, 22 Q4. What is the sequence of execution for the following for loop: for(assignment X; condition Y; assignment Z) {loop body W;} A. X -> Y -> Z -> W -> B. X -> Y -> W -> Z -> C. X -> Z -> Y -> W -> D. X -> Z -> W -> Y -> 9 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Recap Quiz on Lectures 20, 21, 22 Q5. Consider the for loop for (x = y; x >= 10 ; x = x+1) { y = y 2; } Suppose we start executing this loop with the values of x and y as 10 and 10, respectively. How many times is (x = y) executed, and how many times is y = y 2 executed? A. 1 and 1, B. 1 and 1, C. 1 and 1, D. 1 and 0 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Practice Problem on Lectures 20, 21, 22 We want to compute the factorial of a non-negative integer using a looping construct in C++. Fill in the missing part of the code below: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Program Skeleton int n; int i;???? factorialn = 1; cout << Give n: ; cin >> n; for (i =???; i < n; i = i+1) {factorialn =?????; } cout << Factorial of << n << is: ; cout << factorialn << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Practice Problem on Lectures 20, 21, 22 We want to calculate the n th power (n 0) of an integer m by a looping program. Complete the missing part of the following code: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Program Skeleton int m, n, i; unsigned long result =????; cout << Give m and n: ; cin >> m >> n; i =????; do {???? } while (i < n); cout << m << raised to << n << is << result; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Practice Problem for Lectures 20, 21, 22 Suppose n is a power of 2, e.g. n = 16. We want to compute the n th power of an integer m by the method of repeated squaring, i.e. n 2 = n x n n 4 = n 2 x n 2 n 8 = n 4 x n 4, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Practice Problem for Lectures 20, 21, 22 Write an iterative program that takes n and m, checks whether n is a power of 2, and if so, computes m n using repeated squaring. Otherwise, the program exits with return code -1. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Program Skeleton cout << Give m and n: ; cin >> m // Check if n is a power of 2 using a loop // If so, get c such that n = 2 c, otherwise // return -1 // Use repeated squaring c times in a loop to // to get m n Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Practice Problem on Lectures 23, 24, 25 Q7. Evaluate the output: int num=0, i; for(i = 0; i < 10; i++) { if(i%2 == 0) {continue;} } else {cout<< num += i <<, ;} Options: A. 1,4,9,16,25 B. 2,4,6,8,10 C. 1,3,5,7,9 D. 1,5,11,15,19 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

Quiz on Lectures 23, 24, 25 Q5. Identify all the incorrect statement(s) from the following: A. Break statement can be used in all looping constructs B. Missing loop condition of for loop necessarily makes the loop iterate infinitely many times C. for(i=0,j=0; i<10; i++,j--) is a valid form of the for header D. In C++, assignment can never serve as an expression 19 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Quiz on Lectures 23, 24, 25 Q6. What will be printed on the screen on executing the following statements if the value of x is 10 and that of y is 2? cout << x++ <<, << --y <<, ; cout << x+y; A. 11,1,12 B. 10,1,12 C. 10,2,13 D. 11,2,11 20 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,