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

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Array Elements as Function Parameters

Arrays in C++ Instructor: Andy Abreu

Exam 3 Chapters 7 & 9

Computer Programming

Pointers II. Class 31

Review: C++ Basic Concepts. Dr. Yingwu Zhu

C++ For Science and Engineering Lecture 12

CS 101: Computer Programming and Utilization

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

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Pointer Data Type and Pointer Variables

The University of Nottingham

CS 101: Computer Programming and Utilization

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

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

CS24 Week 3 Lecture 1

C++ Final Exam 2017/2018

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

1 Memory management in C++

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

Programming in C++: Assignment Week 2

File I/O Christian Schumacher, Info1 D-MAVT 2013

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Functions, Arrays & Structs

Homework #3 CS2255 Fall 2012

Linked List using a Sentinel

CS 1704 Homework 2 C++ Pointer Basics Fall 2002

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

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Declaring a 2D Array

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Introduction to Programming I COS1511 School of Computing Revision Notes

Functions and Methods. Questions:

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

Practice question Answers

Topics. bool and string types input/output library functions comments memory allocation templates classes

COMP 11 Class 17 Outline

Memory and Pointers written by Cathy Saxton

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Ch. 17: Linked Lists. Introduction to Linked Lists

CS250 Final Review Questions

C++ Programming Lecture 4 Software Engineering Group

CS 103 Unit 13 Slides

Pointers, Dynamic Data, and Reference Types

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Ikra-Cpp: A C++/CUDA DSL for Object-oriented Programming with Structure-of-Arrays Layout

Graphics. Vector and Matrix Algebra. Korea Univ. Computer Graphics Lab. Graphics Korea University.

Graphics. Vector and Matrix Algebra. Korea Univ. Computer Graphics Lab. Graphics Korea University.

Brook+ Data Types. Basic Data Types

EP241 Computer Programming

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Variables, Memory and Pointers

Intermediate Programming & Design (C++) Classes in C++

Week 3: Pointers (Part 2)

Chapter 7 Array. Array. C++, How to Program

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Functions, Arrays & Structs

CSCE Practice Midterm. Data Types

Scope and Parameter Passing

Arrays and Pointers.

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Exam 1 is on Wednesday 10 / 3 / 2018

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Chapter 7 - Notes User-Defined Functions II

CS250 Final Review Questions

Lecture 23: Pointer Arithmetic

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Chapter 9. Pointers and Dynamic Arrays

Data Structures and Algorithms

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: An Example Program using Member Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Structures representing objects Groups of related variables, arrays, other structures Member functions as interfaces for interaction Accessing members (data and functions) of structures Pointers to structures Dynamic allocation and de-allocation of structures 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture An interesting program using member functions Two object-oriented implementations 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Acknowledgment Some examples in this lecture are from An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 All such examples indicated in slides with the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Recap: Vectors in 3 Dimensions [Ref. AGRBook] We want to reason about motion in 3-dimensional space Must deal with 3-dimensional vectors representing Position Velocity Acceleration 3-dimensional vectors are basic entities (objects) in this program Need to define a C++ structure to represent a vector For simplicity, we will use Cartesian coordinates Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Recap: Struct V3 with Member Functions struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); V3 sum (V3 const &b) { V3 v; v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; V3 scale (double const factor) { V3 v; v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

A Simple Motion Simulator [Ref AGRBook] Given Initial position and velocity of a solid body as 3- dimensional vectors Acceleration of the body as a 3-dimensional vector (assume this doesn t change with time) Granularity of simulation time ( ) Total elapsed time (T) Find the positions (as 3-dimensional vectors) every units of time for the entire elapsed time (T) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Augmenting V3 Let s add a member function to print out the x, y and z co-ordinates of a V3 object struct V3 { double x, y, z; Member functions sum, scale and length void print() { cout << x: << x << y: << y << z: << z << endl; return; ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

A Motion Simulator Program int main() { V3 vel, acc, pos; // initial velocity, acceleration, initial position V3 currdispl, currpos; // current displacement & position double t = 0.0, deltat, totalt; // t: time elapsed so far cout << Give x, y and z components of initial velocity: ; cin >> vel.x >> vel.y >> vel.z; cout << Give x, y and z components of acceleration: ; cin >> acc.x >> acc.y >> acc.z; cout << Give x, y and z components of initial position: ; cin >> pos.x >> pos.y >> pos.z; cout << Give total simulation time: ; cin >> totalt; cout << Given simulation time granularity: ; cin >> deltat; Rest of code Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

A Motion Simulator Program int main() { V3 vel, acc, pos; // initial velocity, acceleration, initial position V3 currdispl, currpos; // current displacement & position double t = 0.0, deltat, totalt; // t: time elapsed so far Reading in values if ((totalt < 0) (deltat <= 0)) { cout << Invalid input! << endl; return -1; Rest of code Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

A Motion Simulator Program int main() { V3 vel, acc, pos; // initial velocity, acceleration, initial position V3 currdispl, currpos; // current displacement & position double t = 0.0, deltat, totalt; // t: time elapsed so far Reading in and validating values while (t <= totalt) { // Calculate current displacement using vel*t + (0.5)*acc*t 2 currdispl = (vel.scale(t)).sum(acc.scale(0.5*t*t)); currpos = currdispl.sum(pos); cout << Time << t << ; currpos.print(); t = t + deltat; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

An Alternative Implementation Define a MotionSimulator Object struct MotionSimulator { V3 initpos, initvel, acc; V3 currpos, currvel; double deltat; void initializesimulator() { currpos = initpos; currvel = initvel; return; Rest of member functions ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

An Alternative Implementation Define a MotionSimulator Object struct MotionSimulator { Member declarations void initializesimulator() { void simulateastep() { // Calculate updated position as currpos + currvel*deltat // Calculate updated velocity as currvel + acc*deltat currpos = currpos.sum(currvel.scale(deltat)); currvel = currvel.sum(acc.scale(deltat)); return; Rest of member functions ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

An Alternative (Not Equivalent) Implementation Define a MotionSimulator Object struct MotionSimulator { Member declarations void initializesimulator() { void simulateastep() { void printposition() { currpos.print(); return; ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

A Motion Simulator Program int main() { MotionSimulator msim; double t = 0.0, deltat, totalt; // t: time elapsed so far cout << Give x, y and z components of initial velocity: ; cin >> (msim.initvel).x >> (msim.initvel).y >> (msim.initvel).z; cout << Give x, y and z components of acceleration: ; cin >> (msim.acc).x >> (msim.acc).y >> (msim.acc).z; cout << Give x, y and z components of initial position: ; cin >> (msim.initpos).x >> (msim.initpos).y >> (msim.initpos).z; cout << Give total simulation time: ; cin >> totalt; cout << Given simulation time granularity: ; cin >> deltat; msim.deltat = deltat; Rest of code Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

A Motion Simulator Program int main() { MotionSimulator msim; double t = 0.0, deltat, totalt; // t: time elapsed so far Reading in and validating values msim.initializesimulator(); while (t <= totalt) { msim.simulateastep(); cout << Time << t << ; msim.printposition(); t = t + deltat; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

An Interesting Question Are the results computed by the two simulations the same? If not, why? Try with various values of simulation time granularity For what values of simulation time granularity, do the results computed by the two simulations agree (by and large)? Implementation choices are important when solving a problem Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Summary A simple motion simulator program using object-oriented programming Use of member functions Glimpse of data hiding, and effect of simulation time granularity Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18