SME 1013 Programming for Engineers

Similar documents
Section 7.2 Volume: The Disk Method

Unit II. (i) Computer Programming Languages

REPETITION CONTROL STRUCTURE LOGO

Nesting. Abstraction & Nesting. Example. if x is less than y output x is smaller else output y is smaller. CS256 Computer Science I Kevin Sahr, PhD

SME1013 PROGRAMMING FOR ENGINEERS

Repetition Structures

Grades 7 & 8, Math Circles 20/21/22 February, D Geometry Solutions

Loops / Repetition Statements

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

Grades 7 & 8, Math Circles 20/21/22 February, D Geometry

Dept. of CSE, IIT KGP

SNS COLLEGE OF ENGINEERING,

CS110D: PROGRAMMING LANGUAGE I

Information Science 1

Loops (while and for)

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Grade 9 Surface Area and Volume

Chapter 4 Introduction to Control Statements

You will need the following items: scissors, plate, 5 different colored pencils, protractor, paper to answer questions

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

Grade 9 Surface Area and Volume

CN#7 Objectives. Vocabulary 5/23/ Spheres. I will learn and apply the formulas for the surface area and volume of a sphere.

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Chapter 4: Control structures. Repetition

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Flowchart & Algorithm

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

Notice that the height of each rectangle is and the width of each rectangle is.

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

To find the surface area and volume of a sphere

EDEXCEL NATIONAL CERTIFICATE UNIT 4 MATHEMATICS FOR TECHNICIANS OUTCOME 1

Chapter Goals. Contents LOOPS

Information Science 1

Unit 1 Lesson 4. Introduction to Control Statements

Chapter 4: Control structures

7.1 It is well known that the formula for converting a temperature in Celsius to Fahrenheit is. o F = 9 5 o C (7.10)

Geometry Definitions and Theorems. Chapter 9. Definitions and Important Terms & Facts

C Programming for Engineers Structured Program

Score score < score < score < 65 Score < 50

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Spelling-Punctuation-Grammar Subject How will you promote high standards within this module? Term Duration (approx.)

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

x + 2 = 0 or Our limits of integration will apparently be a = 2 and b = 4.

Chapter 1: An Overview of Computers and Logic

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

A Quick Review of Chapter 1

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

Programming in C++ PART 2

LAB 5: SELECTION STATEMENTS

Introduction to Programming: Variables and Objects. HORT Lecture 7 Instructor: Kranthi Varala

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

Assignment Volume and Surface Area of Solids

Need more help with decimal subtraction? See T23. Note: The inequality sign is reversed only when multiplying or dividing by a negative number.

Repetition and Loop Statements Chapter 5

Warmup. April 28, 2017 Geometry 11.2 Areas of Circles and Sectors 1

Lab 9: Creating a Reusable Class

Fundamentals of Computing and Digital Literacy. Sample. Assignment title: Develop a Wiki. Marking Scheme

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

I101/B100 Problem Solving with Computers

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

S3 (3.1) N5 Volume.notebook April 30, 2018

Introduction. C provides two styles of flow control:

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

S3 (3.1) Volume.notebook March 02, 2016

Applications of Integration. Copyright Cengage Learning. All rights reserved.

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Loops. Repeat after me

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

Numerical Methods in Scientific Computation

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

(Section 6.2: Volumes of Solids of Revolution: Disk / Washer Methods)

Center of a sphere. Radius of a sphere. Chord of a sphere. Diameter of a sphere

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

G Programming Languages - Fall 2012

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

Structured Programming. Dr. Mohamed Khedr Lecture 9

SPHERES WHAT YOU LL LEARN. Ø Finding the surface area of a sphere Ø Finding the volume of a sphere

Loop Structures. Loop Structures. Algorithm to record 5 TV programmes. Recall Structured Programming..3 basic control structures.


Chapter Two: Program Design Process and Logic

APPM 2460: Week Three For, While and If s

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Fourth Edition

Grade VIII. Mathematics Geometry Notes. #GrowWithGreen

REPRESENTING ALGORITHMS. REPRESENTING ALGORITHMS IB DP Computer science Standard Level ICS3U

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

CBSE X Mathematics 2012 Solution (SET 1) Section C

Double Integrals over Polar Coordinate

Gaddis: Starting Out with Alice Test Bank Chapter Two 1 Chapter Two

Cutoff.Guru. Recruitment16.in. Recruitment16.in copyright Geometry and Mensuration. Some important mensuration formulas are:

Chapter 6 More About Loops and Decisions PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

Mr. Whelan Name: Block:

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Volume of Cylinders. Volume of Cones. Example Find the volume of the cylinder. Round to the nearest tenth.

SP about Rectangular Blocks

Repetition Structures II

Top-Down Program Development

Sect Volume. 3 ft. 2 ft. 5 ft

COP 1220 Introduction to Programming in C++ Course Justification

Transcription:

SME 1013 Programming for Engineers Programming Concepts by: Mohamed Hussein compliments to: Assoc. Prof. Dr. Mohd Shafiek Yaacob Stages of Program Development Logic errors Input? Formula? Special Cases? Output? Step 1 Step 2 Branching? Looping? Stop? Fortran? C/C++? BASIC? Java? Syntax errors Devide by Zero? Formula? Special Cases? Output? Program Concept Algorithm Development Program Coding Debuging Experienced programmers spend up to 90% of their time working on the logic of their program, not on the coding 1

What are these? Building blocks of the game of TETRIS Building Blocks of a Program Sequential Executions - instructions are performed one after the other Branching Operations - a decision is made to perform one block of instructions or another. Looping Operations - a block of instructions is repeated Only these three basic building blocks are needed to develop a solution to a problem. 2

Some Definitions Algorithm - a step-by-step procedure for finding the solution to a problem. - the idea of how a problem can be solved. Pseudocode - a free way or style of how an algorithm is written. - a hybrid mixture of a particular programming language and English. Flowchart - a way to describe algorithms graphically. - requires correct use of symbols. Common Flowchart Symbols or stop computation Input or output subprocess direction of flow connection branching looping 3

Sequential Execution of Instructions Sequential instructions are executed one after the other. The computer begins with the first instruction. The result from current and previous calculations may be used in the subsequent calculation. The instructions must be executed in correct order. Example 1.1 Construct an algorithm and a flowchart to compute the weight w of a hollow sphere of outside diameter d, wall thickness t, and density ρ. 4

Solution 1.1 (Algorithm) 1. Compute the inner and outer radii r o = d/2 and r i = d/2 t 2. Compute the volume v = 4π(r o3 r i3 )/3 3. Compute the weight w = ρv Solution 1.1 (Flowchart) r o = d/2 r i = d/2 t v = 4π(r o3 r i3 )/3 w = ρv stop 5

Example 1.2 Construct an algorithm and a flowchart to compute the weight w of a hollow sphere of outside diameter d, wall thickness t, and density ρ. Allow for input of the values of diameter d, wall thickness t, and density ρ. Also, allow for output of the values of volume v and weight w. Solution 1.2 (Algorithm) 1. Input diameter, thickness, and density 2. Compute the inner and outer radii 3. Compute the volume 4. Compute the weight 5. volume and weight 6

Solution 1.2 Flowchart Input d, t, ρ r o = d/2 r i = d/2 t v = 4π(r o3 r i3 )/3 w = ρv v, w stop Branching Operations A branch is a point in the program where the computer will make a decision about which set of instructions to be executed next, A decision is made depending on the answer of a posed question. The question must be formulated such that it has a simple answer and has only one possible outcome (e.g. yes or no).? 7

Example 1.5 Construct an algorithm and a flowchart to determine if a point (x, y) lies within a circle of radius r centered at the origin. If the point lies within the circle, print out a message and the distance, z, of that point from the center of the circle. Solution 1.5 (Algorithm) 1. Input the radius r and the (x, y) coordinate 2. Compute the distance z = (x 2 + y 2 ) 1/2 of the point (x, y) from the origin. 3. Is z < r? If yes, then print inside and print z If no, then print outside End of branch 8

Solution 1.5 (Flowchart) Input r, x, y z = (x 2 + y 2 ) 1/2 yes inside, z z < r? no outside Stop Nesting Branching Operations A nested branch is used when the computer needs to choose between more than two alternatives.? Example 1.6 Construct a flowchart to see if an integer n is negative, positive, or zero.? 9

Solution 1.6 (Flowchart) Input n no n < 0? yes no n = 0? yes Negative Positive Zero Stop Loops A counted loop repeats the executions for a predetermine number of times. A conditional loop repeats the executions until a condition is satisfied. Instructions Count? Condition? yes no 10

Counted Loops Most widely used in computer programming. Loop control variable LCV is used to control the loop. The loop stops when the LCV exceeds stop value Number of iterations is predetermine finished value stop value step value body of loop LCV name loop back Example 1.8 Construct a flowchart to print out the numbers from 1 to 100 and their squares. 11

Solution 1.8 (Flowchart) =1 stop=100 step=1 LCV LCV, LCV 2 Stop Example 1.9 Construct a flowchart to print out the even numbers from 1 to 100 and their squares. 12

Solution 1.9 (Flowchart) =2 stop=100 step=2 LCV LCV, LCV 2 Stop Conditional Loops The most common type is termed the while loop. A set of instructions is repeated while some condition is true. yes Finished? no body of loop 13

Example 1.10 Construct a flowchart to read in a series of numbers and keep track of the running total and the number of data items. Stop reading in the numbers when one of them has a value of zero (sentinel value). Then compute the average of all the numbers and report it. Solution 1.10 (Flowchart) SUM=0 COUNT=0 Input X no AVG=SUM/COUNT X 0? yes SUM=SUM+X COUNT=COUNT+1 AVG, COUNT stop 14

Nested Loops A counted loop is placed within another counted loop or a combination of different loops within each other. Example 1.11 Construct an algorithm and flowchart to create a 10 by 10 multiplication table such as 1 x 1 = 1, 1 x 2 = 2, and so forth. Solution 1.11 (Flowchart) Stop =1 stop=10 step=1 LCV1 =1 stop=10 step=1 LCV2 Product = LCV1 x LCV2 LCV1, LCV2, Product 15

Procedures A large program is often broken into several smaller components known as procedures or modules. Modules or procedures may be reuse again in the same program or in another program. Subprocess Example 1/13 Construct a flowchart to read an integer and evaluate its factorial. Use the subprocess to calculate the factorial. n! = 1 x 2 x x (n-1) x n 16

Solution 1/13 (Flowchart) Input n Y=Factorial(n) n, Y Factorial Y = 1 =1 stop=n step=1 LCV Y = Y x LCV Stop Return 17