You are on page 1of 37

ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์

Engineering Students Union Academic Club

Computer Programming

Iteration & Function


Tutor : P’ PondHub Computer Engineering
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

Lecture 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)

- Forever for & Forever while

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

1.2 for (int i=1; i<=8; i++) {


cout<<endl;
for (int j=1; j<=i; j++){
cout<<j;
}
}

5
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

1.3 for(int i=1;i<=3;i++) {


for(int j=i;j<=3;j++) {
cout << j << " ";
}
cout << endl;
}

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)

2.2 จงเขียนโปรแกรม C++ เพื่อให้ได้ผลการทางานดังต่อไปนี้ โดยกาหนดให้ใช้ for statements

9
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2.3 จงทาการเขียนโปรแกรมภาษา C++ ในการรับค่า n ที่เป็นจานวนเต็ม แล้วใช้โครงสร้างควบคุมแบบวนซ้า


(Iteration) ด้วยคาสั่ง for ในการแสดงผลลัพธ์ให้มรี ูปแบบตามตัวอย่างข้างล่าง
ตัวอย่างผลรัน เมื่อผู้ใช้ป้อนค่า 5 ผ่านทางแป้นพิมพ์

10
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2.4 จงเขียนโปรแกรมภาษา C++ รับค่า n จากแป้นพิมพ์ ต้องการให้ค่า n ที่ป้อนเป็ นเลขจานวนเต็มคี่


มากกว่าหรื อเท่ากับ 5 แสดงผลตามตัวอย่างต่อไปนี้
ตัวอย่างผลรัน เมื่อป้อน n = 7

ตัวอย่างผลรัน เมื่อป้อน n = 8
Enter n: 8
You should enter odd integer.

11
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2.5 จงเขียนโปรแกรมภาษา C++ คานวณหาคาตอบของสมการต่อไปนี้

กาหนดให้โปรแกรมรับค่า n จากแป้นพิมพ์และแสดงผลค่า x ออกทางจอภาพ


ตัวอย่างผลการทางานเมื่อป้อนค่า n เป็น 75
Input n: 75
X = 0.82255

12
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2.6 จงทาการเขียนโปรแกรมภาษา C++ โดยการใช้โครงสร้างควบคุมแบบวนซ้า (Iteration) ในการเขียนตารางสูตร


คูณ ตั้งแต่แม่ 2 ถึงแม่ 12 ดังตัวอย่างผลลัพธ์ที่แสดงข้างล่าง

13
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2.7 จงเขียนโปรแกรมภาษา C++ รับค่า n จากแป้นพิมพ์แล้วแสดงผลตามตัวอย่างต่อไปนี้ โดยใช้คาสัง่ for


ตัวอย่างผลรันเมื่อป้อน n เป็น 3

ตัวอย่างผลรันเมื่อป้อน n เป็น 4

14
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2.8 จงเขียนโปรแกรมภาษา C++ รับค่า n จากแป้นพิมพ์แล้วแสดงผลตามตัวอย่างต่อไปนี้


เมื่อผู้ใช้ป้อนค่า 8

15
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

Introducing to function

#include <iostream>
using namespace std;

int max(int x, int y) {


if (x < y)
return y;
else
return x;
}

int main() {
int a, b;
while(true){
cin >> a >> b;
cout << “Max = “ << max(a,b) << endl;
}
return 0;
}

#include <iostream>
using namespace std;

int max(int, int);

int main() {
int a, b;
while(true){
cin >> a >> b;
cout << “Max = “ << max(a,b) << endl;
}
return 0;
}

int max(int x, int y) {


if (x < y)
return y;
else
return x;
}

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;

void printDate(int, int, int);

int main() {
int month, day, year;
do
{
cout << "Please enter month, day, year: ";
cin >> month >> day >> year;
printDate(month, day, year);
}
while (month > 0);
}

void printDate(int m, int d, int y) {


if(m < 1 || m > 12 || d < 1 || d > 31 || y < 0) {
cerr << "Error: parameter out of range. \n";
return;
}
switch (m)
{
case 1: cout << "January "; break;
case 2: cout << "February "; break;
case 3: cout << "March "; break;
case 4: cout << "April "; break;
case 5: cout << "May "; break;
case 6: cout << "June "; break;
case 7: cout << "July "; break;
case 8: cout << "August "; break;
case 9: cout << "September "; break;
case 10: cout << "October "; break;
case 11: cout << "November "; break;
case 12: cout << "December "; break;
}
cout << d << ", " << y << endl;
}

ทดสอบโปรแกรม เมื่อป้อนค่า (9,28,1966), (1,6,1996), (0,0,0) ตามลาดับ

18
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

การส่งค่าเข้าไปในฟังก์ชัน
Passing by Value Passing by Reference

Example
#include <iostream>
using namespace std;

void swap(float&, float&);

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

void swap(float &x, float &y) {


float temp = x;
x = y;
y = temp;
}

จงหาผลรันของโปรแกรม (หมายเหตุ __ หมายถึงการแสดงผลรัน 1 อักขระ)


__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __

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

(หมายเหตุ __ หมายถึงการแสดงผลรัน 1 อักขระ)


__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __

20
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

2. จงเขียนโปรแกรมภาษา C++ โดยโปรแกรมดังกล่าว ประกอบด้วยฟังก์ชันหลัก ฟังก์ชัน withdraw และ


ฟังก์ชัน prnt_bal โดยรายละเอียดของฟังก์ชัน เป็นดังนี้
ฟังก์ชันหลักทาการ ฟังก์ชัน withdraw ทาการ
- รับค่าจานวนเงินในบัญชีธนาคาร - รับค่าจานวนเงินที่ต้องการถอน (wd_mn)
(bal) - ส่งค่าจานวนที่ต้องการถอน (wd_mn) กลับเข้า
- เรียกใช้งานฟังก์ชัน withdraw ไปในฟังก์ชันหลัก
- คานวณจานวนเงินในบัญชีใหม่ - หากป้อนจานวนที่ต้องการถอน (wd_mn)
- เรียกใช้งานฟังก์ชัน prnt_bal มากกว่าจานวนเงินในบัญชีธนาคาร (bal) ให้ทา
การวนรับค่าใหม่

และ ฟังก์ชัน prnt_bal ทาการ พิมพ์จานวนเงินในบัญชี (bal)


หมายเหตุ ขอให้ใช้ชื่อตัวแปรที่อยู่ในวงเล็บ เพื่อให้สะดวกกับการตรวจ
ตัวอย่างผลรัน โดยตัวหนาคือ input ที่รับเข้ามา
Enter balance: 5000
Enter amount of withdraw money: 1000
New balance is 4000.

ตัวอย่างผลรัน กรณี ที่ wd_mn>bal โดยตัวหนาคือ input ที่รับเข้ามา


Enter balance: 5000
Enter amount of withdraw money: 6000
Enter amount of withdraw money: … (รอรับค่าใหม่)

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

(หมายเหตุ __ หมายถึงการแสดงผลรัน 1 อักขระ)


__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __

22
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

4. จงเขียนโปรแกรมภาษา C++ โดยฟังก์ชันหลักจะทาหน้าที่ ดังต่อไปนี้


- เรียกใช้งานฟังก์ชันที่ชื่อว่า read ที่ทาหน้าทีร่ ับค่ารัศมี (radius) จากแป้นพิมพ์ แล้วส่งค่ารัศมี
กลับมาให้ฟงั ก์ชันหลักใช้งานต่อ กาหนดให้ผลรันที่แสดงออกทางหน้าจอจากการเรียกฟังก์ชัน read
เป็นดังนี้
ตัวอย่างผลรัน เมื่อผู้ใช้ป้อนค่ารัศมีเท่ากับ 2 ผ่านทางแป้นพิมพ์
Enter radius: 2

- เรียกใช้งานฟังก์ชันที่ชื่อว่า fiAreaCircum โดยทาการส่งค่ารัศมีทรี่ ับมาจากฟังก์ชัน read ไปยัง


ฟังก์ชัน fiAreaCircum โดยฟังก์ชัน fiAreaCircum ทาการหาค่าพื้นที่วงกลม (area) และเส้นรอบวง
(circum) จากนั้นทาการส่งค่าพื้นที่วงกลม (area) และเส้นรอบวง (circum) กลับไปยังฟังก์ชันหลัก
เพื่อใช้งานต่อ กาหนดให้ area และ circum มีค่าดังนี้
area = 3.14159 * radius * radius
circum = 2 * 3.14159 * radius
- เรียกใช้งานฟังก์ชันที่ชื่อว่า write โดยฟังก์ชันหลักทาการส่งค่าพื้นที่วงกลม (area) และเส้นรอบวง
(circum) ที่คานวณมาก่อนหน้านี้ ไปยังฟังก์ชัน write โดยฟังก์ชัน write จะทาการพิมพ์ผลลัพธ์
ออกทางจอภาพ กาหนดให้ผลรันที่แสดงออกทางหน้าจอจากการเรียกฟังก์ชัน write เป็นดังนี้
ตัวอย่างผลรัน เมื่อค่ารัศมีที่ผู้ใช้ป้อนผ่านทางแป้นพิมพ์ในฟังก์ชันมีค่าเท่ากับ 2
Area of circle is 12.5664
Circumference is 12.5664

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

void fun2(int m, int& n)


{
m += n;
n = 2 * m - n;
cout << "ans 5: " << m << n << endl;
int k = 2;
cout << "ans 6: " << fun1(k)-4 << endl;
}

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

(หมายเหตุ __ หมายถึงการแสดงผลรัน 1 อักขระ)


__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __

24
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

6. [9 คะแนน] จงพัฒนาโปรแกรมภาษา C++ ที่กาหนดให้สมบูรณ์ เพื่อให้ได้ผลรันดังตัวอย่างที่กาหนดให้


(2/2013)
ตัวอย่างผลรัน เมื่อผู้ใช้ปอ้ นค่า 10 -3 -7 และ 5 ผ่านทางแป้นพิมพ์ตามลาดับ

Enter first positive integer: 10


Enter second positive integer: -3
Please re-enter: -7
Please re-enter: 5
5 <= 10

โปรแกรมที่กาหนดให้ (เติมส่วนของโปรแกรมลงในช่องว่างทีก่ าหนดให้)


#include <iostream>
using namespace std;
...................................... //function inputPositive's
prototype
...................................... //function orderPair's
prototype
int main()
{
.............................................................
int first = inputPositive();
.............................................................
int second = inputPositive();
orderPair(first, second);
cout << first << " <= " << second << endl;
return 0;
}
//function inputPositive's definition
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................

25
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

// ต่อจากหน้ าที่ แล้ว


//function orderPair's definition
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................
..................................................................

26
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

7. จงเขียนโปรแกรมภาษา C++ โดยฟังก์ชันหลัก (main) จะทาหน้าที่ดังต่อไปนี้


- เรียกใช้งานฟังก์ชันที่ชื่อว่า read ที่ทาหน้าทีร่ ับค่าความกว้าง (width) ความยาว (length) และ
ความลึก (depth) จากแป้นพิมพ์ แล้วส่ง 3 ค่าดังกล่าว กลับมาให้ฟงั ก์ชันหลักใช้งานต่อ กาหนด
ให้ผลรันที่แสดงออกทางหน้าจอจากการเรียกฟังก์ชัน read เป็นดังนี้ (ควรใช้ width, length,
depth เป็นชื่อตัวแปร เพื่อให้ง่ายต่อการตรวจ)
ตัวอย่างผลรัน เมื่อผู้ใช้ปอ้ นค่า 3 ค่า เท่ากับ 2 3 4 ผ่านทางแป้นพิมพ์ (อักษรตัวทึบคือค่าที่ป้อน
ผ่านทางแป้นพิมพ์)
Enter width: 2
Enter length: 3
Enter depth: 4

- เรียกใช้งานฟังก์ชันที่ชื่อว่า fiArea โดยทาการส่งค่าความกว้าง (width) และความยาว (length)


จากฟังก์ชันหลัก ที่รบั มาจากฟังก์ชัน read ไปยังฟังก์ชัน fiArea โดยฟังก์ชัน fiArea ทาการหาค่า
พื้นที่สเี่ หลี่ยม (area)จากนั้นทาการส่งค่าพื้นทีส่ ี่เหลี่ยม (area) กลับไปยังฟังก์ชันหลัก โดยผ่านทาง
พารามิเตอร์ เพื่อใช้ในการพิมพ์ผลลัพธ์ออกทางจอภาพต่อไป กาหนดให้พื้นที่สเี่ หลี่ยม (area) มีคา่
ดังนี้
area = width * length

- เรียกใช้งานฟังก์ชันที่ชื่อว่า fiVolume โดยทาการส่งค่าความกว้าง (width) ความยาว (length) และ


ความลึก(depth) จากฟังก์ชันหลัก ที่รับมาจากฟังก์ชัน read ไปยังฟังก์ชัน fiVolume โดยฟังก์ชัน
fiVolume ทาการหาค่าปริมาตร (volume) จากนั้นทาการส่งค่าปริมาตร (volume) กลับไปยัง
ฟังก์ชันหลัก โดยผ่านทางชื่อของฟังก์ชัน (return) เพื่อใช้ในการพิมพ์ผลลัพธ์ออกทางจอภาพ
ต่อไป กาหนดให้ปริมาตร (volume) มีคา่ ดังนี้
volume = width * length * depth

- เรียกใช้งานฟังก์ชันที่ชื่อว่า write โดยฟังก์ชันหลักทาการส่งค่าพื้นที่สี่เหลี่ยม (area) และปริมาตร


(volume) ที่คานวณมาก่อนหน้านี้ ไปยังฟังก์ชัน write โดยฟังก์ชัน write จะทาการพิมพ์ผลลัพธ์
ออกทางจอภาพกาหนดให้ผลรันที่แสดงออกทางหน้าจอจากการเรียกฟังก์ชัน write เป็นดังนี้
ตัวอย่างผลรัน เมื่อป้อนค่า width length depth โดยลาดับ เป็น 2 3 4

Area is 6 square meters.


Volume is 24 cubic meters.

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

cout << "You get: " << drink << endl;


cout << "Change: " << change << endl;

return 0;
}

// เติม arguments ของฟังก์ชั่นให้ครบ ทั้งชนิดและชื่อตัวแปร


void get_input(___________ ___________, ___________ ___________)
{
cout << "Please enter money: ";
cin >> money;
cout << "Enter the drink number: " << endl;
cout << "1 Coke" << endl << "2 Tea" << endl << "3 Milk" <<
endl;
cin >> drink;
}

// เติมชนิด arguments ของฟังก์ชั่นและชนิดของ return value ของฟังก์ชั่น


__________ get_drink(_________ money, _________ drink, ________
change)
{
if (money < 15) return "not enough money";

change = money - 15;


if(drink == 1) return "Coke";
else if(drink == 2) return "Tea";
else if(drink == 3) return "Milk";
else {
change = money;
return "Wrong choice";
}
}

29
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

9. จงเขียนฟังก์ชั่นในการแสดงเกรดของคะแนนทีเ่ ป็นค่าจานวนเต็มบวก และให้หยุดการทางานเมื่อคะแนนไม่ใช่


จานวนเต็มบวก โดยค่าคะแนน มีดังนี้

คะแนน เกรด
มากกว่าหรือเท่ากับ 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

กาหนด main ดังนี้

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)

10. จงเขียนโปรแกรมภาษา C++ รับจานวนเต็มคี่ทมี่ ีค่าไม่น้อยกว่า 3 จากแป้นพิมพ์ แล้ววาดรูปสี่เหลี่ยมจัตรุ ัส


ถ้าเป็นเลขคู่หรือมีค่าน้อยกว่า 3 ให้จบโปรแกรม
กาหนดฟังก์ชั่น main ดังนี้
int main() {
int n;
if (!get_n(n))
return 0;
box(n);
return 0;
}

ตัวอย่างผลรันเมื่อกรอก 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;

เช่น ต้องการเลขสุ่มตั้งแต่ 0-9 เพราะฉะนั้น y+1 =10


x = rand() % (y+1)
x = 20000 %(10) => x = 0
x = 19999 %(10) => x = 9
x = 19998 %(10) => x = 8
x = 19997 %(10) => x = 7
x = 19996 %(10) => x = 6
.
.
.
x = 19991 %(10) => x = 1

2.) ถ้าต้องการสุ่มในช่วงระหว่าง y1-y2 จะได้ range = y2-y1+1;


x = rand()%range + y1;
x = rand()%(y2-y1+1) + y1;

เช่น ต้องการเลขสุ่มตั้งแต่ 10-20 เพราะฉะนั้น y1 = 10, y2 = 20

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

bool isDigit(char); ……………………………………………………

bool isLower(char); ……………………………………………………

bool isUpper(char); ……………………………………………………

bool isSpace(char); ……………………………………………………

bool isCntrl(char); ……………………………………………………

bool isPunct(char); ……………………………………………………

ฟังก๋ชัน 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

ถ้าอยากได้ A ล่ะก็ ยื่นมือมาซี่


(พี่โตไม่ได้กล่าวไว้)

35
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)

แสดงความคิดเห็น
โครงการเตรียมความพร้อมก่อนสอบ ปีการศึกษา 2562
นายปวเรศ นิยมเหลา (P’PondHub) ภาควิชาวิศวกรรมคอมพิวเตอร์

36

You might also like