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
printf() printf() is for output 2
scanf() scanf() is for input. Do not forget & in front of variable names. 3
scanf() 4
Arithmetic Operator 5
Mathematical Functions #include 6 ceil(X) Round X up floor(X) Round X down
Mathematical Functions #include 7 ceil(X) Round X up floor(X) Round X down
Finds the common log of n+3 of an input. 8 #include 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)=
Mathematical Functions #include 9 ceil(X) Round X up floor(X) Round X down
Finds the power of an input. 10 #include 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 3 n1 power n2 = 8
Mathematical Functions #include 11 ceil(X) Round X up floor(X) Round X down
Finds the square root of an input. 12 #include 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 : 3 square root =
Mathematical Functions #include 13 ceil(X) Round X up floor(X) Round X down
Finds the round down value of an input. 14 #include 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 : Round up =
Mathematical Functions #include 15 ceil(X) Round X up floor(X) Round X down
Finds the round down value of an input. 16 #include 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 : Round down =
Formatted Output 17
Formatted Output 18 #include int main() { float n1; n1 = ; printf(“n1 = %.2f\n”, n1); return 0; } n1 = 1.73
Formatted Output 19 #include int main() { float n1; n1 = ; printf(“n1 = %.0f\n”, n1); return 0; } n1 = 1
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
Exercise 1 21
Exercise 3 22
Exercise 4 23
Exercise 5 24
Self Practice 7 25
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. 26
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. 27