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

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

Computer Programming

Computer Programming

Computer Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Lab Instructor : Jean Lai

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

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

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

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

Module 07: Programming in C++

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

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

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

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

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

Increment and the While. Class 15

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

Computer Programming

do { statements } while (condition);

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Introduction to Programming EC-105. Lecture 2

Fundamentals of Programming CS-110. Lecture 2

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Lecture 5. Announcements: Today: Finish up functions in MIPS

Computer Programming


Programming Language. Functions. Eng. Anis Nazer First Semester

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Problem Solving: Storyboards for User Interaction

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

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

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

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Functions in MIPS. Functions in MIPS 1

Lab 8 The Stack (LIFO) Structure

Function Call Example

Data Structures and Algorithms

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

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Lecture #9 **go to Blackboard ** Review from Shackelford Reading If statements Loops. Using if statements and loops

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

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

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.

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

CSC 2400: Computer Systems. Using the Stack for Function Calls

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS2141 Software Development using C/C++ C++ Basics

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

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

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

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

Unit 7. 'while' Loops

Today. Putting it all together

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

1 Unit 8 'for' Loops

Week 3: Pointers (Part 2)

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

EECS402 Lecture 02. Functions. Function Prototype

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Review Questions for Final Exam

CS 106B Lecture 2: C++ Functions

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

Exception with arguments

Local and Global Variables

Chapter 10 - Notes Applications of Arrays

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Lecture 10: for, do, and switch

Sample Code: OUTPUT Daily Highs & Lows

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

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Function Calls and Parameter Passing Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Overall Program Structure #include <iostream> using namespace std; int myencode(int q1marks,int q2marks); int power(int base, int exponent); int main() { for ( ) { cipher = myencode(q1marks, q2marks); } } // PRECONDITION: int myencode(int q1marks, int q2marks) { tworaisedq1 = power(2, q1marks); threeraisedq2 = power(3, q2marks); } // POSTCONDITION: // PRECONDITION: int power(int base, int exponent) { } // POSTCONDITION: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 2

Contract View of Functions #include <iostream> using namespace std; int myencode(int q1marks, int q2marks); int main() { } } for ( ) { cipher = myencode(q1marks, q2marks); Ensure pre-condition of myencode before invoking Guaranteed post-condition of myencode on returning Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, // PRECONDITION: // 1 <= q1marks <= 10 // 1 <= q2marks <= 10 int myencode(int q1marks, int q2marks) { BLACK BOX } // POSTCONDITION: // Returned value = // 2 q1marks x 3 q2marks // No side effects (later lecture) 3

Flow of Control: An Animation #include <iostream> using namespace std; int myencode(int q1marks,int q2marks); int power(int base, int exponent); int main() { for ( ) { cipher = myencode(q1marks, q2marks); } return 0; } int myencode(int q1marks, int q2marks) { tworaisedq1 = power(2, q1marks); threeraisedq2 = power(3, q2marks); return cipher; } int power(int base, int exponent) { return result; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

CALL STACK Activation Records in Call Stack When a function (caller) calls a function (callee) a fresh activation record for callee created Values of function parameters from caller copied to space allocated for formal parameters of callee PC of caller saved Other book-keeping information updated Activation record for callee pushed on call stack int myencode(int q1marks, int q2marks) {. tworaisedq1 = power(2, q1marks); } Activation record: power Activation record: myencode Activation record: main Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

CALL STACK Activation Records in Call Stack When a function (callee) returns Callee s activation record popped from call stack Return value from popped activation record copied to activation record of caller (now on top of stack) Value of PC saved in popped activation record loaded in PC of CPU Free activation record of callee Resume execution of instruction at location given by updated PC int power(int base, int exponent) {. return result; } Activation record: power Activation record: myencode Activation record: main Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Call-by-Value Parameter Passing Values of function parameters copied from activation record of caller to activation record of callee Recall: Formal parameters of callee (power) are its local variables Not confused with parameters used in caller (myencode) when invoking callee (power) Only way in which callee (power) can let caller (myencode) see effects of its computation is through return value of callee Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Call-by-Reference Parameter Passing #include <iostream> int swap(int &m, int &n) using namespace std; { int swap(int &m, int &n); int main() { int temp; m intand status; n are NOT local variables of swap, temp = m; cout but << references Give two integers: (or aliases) ; cin to >> caller a >> b; m = n; variables status = swap(a, and b) b); used to pass parameters n = temp; cout << a: << a << b: << b << endl; return 0; return 0; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Practice Problem 1 Write a C++ function intsqroot that takes a non-negative double input parameter and returns a double value such that * fractional part of intsqroot(x) is always 0.0 * intsqroot(x) is always non-negative * (intsqroot(x)) 2 <= x < (intsqroot(x) + 1) 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Practice Problem 1 (continued) double intsqroot(double x) { double result; // Input validation } // Your code to compute intsqroot return result; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Practice Problem 2 We want to write a program that takes the coordinates of 3 points, each in 3-dimensional space, and finds which one of them is farthest from the origin, and which is nearest to the origin. The program then prints the nearest and farthest points, along with the integral part of their distances from the origin. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Practice Problem 2 (continued) Each point is represented by a triple of floating-point coordinates (x, y, z). The distance of the point (x, y, z) from the origin is the positive square root of x 2 + y 2 + z 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Practice Problem 2 (continued) Write a C++ program to solve the above problem. Use the intsqroot function designed in Practice Problem 1. Optional: How would you extend your program to accept n points and find the farthest and nearest ones, where n is user provided. [You cannot use arrays]. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Practice Problem 3 We want to write a C++ function that can be eventually used to play a game of tic-tac-toe (using 0 s and 1 s). 0 1 1 0 1 0 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Practice Problem 3 (continued) A configuration of the tic-tac-toe grid is represented by a sequence of 9 integers 0 1 0 0, 1, 0, -1, 1, -1, 0, 1, -1 1 0 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Practice Problem 3 (continued) A configuration of the tic-tac-toe grid is represented by a sequence of 9 integers 0 1 0 0, 1, 0, -1, 1, -1, 0, 1, -1 1 0 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Practice Problem 3 (continued) A configuration of the tic-tac-toe grid is represented by a sequence of 9 integers 0 1 0 0, 1, 0, -1, 1, -1, 0, 1, -1 1 0 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Practice Problem 3 (continued) A valid configuration has number of 0 s either equal to number of 1 s or more by one. Similarly, a valid configuration can have at most one winning line of 0 s or 1 s. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

Practice Problem 3 (continued) Example of invalid configuration 0 1 0 0, 1, 0, 1, 1, 1, 0, 1, -1 1 1 1 0 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Practice Problem 3 (continued) Write a C++ function tttcheckconfig that checks if a configuration of the game is a valid one. Your function should return the boolean value true if the configuration is valid, else it should return the boolean value false. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

Practice Problem 4 Write a C++ function tttreferee that takes as inputs a sequence of 9 integers in {-1, 0, 1} representing a config of tic-tac-toe, and returns 1 if 1 has a winning config, 0 if 0 has a winning config, 2 otherwise Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21

Practice Problem 5 Using the functions tttcheckconfig and tttreferee, we want to write a C++ program that plays a game of tic-tac-toe with the user. The user always plays first and uses 0. The program and the user alternate with their turns, and the program uses 1. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

Practice Problem 5 (continued) Positions on tic-tac-toe grid 1 2 3 4 5 6 7 8 9 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

Practice Problem 5 (continued) The user indicates her choice of position for the next 0 by providing the position of the tic-tactoe grid. The program must read in this input, and find a position for the next 1 such that we have a valid configuration, and (hopefully, a winning configuration for 1 ). It then outputs the position of 1 so that the user can read it. This process continues until either the grid is filled or somebody wins. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 24

Practice Problem 5 (continued) Typical run of your program: Position of 0: 3 (user input) Position of 1 is 5 (program output) Position of 0: 6 (user input) Position of 1 is 1 (program output) Position of 0: 9 (user input) 0 is winner 1 2 03 4 51 06 7 8 09 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 25