CS1100 Computational Engineering

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional making.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
1. Agenda for loop Short-handed notation How to use break and continue 2.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
ECE Application Programming
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Chapter 13 Control Structures
Looping.
Chapter 13 Control Structures
CS1100 Computational Engineering
Structured Program
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
UMBC CMSC 104 – Section 01, Fall 2016
Computer Programming Techniques Semester 1, 1998
Examples Example Problems, their Algorithms, and their C Source Code.
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 13 Control Structures
Presentation transcript:

CS1100 Computational Engineering Control Structures N.S. Narayanaswamy

Perfect Number Detection Perfect number – sum of proper divisors adds up to the number Pseudocode: Read a number, A Set the sum of divisors to 1 If A is divisible by 2, Add 2 to the sum of divisors If A is divisible by 3, Add 3 to the sum of divisors … If A is divisible by A/2, Add A/2 to the sum of divisors If A is equal to the sum of divisors, A is a perfect number Perfect numbers upto 1,000,000: 6, 28, 496, 8128 only 2

Refining the Pseudocode Read a number, A Set the sum of divisors to 1 Set B to 2 While B is less than or equal to A/2 If A is divisible by B, Add B to the sum of divisors Increment B by 1 If A is equal to the sum of divisors, A is a perfect number Perfect numbers upto 1,000,000: 6, 28, 496, 8128 only 3

Perfect Number Detection main ( ){ int d=2, n, sum=1; scanf (“%d”, &n); while (d <= (n/2)) { if (n%d == 0) sum += d; d++; } if (sum == n) printf (“%d is perfect\n”, n); else printf (“%d is not perfect\n”, n); d<n will also do, but would do unnecessary work Perfect numbers upto 1,000,000: 6, 28, 496, 8128 only Exercise: Modify to find the first n perfect numbers 4

Counter controlled repetitions needs for loops Counter controlled repetitions needs Initial value for the counter Modification of counter: i = i+1or i= i–1, or any other arithmetic expression based on the problem, and Final value for the counter for repetition structure provides for the programmer to specify all these Any statement written using for can be rewritten using while Use of for helps make the program error free 5

for (expr1; expr2; expr3) <statement> Semantics: The for construct General form: for (expr1; expr2; expr3) <statement> Semantics: evaluate “expr1” - initialization operation(s) repeat - evaluate expression “expr2” and If “expr2” is true execute “statement” and “expr3” Else stop and exit the loop

Example Code with the while Construct scanf(“%d”, &n); value = 1; printf (“current value is %d \n”, value); counter = 0; while (counter <= n){ value = 2 * value; counter = counter + 1; } n = 0 : both 1 and 2 get printed! n = 2: 1, 2, 4, 8 get printed! The error is that n+1 iterations take place. But instead of n+1 values, n+2 values get printed! 7

Example Code with the for Construct scanf(“%d”, &n); value = 1; for (count = 0; count <=n; count=count+1){ if (count == 0) printf(“value is %d \n”,1); else{ value = 2 * value; printf(value is %d \n”, value); } Observe: a mistake in the earlier program is gone 8

Computing the Sum of the First 20 Odd Numbers Set j to the first odd number int i, j, sum; sum = 0; for (j = 1, i = 1; i <= 20; i = i+1){ sum += j; j += 2; } i : Loop control variable Termination condition Increment sum by the ith odd number Set j to the next odd number Note that we could write that i = i +1 as i++; It is advisable not to confuse the beginners with inc, dec operators! It is advisable to pull out “j = 1” to the outside and add it after “sum =0”; 9

String constants used to align heading and output data in a table Calculating Compound Interest a = p(1 + r)n #include<stdio.h> #include<math.h> main( ){ int yr; double amt, principal = 1000.0, rate = .05; printf(“%4s%10s\n”, “year”, “Amount”); for (yr = 1; yr < = 10; yr++) { amt = principal * pow(1.0 + rate, yr); printf(“%4d%10.2f\n”, yr, amt); } String constants used to align heading and output data in a table

The do-while construct for and while check termination condition before each iteration of the loop body Sometimes - execute the statement and check for condition General form: do {<statement>} while (expr); Semantics: execute the statement and check expr if expr is true, re-execute statement else exit

An Example #include<stdio.h> main( ) { int count = 1; do{ printf(“%d\n”, count); } while(++count <= 10); return 0; }

Find the Square Root of a Number How do we find the square root of a given number N? We need to find the positive root of the polynomial x2 – N Solve: x2 – N = 0

Newton–Raphson Method f(x) = x2− N f(x) √N f ' : the derivative of the function f By simple algebra we can derive xn+1 = xn – ( xn2 – N )/2xn = (xn2 + N)/2xn = (xn + N/xn )/2 http://en.wikipedia.org/wiki/Newton's_method

Square Root of a Number int n; float prevGuess, currGuess, error, sqRoot; scanf(“%d”, &n); currGuess = (float) n/2 ; error = 0.0001; do{ prevGuess = currGuess; currGuess = (prevGuess + n/prevGuess)/2; }while(fabs(prevGuess – currGuess)>error); sqRoot = currGuess; printf(“%f\n”, sqRoot);

Repetition Structures true false do-while Structure expr body true false while Structure expr body true false for Structure expr body init incr Single Entry Single Exit

Structured Programming To produce programs that are easier to develop, understand, test, modify easier to get correctness proof Rules Begin with the “simplest flowchart” Any action box can be replaced by two action boxes in sequence Any action box can be replaced by any elementary structures (sequence, if, if/else, switch, while, do-while or for) Rules 2 and 3 can be applied as many times as required and in any order

continue – starts the next iteration of the loop in which it occurs Break and Continue break – breaks out of the innermost loop or switch statement in which it occurs continue – starts the next iteration of the loop in which it occurs 18

#include<stdio.h> main ( ){ int i; An Example #include<stdio.h> main ( ){ int i; for (i = 1; i < 10; i = i+1){ if( i == 5) break; //continue; printf(“%4d”, i); } Note that we could write that i = i +1 as i++; It is advisable not to confuse the beginners with inc, dec operators! It is advisable to pull out “j = 1” to the outside and add it after “sum =0”; 19

Find a Smallest Positive Number #include<stdio.h> main ( ){ int n=0, smallNum = 10000; printf(“Enter Numbers (in the range 0 to 9999):\n”); scanf(“%d”, &n); while (n >= 0){ if(smallNum > n) smallNum = n; scanf(“%d”,&n); } printf(“Smallest number is %d\n”,smallNum); 20

Exercises Write a program that reads in the entries of a 3x3 matrix, and prints it out in the form of a matrix. The entries could be floating point too. Write a program that reads in orders of two matrices and decides whether two such matrices can be multiplied. Print out the decision. Write a program that reads in two matrices, and multiplies them. Your output should be the two matrices and the resulting product matrix. 21