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

Similar documents
CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

CS101 Computer programming and utilization

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

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.

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

C++ Programming: From Problem Analysis to Program Design, Third Edition

Computer Programming

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

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

Computer Programming

Computer Programming

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

2 nd Week Lecture Notes

Computer Programming

Computer Programming

Fundamentals of Programming CS-110. Lecture 2

do { statements } while (condition);

Computer Programming : C++

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Increment and the While. Class 15

CSc Introduction to Computing

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

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

C++ basics Getting started with, and Data Types.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Computer Programming

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Unit 7. 'while' Loops

Computer Programming

REVIEW. The C++ Programming Language. CS 151 Review #2

VARIABLES & ASSIGNMENTS

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

If Control Construct

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Ch 6. Functions. Example: function calls function

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Programming in C++ PART 2

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Scientific Computing

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Introduction to Programming EC-105. Lecture 2

Review. Relational Operators. The if Statement. CS 151 Review #4

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lab Instructor : Jean Lai

5. Selection: If and Switch Controls

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Computer Programming

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

BITG 1233: Introduction to C++

Computer Programming

J. P. Cohoon and J. W. Davidson 1999 McGraw-Hill, Inc. Control Constructs. Mechanisms for deciding when and how often an action should be taken

Relational Operators and if. Class 10

Programming Language. Functions. Eng. Anis Nazer First Semester

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Boolean Algebra Boolean Algebra

Chapter 4 - Notes Control Structures I (Selection)

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Data Structures and Algorithms

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Computer Programming

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Why Is Repetition Needed?

Lab # 02. Basic Elements of C++ _ Part1

A First Program - Greeting.cpp

Practice test for midterm 1

Computer Science II Lecture 1 Introduction and Background

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Computer Programming

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

Computer Programming

CS101 PLEDGED SPRING 2001

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

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

Computer Programming

Understanding main() function Input/Output Streams

Transcription:

CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Lecture 4, Conditional execution of instructions Friday, August 5, 2011

Overview Computational problems requiring conditional evaluation Flow charts Basic control structures in C++ for conditional execution if statement if else structure Conditional expressions if else if ladder structure switch statement Labs Next week Lectures next week General announcement 2

Flow charts Flow charts are used to graphically show the sequence of execution of our instructions. Consider the following program segment for adding two numbers cin >> a; cin >> b; sum = a+b; cout << sum; 3

Flow chart for adding two numbers Input a Input b sum = a + b Output sum 4

Flow chart... It is useful to graphically show the sequence of execution of our instructions. Flow charts are often used for this purpose Input a Input b sum = a + b Output sum In flow charts, we do not have to write instructions in c++, (although we can) Purpose is to show the flow of control of the algorithm of our programs 5

A simple computational problem Starting from a point, a person walks at a steady speed of 2 Km per hour for 1 hour. Write a program to determine the distance traveled by the person in some given time t. Distance Time Formula for calculating distance, is clearly: d = 2 * t 6

C++ program to solve the problem /* Program to calculate distance travelled in a given time CS101, Autumn 2011-12*/ #include <iostream> using namespace std; int main() { float time, distance; cout << give value of time\n ; cin >> time; distance = 2.0 * time; cout << Distance travelled is: << distance << endl; return 0; } 7

A problem requiring conditional execution Starting from a point, a person walks at a steady speed of 2 Km per hour for 1 hour. The person then starts running at a speed of 4 Km per hour. He continues to run for 2 hours and then stops. Write a program to determine the distance traveled by the person in some given time t. Distance Time 8

Another problem requiring conditional execution Cost of a ticket for a bus journey between two towns Rs 25.50. However, for a child of age less than 12 years, it is Rs 12.75. Given the age of the passenger, write a program to calculate the cost of a ticket. int age; float ticket_cost; cout << give age of the passenger: ; cin >> age; How do we write instruction to calculate the correct cost? 9

Cost of a ticket... In the case of the problem of computing cost of ticket, we want to instruct the machine, to calculate as follows: If the age is less than 12 years then calculate the cost of ticket as Rs 12.75 else Calculate the cost of the ticket as Rs 25.5 In our program, We need to execute either the first or the second assignment operation based on the value of age, but never both 10

Cost of a ticket... Thus, for computing the cost of ticket, we do not want to execute our instructions in a simple sequence We first want to the machine to examine the value of age. Then depending on whether the age is < 12 or not, We want the computer to execute either the first or the second instruction but never both This conditional execution of either the first or the second assignment is possible using the if instruction of c++ 11

Flow chart for calculating ticket_cost (tc) cin >> age False age < 12? True tc = 25.50 tc = 12.75 cout << tc 12

if statement in C++ C++ provides a control structure to enforce conditional execution General format if (condition) statement1; else statement2; Condition in parentheses must be a valid comparison of values (count <= N) (new_value old_value < 1.0E-5) (age >= 12) (age < 12) First, the condition is evaluated as either true or false If the condition is true, statement1 is executed If the condition is false, statement2 is executed Both statements are never executed 13

General flow chart for an if statement 14

Bus ticket problem if (age < 12) ticket_cost = 12.75; else ticket_cost = 25.50; 15

Multiple actions within any true-false branch We should always use a C++ block Any set of c++ statements, enclosed in a pair of braces { } constitute a block, logically equivalent to a single statement Wherever we can write a statement, a block can be written 16

C++ Program // Program to calculate cost of bus ticket #include <iostream> using namespace std; int main() { int age; float ticket_cost; cout << give age of passenger: ; cin >> age; if (age < 12) {ticket_cost = 12.75}; else {ticket_cost = 25.50}; cout << cost of ticket is << ticket_cost; return 0; } 17

C++ Program // Program to calculate cost of bus ticket #include <iostream> using namespace std; int main() { int age; float ticket_cost; cout << give age of passenger: ; cin >> age; if (age >= 12) {ticket_cost = 25.5;} else {ticket_cost = 12.75;} cout << cost of ticket is << ticket_cost << endl; return 0; } 18

if else if ladder If additional Concession available to old people ( age > 60) Cost of ticket for them is Rs 20 Flow chart: 19

Modified program int age; float ticket_cost; cout << give age ; cin >> age; if (age > 60) {ticket_cost = 20;} else if (age >= 12) {ticket_cost = 25.50;} else {ticket_cost = 12.75;} cout << cost is << ticket_cost; 20

Alternate version of the program int age; float ticket_cost; cout << give age ; cin >> age; If (age < 12) {ticket_cost = 12.75;} else if (age < 60) {ticket_cost = 25.50;} else {ticket_cost = 20.00;} cout << cost is << ticket_cost; return 0; 21

The two versions int age; float ticket_cost; cout << give age ; cin >> age; if (age > 60) {ticket_cost = 20;} else if (age >= 12) {ticket_cost = 25.50;} else {ticket_cost = 12.75;} cout << cost is << ticket_cost; return 0; int age; float ticket_cost; cout << give age ; cin >> age; If (age < 12) {ticket_cost = 12.75;} else if (age < 60) else {ticket_cost = 25.50;} {ticket_cost = 20.00;} cout << cost is << ticket_cost; return 0; 22

Finding maximum of two numbers Given two integer numbers a and b, find the maximum of these Flow chart: 23

Program to find maximum of two numbers int a, b, max; cin >> a >> b; if (a > b) {max = a;} else {max = b;} cout << max; 24

Alternate logic 25

Two versions of program to find maximum // Version I int a, b, max; cin >> a >> b; if (a > b) {max = a;} else {max = b;} cout << max; // Version II int a, b, max; cin >> a >> b; max = a; if (b > max) {max = b;} cout << max; 26

Finding maximum of three numbers Given 3 integer numbers a, b, and c, find the maximum of these 27

Program // Program to find maximum of 3 given numbers int a, b, c, max; cin >> a >> b >> c; if (a > b) { if (b > c) { max = a;} else {if (a > c) {max = a;} else {max = c;} } } else { if (b > c) {max = b;} else { max = c;} } cout << max; 28

Maximum of three numbers alternate logic 29

Two program versions to find maximum of 3 numbers // Version I int a, b, c, max; cin >> a >> b >> c; if (a > b) { if (b > c) { max = a;} else {if (a > c) {max = a;} else {max = c;} } } else { if (b > c) {max = b;} else { max = c;} } cout << max; // Version II int a, b, c, max; cin >> a >> b >> c; max = a; if (b > max) {max = b;} if (c > max) {max = c;} cout << max; 30

Finding maximum of 5 numbers int a, b, c, d, e, max; cin >> a >> b >> c >> d >> e; max = a; if (b > max) {max = b;} if (c > max) {max = c;} if (d > max) {max = d;} if (e > max) {max = e;} cout << max; 31

Conditional expression A comparison of two values results in true or false These are called logical or Boolean values A comparion operation is also treated as an expression Evaluates to true or false C ++ has a special data type called bool C++ has a numerical equivalent for these two logical values false is converted to value 0 true is converted to value 1 32

Conditional expressions a op b where op is <, >, <=, >=, ==,!= == is equal to!= is not equal to Conditions can be combined: (a > 0) && (a <= 9) : true if a is a positive digit. && : conjunction. and : disjunction. or! : negation. not 33

Examples of conditions 34

The switch statement In our algorithms, we need to branch out to one of multiple different instructions. Branching decision is based on the value of an expression 35

Switch statement switch (expression){ case constant1 : statement sequence 1; break; case constant2 : statement sequence 2; break; - - default: statement sequence; } 36

A problem suited for use of switch 37

38