Sunday, September 28, 2014
Tuesday, September 23, 2014
[LBC] Test day 1 - 2:
#include <stdio.h>
#include <stdlib.h>
#define MAX 12
int main(int argc, char *argv[]) {
float ftemp, months[12];
int i,j;
for (i = 0; i < 12; i++){
printf("Enter sales amount of month %d : ", i+1);
scanf("%f", &months[i]);
}
for (i = 11; i > 0; i--){
for (j = 11; j >= 12 - i; j--){
if (months[j] > months[j-1]){
ftemp = months[j-1];
months[j-1] = months[j];
months[j] = ftemp;
}
}
}
printf("After sort descending:\n");
for (i = 0; i < 12; i++){
printf("%.2f ", months[i]);
}
printf("\nTop 3 months with highest sales amount\n");
for (i = 0; i < 3; i++){
printf("%.2f \n", months[i]);
}
return 0;
}
[LBC] Day 7 -1: Excercise Struct
1. Write a C Program to store student data in a structure. The data should include student ID, name, course registered for, and year of joining. Write a function to display the details of students enrolled in a specified academic year. Write another function to locate and display the details of a student based on a specified student ID.
To do this,
a. Define the structure to store the student details.
b. Declare and initialize the structure with details of 10 students.
c. Set a loop to display a menu for the operations to be performed.
d. Accept the menu choice and invoke appropriate functions with the structure array as parameter.
e. In the function to display students for a year, accept the year. Set a loop to check each student’s enrollment year, and display if it matches. At the end, allow the user to specify another year.
In the function to locate student details, accept the student ID. Set a loop to check each student’s ID, and display if it matches. At the end, allow the user to specify another student ID.
2. Write a C program to store 5 lengths in a structure array. The lengths should be in the form of yards, feet and inches. Sort and display the lengths.
3. Write a C Program to store employee details in a structure array. The data should include emploee ID, name, salary, and date of joining. The date of joining should be stored in a structure. The program should perform the following operations based on a menu selection:
a. Increase the salaries according to the following rules:
Salary Range
|
Percentage increase
|
<= 2000
|
15%
|
> 2000 and <= 5000
|
10%
|
>5000
|
No increase
|
b. Display the details of employees who complete 10 years with the company.
4. Write a C program to implement an inventory system. Store the item number, name, rate and quantity on hand in a structure. Accept the details for five items into a structure array and display the item name and its total price. At the end, display the grand total value of the inventory.
5. Write a C program to store the names and scores of 5 students in a structure array. Sort the structure array in descending order of scores. Display the top 3 scores.
* Chú ý:
- Struct là một kiểu dữ liệu trong C, nó có thể chứa nhiều biến chứa nhiều biến có kiểu dữ liệu khác nhau, trong nó có thể có số nguyên, số thực, chuỗi và có thể chứa các kiểu struct khác nữa.
Cú pháp khai báo struct:
struct tên kiểu struct
{
kiểu dữ liệu_các thuộc tính của struct;
struct _ tên kiểu struct;
};
- Ta có thể tạo các mảng struct bằng cách khai báo : struct_ tên kiểu struct_ tên mảng struct[số lượng ];
- Khi khai báo kiểu struct trong hàm và nguyên mẫu hàm, chú ý phải khai báo đủ: struct_tên kiểu struct_tên struct[]
- Để tránh lỗi bộ nhớ đệm khi phải nhập một chuỗi sau một số thì khi nhập số ta lưu số đó vào một chuỗi tạm thời và dùng hàm atoi() để chuyển chuỗi tạm thời đó thành số và gán số đó vào struct.
Sunday, September 21, 2014
[LBC] Day 6 - 3: Excercise Function - Pass by references
1. Viết hàm truyền vào 2 số nguyên bất kỳ, thực hiện đổi chỗ 2 số đó
Nguyên mẫu hàm như sau:
void swap (int &number1, int &number2);
2. Viết hàm truyền vào một mảng số nguyên bất kỳ, sau đó đảo ngược mảng số nguyên đó
void reverse(int array[]);
3. Viết hàm truyền vào một mảng ký tự, và một ký tự bất kỳ, kiểm tra xem ký tự bất kỳ đó có nằm trong mảng không. Nếu có trả về số lần xuất hiện của ký tự đó, nếu không trả về -1
int countCharacter(char array[], char ch);
*Chú ý:
- Kiểu dữ liệu trả về của một hàm liên quan đến câu lệnh return, khi ta dùng return về giá trị nào thì giá trị trả về của hàm cũng sẽ có kiểu dữ liệu đó.
- Khi không có dữ liệu trả về thì kiểu dữ liệu của hàm là kiểu rỗng (void) và ta không có câu lệnh return trong hàm.
- Trong bài số 1, để đổi chỗ 2 số ta phải thay đổi địa chỉ chứa 2 số đó khi truyền tham số vào hàm, lý do phải truyền địa chỉ vì nếu chỉ truyền biến bình thường thì khi chương trình gọi hàm nó sẽ tạo ra các biến tạm thời và sẽ không thay đổi được biến thực tế khi truyền vào hàm.
[LBC] Day 6 - 2: Excercise Function - Pass by value
1. Write a function which accepts an integer value as input, and returns an integer which is the cube of that input
2. Write a function that accepts a char as input and returns true if the char is a digit from 0 to 9 or false if the character is not a digit from 0 to 9
3. Write a function named Smallest that takes three integer inputs and returns an integer that is the smallest of the three inputs. Write the prototype for the Smallest function. Write a program that gets 3 integers from a user and displays the smallest.
4. Write a function that, given a letter of the alphabet, returns true if the letter is a vowel (lower or uppercase) and returns false if the letter is not a vowel.
Khi khai báo mẫu nguyên hàm, trong phần kiểu dữ liệu của các biến số ta có thể chỉ khai báo kiểu dữ liệu chứ không cần phải viết cả tên các biến.
[LBC] Day 6 - 1: Excercise Function
1. Write a C program that accepts a number and square the number with the help of a function.
2. Write a C program to find the area and perimeter of a circle.
3. Write a C program to calculate the factorial of an integer
Function prototype: long factorial(int number);
4. Write a program with a function that takes two int parameters, adds them together, then returns the sum. The program should ask the user for two numbers, then call the function with the numbers as arguments, and tell the user the sum.
Function prototype: int calSum(int number1, int number2);
* Function:
Cấu trúc của function có dạng:
Kiểu dữ liệu trả về Tên hàm(kiểu dữ liệu và các biến số){Khối lệnh}
Để sử dụng hàm trong chương trình chính, trước tiên ta phải khai báo mẫu nguyên hàm có dạng:
Kiểu dữ liệu trả về Tên hàm(kiểu dữ liệu và các biến số);
Nguyên hàm sẽ có thêm dấu ";" đằng sau và không có khối lệnh.
Cấu trúc của function có dạng:
Kiểu dữ liệu trả về Tên hàm(kiểu dữ liệu và các biến số){Khối lệnh}
Để sử dụng hàm trong chương trình chính, trước tiên ta phải khai báo mẫu nguyên hàm có dạng:
Kiểu dữ liệu trả về Tên hàm(kiểu dữ liệu và các biến số);
Nguyên hàm sẽ có thêm dấu ";" đằng sau và không có khối lệnh.
Thursday, September 18, 2014
[LBC] Day 5 - 2: Character array
1. Write a program which prints the letters in a char array in reverse order. For
example, if the array contains {'c', 's', 'c', '2', '6', '1'}the output (to the terminal)
should be "162csc"
2. Write a program : declare a char array and the size of the array (of type char). This function counts the number of digit letters in the char array.
3. Write a program that counts the number of words in a string
4. Write a program that accept two string, after checking if string1 equals string2 then print 'string 1 equals string2', else print string1 less than or greater than string 2.
5. Write a function that scans a character array for the character - and replaces it with _
Chú ý:
- Với chuỗi ký tự ta cần nhớ ký tự cuối cùng mà chuỗi lưu trữ là ký tự NULL (\0) để máy tính xác định điểm kết thúc của chuỗi.
- Áp dụng lý thuyết trên ta có đk để duyệt toàn bộ các phần tử của chuỗi ký tự là i chạy tăng dần từ 0 cho đến khi nào array[i] = '\0' thì ta kết thúc việc duyệt chuỗi. Hoặc có thể cho i < sizeof(arr)/sizeof(char), cách này sẽ duyệt hết chuỗi và bỏ qua ptử cuối cùng của chuỗi là '\0'
- Sự khác nhau giữa mảng các ký tự và chuỗi ký tự là ở chỗ ptử cuối cùng của chuỗi ký tự là '\0', nếu k có ký tự NULL kết thúc đó thì chuỗi ký tự chỉ là mảng các ký tự riêng lẻ mà thôi.
- Phép gán arr = "Ha noi" sẽ báo lỗi do ta gán một chuỗi ký tự vào ptử đầu tiên của mảng ký tự là không đúng
Subscribe to:
Posts (Atom)