Case study 1: Calculate the approximation of Pi

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
For(int i = 1; i
Tree Recursion Traditional Approach. Tree Recursion Consider the Fibonacci Number Sequence: Time: , 1, 1, 2, 3, 5, 8, 13, 21,... /
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Muller ‘ s mothod Hun Hee Lee. Muller ’ s method Muller ’ s method for solving an equation of one variable f(x)=0. Muller ’ s method is an iterative method.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software.
1 CSE 20 Lecture 12: Analysis of Homogeneous Linear Recursion CK Cheng May 5, 2011.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
1.6 Loops academy.zariba.com 1. Lecture Content 1.While loops 2.Do-While loops 3.For loops 4.Foreach loops 5.Loop operators – break, continue 6.Nested.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Do-while loop Syntax do statement while (loop repetition condition)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Controlling Function Behavior Sequence, Selection and Repetition.
Formula? Unit?.  Formula ?  Unit?  Formula?  Unit?
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1 If statement (preview version) Syntax form: if ( condition) Statements else Statements.
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
5.1.  When we use the midpoint rule or trapezoid rule we can actually calculate the maximum error in the calculation to get an idea how much we are off.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
1 CSE 20 Lecture 13: Analysis of Recursive Functions CK Cheng.
Spot the Pattern Look for the pattern with the sequence of number and write the next 2 numbers in the pattern. 5, 8, 11, 14, , 10,
ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Lecture S1: Sample Lecture.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
Recursion DRILL: Please take out your notes on Recursion
while Repetition Structure
Repetition Control Structure in C++ Program
Lecture 5.
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Lecturer CS & IT Department UOS MBDIN
Lecture2.
Introduction to Computer Science - Alice
Formatted and Unformatted Input/Output Functions
FOR LOOPS.
CS149D Elements of Computer Science
Unit 3 Review (Calculator)
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Introduction to Programming
Input a number and print its table, using FOR loop.
Sequences Tuesday, 30 April 2019.
Print the following triangle, using nested loops
Statements in C Programming
Introduction to Programming
Calculate 9 x 81 = x 3 3 x 3 x 3 x 3 3 x 3 x 3 x 3 x 3 x 3 x =
Which sequence is linear? How do you know?
Types of Errors And Error Analysis.
Chapter 4: Loops and Iteration
Presentation transcript:

Case study 1: Calculate the approximation of Pi Calculate the approximation of Pi by formula with error 1e-4. Algorithm pi = 0, s = 1, n = 1, t = s/n while fabs(t) > 1e-4 do pi = pi + t n = n +2 s = -s t = s / n; end while Pi = 4 * pi

Implementation #include <stdio.h> #include <math.h> main(){ int s = 1; double n = 1, t = 1, pi = 0; while (fabs(t)>= 1e-4) { pi = pi + t; n += 2; s = - s; t = s/n; } pi = pi * 4; printf("%10.6f\n", pi); 3.141393

Case Study 2: Fibonacci Sequence Calculate the first 40 number of Fibonacci sequence: F(1) = 1, F(2) = 1, …, F(n) = F(n-1) + F(n-2) Algorithm f1 = 1, f2 = 1, i = 1 For i from 1 to 20 do Print f1 and f2 f1 = f1 + f2 f2 = f2 + f1 End of for loop

Implementation #include <stdio.h> main(){ long int f1, f2; int i; f1 = 1; f2 = 1; for (i = 1; i<=20; i++) { printf("%12ld %12ld ", f1, f2); if(i%2==0) printf("\n"); f1 = f1 + f2; f2 = f2 + f1; }

Case Study 3: Simple Encryption and Decryption Problem: Convert a plain text to a cipher text by alphabet of module k. Where 0<k<26 is an integer, used a key to encode and decode. For example if k = 3, A will be encrypted to D, and y will be encrypted to b. Encryption Algorithm: Get a key k While the input character c is not \n Convert c into its cipher text by using the kth letter forward End while For key k Convert c into its plain text by using the kth letter backward

Implementation #include <stdio.h> main(){ char c; int key; printf("Input a key between 1 and 25: \n"); scanf("%d", &key); while ((c = getchar())!='*') { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) c = c + key; if (c > 'Z' && c <='Z'+key || c >'z') c = c - 26; } printf("%c",c);

Reading Data from a File #include <stdio.h> /* defines fopen, fclose, fscanf fprintf, and EOF */ int main(void) { FILE *inp; /* input file pointer */ int sum = 0, /* sum of scores input so far */ score, /* current score */ input_status; /* status value returned by fscanf */ inp = fopen("scores.dat", "r"); /* open a file for reading */ printf("Scores\n"); input_status = fscanf(inp, "%d", &score); /* read the first line from the file */ while (input_status != EOF) { /* if not the end of file sign */ printf("%5d\n", score); sum += score; input_status = fscanf(inp, "%d", &score); /* continue to read the next line */ } printf("\nSum of exam scores is %d\n", sum); fclose(inp); /* close the file */ Run by command line

Structure Chart for Computing Solar Collecting Area Size

Program to Approximate Solar Collecting Area Size

Program to Approximate Solar Collecting Area Size (cont’d)

Program to Approximate Solar Collecting Area Size (cont’d)