You are on page 1of 13

Bài thực hành 4: Xây dựng hàm

 Bài thực hành 1: Viết chương trình sau có sử dụng hàm:


- Nhập 1 số thực.
- Tính bình phương của số nhập vào.
-

 Bài thực hành 2: Viết chương trình sau có sử dụng hàm:


- Nhập một số nguyên n.
- Liệt kê các số nguyên tố nhỏ hơn n.
 Bài thực hành 3: Viết chương trình sau có sử dụng hàm:
- Nhập số nguyên dương n.
- In ra màn hình các ước số của n.
 Bài thực hành 4: Viết chương trình sau có sử dụng hàm:
- Nhập 1 số thập phân.
- Chuyển số thập phân sang số nhị phân.
 Bài thực hành 5: Viết chương trình sau có sử dụng hàm:
- Nhập số nguyên dương n.
- Kiểm tra số nguyên n có phải là số chính phương không.
Lưu ý: Số chính phương là bình phương (lũy thừa bậc 2) của một số tự nhiên. Ví dụ
25 là số chính phương.

 Bài thực hành 6: Viết chương trình sau có sử dụng hàm:


- Nhập số nguyên n.
- Liệt kê tất cả các số chính phương nhỏ hơn n.
Mã nguồn tham khảo:
 Bài thực hành 7: Viết chương trình sau có sử dụng hàm:
- Nhập số nguyên n.
- Kiểm tra đối số nguyên dương n có phải là số Amstrong hay không?
(Số Armstrong là một số có chữ số n bằng tổng của các số thứ n của các chữ số. Số đó là tổng các
chữ số riêng của mỗi số được nâng lên lũy thừa số chữ số.)
Ví dụ: 153 = 13 + 53 + 33.
- In ra các số Amstrong nhỏ hơn số n.
 Bài thực hành 8: Viết chương trình sau có sử dụng hàm:
- Nhập họ tên, điểm toán, điểm văn của 1 học sinh.
- Tính điểm trung bình và xuất ra kết quả.

 Bài thực hành 9: Viết chương trình sau sử dụng hàm:


- Nhập số nguyên n.
- Tính n!

 Bài thực hành 10: Viết chương trình sau sử dụng hàm:
- Nhập 2 số thực.
- Hoán vị 2 số.

 Bài thực hành 11: Viết chương trình hỗ trợ người dùng thực hiện các
phép tính gồm cộng, trừ, nhân và chia hai số.
- Nếu ký tự được nhập từ bàn phím là “+” thì phép tính cộng.
- Nếu ký tự được nhập từ bàn phím là “-” thì phép tính trừ.
- Nếu ký tự được nhập từ bàn phím là “*” thì phép tính nhân.
- Nếu ký tự được nhập từ bàn phím là “/” thì phép tính chia.
Yêu cầu thực hiện:
- Viết hàm tính tổng.
- Viết hàm tính trừ.
- Viết hàm tính nhân.
- Viết hàm tính chia.

 Bài thực hành 12: Viết chương trình sau có sử dụng hàm:
- Nhập số nguyên dương n.
- Tính tổng S = 1+ 2 + 3+ …..+ n , với n>0 (Viết hàm tính tổng - unsigned long
TongS (unsigned int n);

 Bài thực hành 13: Viết chương trình sau có sử dụng hàm:
- Nhập số nguyên n.
- Kiểm tra số n có phải là số hoàn hảo hay không (Viết hàm Perfect (n)).
Lưu ý: Số hoàn hảo là số nguyên dương bằng tổng các ước thực sự của nó. Ví dụ: 6 =
1+ 2+3.

 Bài thực hành 14: Viết chương trình sau có sử dụng hàm:
- Nhập họ tên, lương, thưởng của một nhân viên
- Tính tổng lương và in kết quả ra màn hình.

 Bài thực hành 15: Viết chương trình sau có sử dụng hàm:
- Nhập 1 dãy số.
- Tìm min/max của dãy số nhập vào.
- Tính trung bình của 2 số min và max.
* Exercise 16: Markup
Write a program that asks the user to enter an item’s wholesale cost and its markup
percentage. It should then display the item’s retail price. For example:
• If an item’s wholesale cost is 5.00 and its markup percentage is 100%, then the
item’s retail price is 10.00.
• If an item’s wholesale cost is 5.00 and its markup percentage is 50%, then the
item’s retail price is 7.50.
The program should have a function named calculateRetail that receives the
wholesale cost and the markup percentage as arguments and returns the retail price
of the item.
Input Validation: Do not accept negative values for either the wholesale cost of the
item or the markup percentage.

* Exercise 17: Rectangle Area


Write a program to calculate the area of a rectangle and then display the
rectangle’s area. The program calls the following functions, which have not been
written:
• getLength – This function should ask the user to enter the rectangle’s length and
then return that value as a double.
• getWidth – This function should ask the user to enter the rectangle’s width and
then return that value as a double.
• getArea – This function should accept the rectangle’s length and width as
arguments and return the rectangle’s area. The area is calculated by multiplying
the length by the width.
• displayData – This function should accept the rectangle’s length, width, and area
as arguments and display them in an appropriate message on the screen.

* Exercise 18: Winning Division


Write a program that determines which of a company’s four divisions (Northeast,
Southeast, Northwest, and Southwest) had the greatest sales for a quarter. It should
include the following two functions, which are called by the main function.
• double getSales() is passed the name of a division. It asks the user for a division’s
quarterly sales figure, validates the input, then returns it. It should be called once
for each division.
• void findHighest() is passed the four sales totals. It determines which is the
largest and prints the name of the high grossing division, along with its sales
figure.
Input Validation: Do not accept dollar amounts less than $0.00

* Exercise 19: Find the biggest number


Given 2 numbers n and m (n<m), find a number in range from n to m such that
the sum of its digits is maximum. If there are several such integers, determine
the biggest of them. If n>m then return F.

Example:
Input: n = 50; m = 100
Output: 99

Input: n = 50; m = 98
Output: 98
Explanation:
There are two numbers with the maximum digit sum: 98 and 89.
Since 98 > 89, so 98 is the answer.

* Exercise 20: Derivative of a function


Create a function that takes numbers x and y as arguments and returns the
derivative of the function f(x) = x^y.
Example:
Input: x = -3; y = 4
Output: f’(x) = -108
* Exercise 21: Economical Numbers
A number is Economical if the quantity of digits of its prime factorization
(including exponents greater than 1) is equal or lower than the digits quantity of
the number itself.
Given an integer n, implement a function that returns from 1 to 3:
Return “1”: "Equidigital" if the quantity of digits of the prime factorization
(including exponents greater than 1) is equal to the quantity of digits of n;
Return “2”: "Frugal" if the quantity of digits of the prime factorization (including
exponents greater than 1) is lower than the quantity of digits of n;
Return “3”: "Wasteful" if none of the two above conditions is true.
Example 1:
is_economical(14) returns 1 (Equidigital)
//The prime factorization of 14 (2 digits) is [2 x 7] (2 digits).
//Exponents equal to 1 are not counted.
Example 2:
is_economical(125) returns 2 (Frugal)
//The prime factorization of 125 (3 digits) is [5^3] (2 digits).
//Notice how exponents greater than 1 are counted.
Example 3:
is_economical(1024) returns 2 (Frugal)
//The prime factorization of 1024 (4 digits) is [2^10] (3 digits).
Example 4:
is_economical(30) returns 3 (Wasteful)
//The prime factorization of 30 (2 digits) is [2 x 3 x 5] (3 digits).

* Exercise 22: Candy Parcel


We need to assemble a parcel of ordered candy. We got two types of candies:
 Small parcel (2 candies each parcel)
 Big parcel (5 candies each parcel)
Create a function that takes three parameters: Number of small available candies
nSmall, number of big candies available nBig and desired amount of the final
parcel order.
The function should return the required number of small candies to achieve the
goal. The function should return -1 if the goal cannot be achieved by any possible
combinations of candies.
Note:
First use of big parcel that are available to achieve the desired goal. And only
then should you proceed to use the small parcel.
You can't break candies into small pieces.
Example:
CandyParcel(4, 1, 13) returns 4
// 4 small parcel = 8 candies
// 1 big parcel = 5 candies
// 8 + 5 = 13 candies
// Required number of small candies = 4

CandyParcel (4, 1, 14) returns -1


// You cannot make any combination to reach 14.

CandyParcel (2, 1, 7) returns 1


// 1 big parcel = 5 candies
// 1 small parcel = 2 candies
// 5 + 2 = 7
// Required number of small candies = 1

* Exercise 23: Reorder the digits


Create a function that reorders the digits of x (x has 4 digits) based on ascending
(asc) or descending (desc) order.
Example:
Input: X = 1543
Ouput: ASC(x) = 1345
DESC(x) = 5421

* Exercise 24: Primorial of a Number


In mathematics, primorial, denoted by “#”, is a function from natural numbers to
natural numbers similar to the factorial function, but rather than successively
multiplying positive integers, the function only multiplies prime numbers.
Create a function that takes an integer n (n > 1) and returns its primorial.
Explanation:
n = 6# signifies the product of the first 6 primes
Examples:
primorial(1) returns 2
primorial(2) returns 6
primorial(6) returns 30030

* Exercise 25: Polydivisible Numbers


Mubashir was reading about Polydivisible Numbers on Wikipedia.
In mathematics a Polydivisible Number (or magic number) is a number in a given
number base with digits abcde... that has the following properties:
 Its first digit a is not 0.
 The number formed by its first two digits ab is a multiple of 2.
 The number formed by its first three digits abc is a multiple of 3.
 The number formed by its first four digits abcd is a multiple of 4.
 etc.
Create a function which takes an integer n and returns true if the given number is
a Polydivisible Number and false otherwise.
Example:
Input: 345654
Output: 345654 is Polydivisible number.
Explanation:
The first digit of the number is non-zero.
The number formed by the first 2 digits (34) is divisible by 2.
The number formed by the first 3 digits (345) is divisible by 3.
The number formed by the first 4 digits (3456) is divisible by 4.
The number formed by the first 5 digits (34565) is divisible by 5.
The number formed by the first 6 digits (345654) is divisible by 6.

* Exercise 26: Find Area of Circle


Program to find Area of Circle using Pointer
Example:
Input: 5
Output: 78.5

* Exercise 27: Find the factorial


Program to find the factorial using pointers
Example:
Input: 6
Output: 720

* Exercise 28: Swap Number


Program for Swap Numbers Using Pointers
Example:
Input: A = 20
B = 40
Output: A = 40
B = 20

* Exercise 29: Overloading function: Add two parameters of different data types
Create functions which find the sum of two numbers of type int, float and double. The
overloaded function add() takes two parameters of different data types.
Example:
Input:
Int: x= 25; y=50;
Float: a= 12.65; b=116.50;
Double: m= 1234.567; n=2468.357;
Output:
Sum int: 75
Sum float: 129.15
Sum double: 3702.92
Bài thực hành 5: Xây dựng hàm (tt)

1. Mục tiêu
- Xây dựng hàm sử dụng tham số là con trỏ.
- Xây dựng hàm đệ quy.
- Sử dụng các thư viện của ngôn ngữ lập trình C.
- Xây dựng các ứng dụng tổng hợp: nhập xuất, lệnh điều kiện, lệnh lặp, hàm.
2. Số tiết: 4
3. Nội dung thực hành
Giảng viên lựa chọn 1 số bài cho sinh viên thực hành tại lớp và các bài tập khác để
sinh viên tự thực hành tại nhà.
 Bài thực hành 1: Viết chương trình hoán vị 2 số, sử dụng hàm có tham số là con
trỏ.   
 Bài thực hành 2: Viết chương trình tìm số lớn nhất trong 3 số thực, sử dụng hàm
có tham số là con trỏ.   

 Bài thực hành 3: Viết chương trình tăng giá trị của biến nơi con trỏ đang trỏ đến
lên 1 đơn vị, sử dụng hàm có tham số là con trỏ.   

 Bài thực hành 4: Viết chương trình sau sử dụng hàm đệ quy:   
- Nhập số nguyên n.
- Tính n!

 Bài thực hành 5:  Viết chương trình sau sử dụng hàm đệ quy:   
- Nhập số nguyên n.
- Tính số hạng thứ n của dãy Fibonaci.

F(n) =

 Bài thực hành 6:  Viết chương trình sau sử dụng hàm đệ quy:   
- Nhập một số nguyên.
- Kiểm tra số nguyên có bằng tổng giai thừa các chữ số của nó hay không.
Ví dụ: 145!=1!+4!+5!

 Bài thực hành 7: Viết chương trình sau sử dụng hàm đệ quy:   
- Nhập số nguyên dương n.
- Đếm số lượng chữ số của một số nguyên dương n.

 Bài thực hành 8:   Viết chương trình sau sử dụng hàm đệ quy:   
- Nhập số nguyên dương n.
- Tính tổng các số từ 1 đến n.

 Bài thực hành 9:  Viết chương trình sau sử dụng hàm đệ quy:   
- Nhập 2 số nguyên a, b 
- Tìm ước số chung lớn nhất của 2 số a, b.
- Tìm bội số chung nhỏ nhất của 2 số a, b.
 Bài thực hành 10:  Viết chương trình cho bài toán Tháp Hà Nội.
Dưới đây là hình minh họa cách giải bài toán Tháp Hà Nội (Tower of Hanoi) với
trường hợp có 3 đĩa.

Di chuyển các đĩa có kích cỡ khác nhau sang cột khác sao cho vẫn đảm bảo thứ tự
ban đầu của các đĩa: đĩa nhỏ nằm trên đĩa lớn.
Dưới đây là một số qui tắc cho trò chơi toán học Tháp Hà Nội (Tower of Hanoi):
- Mỗi lần chỉ có thể di chuyển một đĩa từ cột này sang cột khác.
- Chỉ được di chuyển đĩa nằm trên cùng (không được di chuyển các đĩa nằm
giữa).
- Đĩa có kích thước lớn hơn không thể được đặt trên đĩa có kích thước nhỏ hơn.
Mã nguồn tham khảo:
#include<stdio.h>

 
void TOH(int num, char x, char y, char z);

 
int main() {
int num;
printf("\nNhap so dia: ");
scanf("%d", &num);
 
TOH(num - 1, 'A', 'B', 'C');
return (0);
}

 
void TOH(int num, char x, char y, char z) {
if (num > 0) {
   TOH(num - 1, x, z, y);
   printf("\n%c -> %c", x, y);
   TOH(num - 1, z, y, x);
}
}

 Bài thực hành 11: Tính 1+1/2 + 1/3 + ... + 1/n


Gợi ý:
float Tinh (int n)
{
    if(n==1)
        return 1;
    return Tinh(n-1) + 1/n;
}

 Bài thực hành 12: Tính 1/2 + 1/4 +...+ 1/2n


Gợi ý:
float Tinh (float x)

{
    if (x==1)
        return 1 ;
   
    return Tinh(x-1) + 1/(2*x) ;
}

 Bài thực hành 13: Tính S(n) = 1/(1*2) + 1/(2*3) + 1/( n(*n-1))
Gợi ý:
float Tinh (float x)
{
    if (x==1)
        return 0.5 ;
    return Tinh(x-1) + 1/(x*(x+1)) ;

}
 Bài thực hành 14: Tính S(n) = 1/2 + 3/4 + 5/6 + ... + (2n+1)/(2n+2)
Gợi ý:
float Tinh (float x)
{
    if (x==1)
        return 0.5 ;
    return Tinh(x-1) + (2*x-1)/(2*x) ;

 Bài thực hành 15: Tính S(n) = 1 + 1.2 + 1.2.3 + .... + 1.2.3....n
Gợi ý:
long GT(int x)
{
    if(x==1)
        return 1;

    return GT(x-1)*x;
}
long Tinh (int x)
{
    if (x==1)
        return 1 ;

    return Tinh (x-1) + GT(x-1)*x;


}

 Bài thực hành 16: Tính P(x,n) = x^n


Gợi ý:
float luythua(float x , int n)
{
    if(n==0)
        return 1;
    if(n<0)
        return luythua(x,n+1) * 1/x ;
    return luythua(x,n-1)*x ;

 Bài thực hành 17: Tính U(n)=x + x^2 + x^3 +...+x^n


Gợi ý:
long luythua(int x , int n)
{
    if(n==0)
        return 1;
    return luythua(x,n-1)*x ;

long Tinh(int x , int n)


{
    if(n == 1)
        return x;
    return Tinh(x,n-1) + luythua(x,n-1)*x;

You might also like