11
CP104 Introduction to Programming Repetition and loop Lecture 15 __ 1 Case study 1: Calculate the approximation of Pi Calculate the approximation of Pi by formula with error 1e-4. Algorithm 1. pi = 0, s = 1, n = 1, t = s/n 2. while fabs(t) > 1e-4 do 1. pi = pi + t 2. n = n +2 3. s = -s 4. t = s / n; 5. end while 3. Pi = 4 * pi 1 1 1 1 ... 4 3 5 7

Case study 1: Calculate the approximation of Pi

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 1

Case study 1: Calculate the approximation of Pi

• Calculate the approximation of Pi by formula

with error 1e-4.

• Algorithm

1. pi = 0, s = 1, n = 1, t = s/n2. while fabs(t) > 1e-4 do

1. pi = pi + t2. n = n +23. s = -s4. t = s / n;5. end while

3. Pi = 4 * pi

1 1 11 ...

4 3 5 7

Page 2: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 2

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

Page 3: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 3

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)

• Algorithm1. f1 = 1, f2 = 1, i = 1

2. For i from 1 to 20 do1. Print f1 and f2

2. f1 = f1 + f2

3. f2 = f2 + f1

End of for loop

Page 4: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 4

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; }

}

Page 5: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 5

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:1. Get a key k2. While the input character c is not \n3. Convert c into its cipher text by using the kth letter forward4. End while

• Encryption Algorithm:1. For key k2. While the input character c is not \n3. Convert c into its plain text by using the kth letter backward 4. End while

Page 6: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 6

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); }

Page 7: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 7

Reading Data from a File

#include <stdio.h> /* defines fopen, fclose, fscanf fprintf, and EOF */

intmain(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

Page 8: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 8

Structure Chart for Computing Solar Collecting Area Size

Page 9: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 9

Program to Approximate Solar Collecting Area Size

Page 10: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 10

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

Page 11: Case study 1: Calculate the approximation of Pi

CP104 Introduction to Programming Repetition and loop Lecture 15 __ 11

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