Professional Documents
Culture Documents
Computer-Programming IterationFunction
Computer-Programming IterationFunction
Computer Programming
1
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ทบทวน Iteration
- for loop
- while loop
- do … while
2
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
- continue
- goto
- Breaking a loop
3
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
- Nested loop
4
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
แบบฝึกหัด Iteration
1. จงหาผลรันของ Loop ต่อไปนี้
1.1 for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cout << i << j << " ";
}
cout << endl;
}
5
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
int n = 5;
1.4 for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
if ((row == 1)||(row == n))
cout << "*";
else if (row==col)
cout << "*";
else
cout << ' ';
}
cout << endl;
}
6
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
1.5
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
if(i==1 || i==5) cout << "* ";
else if(i == j || j==5-i+1) cout << "* ";
else cout << " ";
}
cout << endl;
}
7
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
2. จงเขียนโปรแกรมแก้ปัญหาต่อไปนี้
2.1 จงเขียนโปรแกรม โดยใช้ for loop แสดงตารางเศษเหลือของการหารที่แสดงผลการทางาน
ตามตัวอย่าง
ตัวอย่างผลรัน
8
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
9
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
10
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ตัวอย่างผลรัน เมื่อป้อน n = 8
Enter n: 8
You should enter odd integer.
11
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
12
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
13
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ตัวอย่างผลรันเมื่อป้อน n เป็น 4
14
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
15
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
Introducing to function
#include <iostream>
using namespace std;
int main() {
int a, b;
while(true){
cin >> a >> b;
cout << “Max = “ << max(a,b) << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
while(true){
cin >> a >> b;
cout << “Max = “ << max(a,b) << endl;
}
return 0;
}
16
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ประเภทของฟังก์ชัน
ดูให้ดีว่า x แต่ละที่มีค่าเท่าไหร่
#include <iostream>
using namespace std;
void f();
void g();
int x = 11;
int main() {
int x = 22;
{
int x = 33;
cout<< "In block inside main(): x = " << x << endl;
}
cout << "In main(): x = " << x << endl;
cout << "In main(): ::x = " << ::x << endl;
}
void f() {
intx = 44;
cout<< "In f(): x = " << x << endl;
}
void g() {
cout<< "In g(): x = " << x << endl;
}
17
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
Example
#include <iostream>
using namespace std;
int main() {
int month, day, year;
do
{
cout << "Please enter month, day, year: ";
cin >> month >> day >> year;
printDate(month, day, year);
}
while (month > 0);
}
18
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
การส่งค่าเข้าไปในฟังก์ชัน
Passing by Value Passing by Reference
Example
#include <iostream>
using namespace std;
int main()
{
float a = 5.6, b = 6.5;
cout << "a = " << a << ", b = " << b << endl;
swap(a,b);
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
19
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
แบบฝึกหัด Function
1. จงหาผลรันขอโปรแกรมต่อไปนี้
#include <iostream>
using namespace std;
int x = 44;
void fun1();
void fun2(int&);
int fun3(int);
int fun4(int, int);
int main(){
int a = 5, b = 3, x = 11;
fun1();
fun2(x);
cout << "x = " << x << endl;
cout << fun3(b) << endl;
cout << fun4(a, b) << endl;
return 0;
}
void fun1(){
int x = 22;
cout << "x = " << ::x << endl;
}
void fun2(int &x)
{
cout << "x = " << x << endl;
x = 33;
}
int fun3(int n){
int res = 1;
for(int i = 1; i <= n; i++)
res *= i;
return res;
}
int fun4(int n, int k){
return fun3(n)/(fun3(k)*fun3(n-k));
}
20
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
21
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
3. จงหาการทางานของโปรแกรมต่อไปนี้
#include <iostream>
using namespace std;
int x=10;
void f(int& x)
{
x=30;
}
void g(int x)
{
x=x+10;
}
int i(int x)
{
return 2*x;
}
void h(int x,int& z)
{
z=i(x);
}
int main()
{
cout << "Global x: "<< x <<endl;
{
int x=20;
cout << "x in block: " << x << endl;
}
int x=5;
f(x);
cout << "x = " << x << endl;
g(x);
cout << "x = " << x << endl;
int z;
x=1;
h(x,z);
cout << "x = " << x << endl;
cout << "z = " << z << endl;
}
22
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
23
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
5. จงหาผลรันของโปรแกรมที่กาหนดให้
#include <iostream>
using namespace std;
int m, n;
int fun1(int n)
{
m = m + 2;
return 2 * n - 7;
}
int main()
{
m = 2;
n = 4;
fun2(m, n);
cout << "ans 1: " << m << n << endl;
cout << "ans 2: " << fun1(m) << endl;
cout << "ans 3: " << fun1(n) << endl;
cout << "ans 4: " << fun1(m) << endl;
return 0;
}
24
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
25
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
26
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
27
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
28
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
8. จงเติมโปรแกรมต่อไปนี้ให้สมบูรณ์
#include <iostream>
using namespace std;
// ประกาศฟังก์ชันที่นี่
________________________________________________________
________________________________________________________
int main()
{
float money = 0, change;
int number=0;
get_input(money, number);
change = money;
string drink = get_drink(money, number, change);
return 0;
}
29
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
คะแนน เกรด
มากกว่าหรือเท่ากับ 90 A
มากกว่าหรือเท่ากับ 80 B
มากกว่าหรือเท่ากับ 70 C
มากกว่าหรือเท่ากับ 60 D
ต่ากว่า 60 F
ตัวอย่างผลรัน
Please enter your score (0-100): 59
Your grade is F
Please enter your score (0-100): 60
Your grade is D
Please enter your score (0-100): 70
Your grade is C
Please enter your score (0-100): 80
Your grade is B
Please enter your score (0-100): 90
Your grade is A
Please enter your score (0-100): -8
Score must be between 0 and 100
Press any key to exit
int main() {
int score;
while (true) {
cout << “Please enter your score (0-100): ”;
cin >> score;
if (score < 0 || score > 100) {
cout << “Score must be between 0 and 100\n”;
cout << “Press any key to exit” << endl;
break;
}
get_grade(score);
}
return 0;
}
30
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
31
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ตัวอย่างผลรันเมื่อกรอก 5 ตัวอย่างผลรันเมื่อกรอก 7
7
5 * * * * * * *
* * * * * * *
* * * *
* * * *
* * * *
* * * * * * *
* * * * * * *
32
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
แนะนาฟังก์ชันพิเศษใส่ไข่
Function Random
#include<cstdlib>
int x, y, range;
x = rand();
range = y-0+1;
1.) ถ้าต้องการสุ่มอยู่ในช่วงระหว่าง 0 - y จะได้ x = rand() % range;
x = rand()%(11) + 10
x = (19998 % 11) + 10 = (0) + 10 = 10
x = (19999 % 11) + 10 = (1) + 10 = 11
x = (20000 % 11) + 10 = (2) + 10 = 12
x = (20001 % 11) + 10 = (3) + 10 = 13
x = (20002 % 11) + 10 = (4) + 10 = 14
.
.
.
x = (20008 % 11) + 10 = (10) + 10 = 20
33
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
Factorial function
long fact(int n){
if(n < 0) return 0;
long f = 1;
while(n > 1){
f *= n--;
}
return f;
}
int main(){
for(int i= -1; i < 6; i++)
cout<< " " << fact(i);
cout<< endl;
return 0;
}
Classifying a Character
ฟังก๋ชัน isPrime()
bool isPrime(int n){
float sqrtn = sqrt(n);
if(n < 2) return false;
if(n < 4) return true;
if(n % 2 == 0) return false;
for(intd = 3; d < sqrtn; d += 2)
if(n % d == 0) return false;
return true;
}
34
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ระวัง !
Iteration
- ตรวจสอบ condition ให้ ชดั เจน
- การวนลูปนัน้ เกิดขึน้ จนกว่า condition จะเป็ น เท็จ
- ถ้าใช้ลปู อนันต์ while(true) หรือ for(;;) อย่าลืมหาทางออกให้ลปู ด้วย
- วิเคราะห์ผลรันแบบ Step by step
Function
- บางฟั งก์ชน
ั ทีเ่ ราต้องเรียกใช้ อย่าลืม #include <library>
- Type ของฟั งก์ชน ั ต้องเป็ นชนิดเดียวกันกับตัวแปรทีเ่ ราคืนค่าให้มนั
- หากเขียนฟั งก์ชนั ทีหลัง main() ต้องประกาศฟั งก์ชนั ก่อนด้วย
- ค่าทีส่ ง่ เข้าไปในฟั งก์ชนั ควรเป็ นประเภทเดียวกันกับ parameter ของฟั งก์ชนั
- อย่าสับสนตัวแปร Global กับ Local
- ต้องเข้าใจความแตกต่างของ Pass by Value กับ Reference
35
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
แสดงความคิดเห็น
โครงการเตรียมความพร้อมก่อนสอบ ปีการศึกษา 2562
นายปวเรศ นิยมเหลา (P’PondHub) ภาควิชาวิศวกรรมคอมพิวเตอร์
36