lab 3: formatted i/o and c library functions its100: computer and programming lab. section 1...

27
LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr. Thanasan Tanhermhong (Tum) Ms. Pattheera Panitsuk (Fon+) Mr. Pongsate Tangseng Mr. Chinorot Wangtragulsang Mr. Kanin Assantachai (Ob) 1

Upload: jason-moore

Post on 04-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

1

LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONSITS100: Computer and Programming Lab.

Section 1Instructor: Wirat Chinnan

TAs:

Ms. Sasirassamee Buavirat

Mr. Thanasan Tanhermhong (Tum)

Ms. Pattheera Panitsuk (Fon+)

Mr. Pongsate Tangseng

Mr. Chinorot Wangtragulsang

Mr. Kanin Assantachai (Ob)

Page 2: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

2

printf()• printf() is for output

Page 3: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

3

scanf()• scanf() is for input. • Do not forget & in front of variable names.

Page 4: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

4

scanf()

Page 5: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

5

Arithmetic Operator

Page 6: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

6

Mathematical Functions• #include<math.h>

ceil(X) Round X up

floor(X) Round X down

Page 7: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

7

Mathematical Functions• #include<math.h>

ceil(X) Round X up

floor(X) Round X down

Page 8: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

8

Finds the common log of n+3 of an input.

#include <stdio.h>#include <math.h>int main(){ float n, ans; printf(“Enter a number: ”); scanf(“%f”,&n); n = n+3; ans = log10(n); printf(“log10(n+3)= %f\n”, ans); return 0;} Enter a number : 1

log10(n+3)= 0.477121

Page 9: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

9

Mathematical Functions• #include<math.h>

ceil(X) Round X up

floor(X) Round X down

Page 10: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

10

Finds the power of an input.

#include <stdio.h>#include <math.h>

int main(){ int n1,n2, ans; printf(“Enter two numbers: ”); scanf(“%d %d”,&n1,&n2); ans = pow(n1,n2); printf(“n1 power n2 = %d\n”, ans); return 0;}

Enter two Numbers: 2 3n1 power n2 = 8

Page 11: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

11

Mathematical Functions• #include<math.h>

ceil(X) Round X up

floor(X) Round X down

Page 12: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

12

Finds the square root of an input.

#include <stdio.h>#include <math.h>int main(){ float n1, ans; printf(“Enter a number: ”); scanf(“%f”,&n1); ans = sqrt(n1); printf(“square root = %f\n”, ans); return 0;}

Enter a number : 3square root = 1.732051

Page 13: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

13

Mathematical Functions• #include<math.h>

ceil(X) Round X up

floor(X) Round X down

Page 14: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

14

Finds the round down value of an input.

#include <stdio.h>#include <math.h>int main(){ float n1, ans; printf(“Enter a number: ”); scanf(“%f”,&n1); ans = ceil(n1); printf(“Round up = %f\n”, ans); return 0;}

Enter a number : 3.6345Round up = 4.000000

Page 15: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

15

Mathematical Functions• #include<math.h>

ceil(X) Round X up

floor(X) Round X down

Page 16: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

16

Finds the round down value of an input.

#include <stdio.h>#include <math.h>int main(){ float n1, ans; printf(“Enter a number: ”); scanf(“%f”,&n1); ans = floor(n1); printf(“Round down = %f\n”, ans); return 0;}

Enter a number : 3.6345Round down = 3.000000

Page 17: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

17

Formatted Output

Page 18: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

18

Formatted Output

#include <stdio.h>int main(){ float n1; n1 = 1.732051; printf(“n1 = %.2f\n”, n1); return 0;}

n1 = 1.73

Page 19: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

19

Formatted Output

#include <stdio.h>int main(){ float n1; n1 = 1.732051; printf(“n1 = %.0f\n”, n1); return 0;}

n1 = 1

Page 20: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

20

To Do in Class• Exercise 1, 3, 4, 5, and Self Practice 7• Call your TA when you finished. • You may take a break • Be ready for the speed test at 15.00

Page 21: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

21

Exercise 1

Page 22: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

22

Exercise 3

Page 23: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

23

Exercise 4

Page 24: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

24

Exercise 5

Page 25: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

25

Self Practice 7

Page 26: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

26

Speed Test• Speed test should be treated just like a real exam. • Rules:

• No talking. Be quiet.• No mobile phone.• No electronic devices other than your PC• No Internet• No cheating

• Cheating will result in a severe penalty

• TAs will not help you (except when your PC crashes).• Time allowed: 45 minutes.

Page 27: LAB 3: FORMATTED I/O AND C LIBRARY FUNCTIONS ITS100: Computer and Programming Lab. Section 1 Instructor: Wirat Chinnan TAs: Ms. Sasirassamee Buavirat Mr

27

Speed Test Instruction• Write your name on the question sheet.• Create all workspace on your ‘Desktop’ • When you finished

• Raise your hand to signal the assigned TA• TA grades your work• Quietly leave the room

• DO NOT bring the question sheet out. Leave it on your table.