Week 9
Week 9
Introduction
week 9: Function
Lecturer : Cao Tuan Dung
Dept of Software Engineering
Hanoi University of
Technology
For HEDSPI Project
Functions
• a group of declarations and statements
that is assigned a name
– effectively, a named statement block
– usually has a value
• a sub-program
– when we write our program we always define a
function named main
– inside main we can call other functions
• which can themselves use other functions, and so
on…
Example: Square
double square(double a)
{ This is a function defined
return a * a; outside main
}
int main(void)
{
double num = 0.0, sqr = 0.0;
printf("enter a number\n");
scanf("%lf",&num);
return 0;
}
Example: Square
double square(double a); /* nguyen mau ham */
int main(void)
{
double num = 0.0, sqr = 0.0;
printf("enter a number\n");
scanf("%lf",&num);
sqr = square(num);
double x1=x;
int i = 1;
do
{
lastsinx = sinx;
denominator = 2 * i * (2 * i + 1);
x1 = -x1 * x * x / denominator;
sinx = sinx + x1;
i = i + 1;
} while (epsilon <= fabs(sinx - lastsinx));
Goi y
• Cac ham nen co tham so: x va
epsilon
• Vi du: a= sinx(PI/2, 0.001);
• printf("%4.3f", emux(4,0.00001));
Bài tập bổ sung (BTVN2)
• Viết hàm xử lý cho phép thống kê số lần
xuất hiện của các ký tự nguyên âm (a,e,
u, i, y, o) trong dãy liên tục các ký tự
người dùng gõ từ bàn phím và đổi các ký
tự này sang dạng chữ Hoa.
int main() {
double fahr = 0;
printf("Fahrenheit\tCelsius\n");
while (fahr < 21) {
printf("%6.1f\t%6.1f\n", fahr, celsius(fahr);
fahr += 1;
}
return 0;
}
double celsius(double f) {
return 5 * (f - 32) / 9;
}
Exercise 12.7
• Given a positive number n which is
k-figure number. Write a function to
verify whether n has all figures being
odd numbers or even numbers.
Exercise
• The program Vietnamese Idol has 5 judges,
each of whom awards a score between 0
and 10 for each performer. Performer's final
score is determined by dropping the highest
and lowest score received, the averaging th
3 remaining scores. Write a program that
uses this method to calculate a contestant's
score using two following functions:
– void getJudgeData() should ask the user for a
judge's score, store it in a reference parameter
variable, and validate it.
– void calcScore() should calculate and display the
average score of performer.
Exercise: Leap Year
• Write an algorithm isLeapYear as a
function that determines whether a given
year is a leap year. Pass the year as a
parameter. A year is a leap year if
– It is a multiple of 4 but not a multiple of 100
OR
– It is a multiple of 400
– So, for example, 1996 and 2000 are leap
years, but 1900, 2002 and 2100 are not.
• Use this function in a program that asks
from user the month, year and return
the number of days of the month.
Bài tập bổ sung: Viện phí
• Chi phí bệnh viện của bệnh nhân gồm các khoản sau:
– Phí nội trú (150000 VND/ngày)
– Tiền thuốc: giá trị nguyên dương, lớn.
– Tiền phẫu thuật: (nếu có giá trị sẽ khác 0)
• Bệnh nhân có 3 loại thẻ bảo hiểm y tế sau:
– Gold(G): Chỉ cần thanh toán 30% chi phí
– Silver(S): Chỉ cần thanh toán 50% chi phí
– Citizen(C): Phải thanh toán 70% chi phí
• Thiết kế hàm hospitalFee với các tham số phù hợp để
tính số tiền bệnh nhân phải trả chỉ qua một lần gọi
hàm. Hàm vừa trả về tổng số tiền phải trả, vừa in ra
màn hình chi tiết các khoản thanh toán thành phần
của hóa đơn thanh toán. Chú ý giá trị các đối số
truyền vào hàm được nhập liệu ở chương trình chính,
không nhập liệu bên trong hàm.
Viện phí
• Sử dụng hàm trên để viết chương trình có giao diện
như sau
CHUONG TRINH TINH VIEN PHI
1. Thông tin bệnh nhân // Họ tên, loại thẻ bảo hiểm
và Thông tin khám chữa bệnh: //thông tin ngày nằm
viện,thuốc,..
2. Hóa đơn thanh toán viện phí //cho bệnh nhân hiện tại
3. Báo cáo thống kê
4. Thoát
• Trong đó chức năng báo cáo thống kê cần hiển
thị:
– Tổng số bệnh nhân (Chương trình cho phép nhập nhiều lần thông
tin) và số tiền trung bình một bệnh nhân phải trả.
– Số lượng thẻ bảo hiểm y tế đã khai báo cho mỗi loại G, S, C
– Số tiền lớn nhất mà bệnh nhân được hưởng từ bảo hiểm.