Thứ Ba, 10 tháng 2, 2015

Bài 6.1

I.Lý Thuyết

II.Bài Tập

1. Write a C program that accepts a number and square the number with the help of a function.
To do this,
a. Declare a function.
b. Accept the number.
c. Pass the number to the function and return the square of that number

Function prototype: int cal_square(int number);

2. Write a C program to find the area and perimeter of a circle.
Function prototype: float areaCircle(float radius);
Function prototype: float perimeterCircle(float radius);

Using the library: “Math.h” => PI = 3.14

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);
 Bài Làm :
 
Bài 1: 

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int S(int a);

int main(int argc, char *argv[]) {
    int x =4;

    int sub(x);
    printf("S=%d",S(x));
    return 0;
   
}
int S(int a){
    int S=a*a;
    return S;
}
 Bài 2
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
float C(float x);
float S(float x);
int main(int argc, char *argv[]) {
    float R;
    printf("Nhap Ban Kinh R =");
    scanf("%f",&R);
    float a = C(R);
    float b =S(R);
    printf("Chu Vi Hinh Tron = %f",a);
    printf("\nDien tich Hinh Tron = %f",b);
    return 0;
}
float S(float x){
    float S=PI*x*x;
    return S;
}
float C(float x){
    float C=2*PI*x;
    return C;
}
Bài 3

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int GiaiThua(int T);
   

int main(int argc, char *argv[]) {
    int S;
    printf("Nhap Mot So :");
    scanf("%d",&S);
    int C = GiaiThua(S);
    printf("%d ! = %d",S,C);
   
    return 0;
}
int GiaiThua(int T){
    int a=1;
    int b=1;
    for(;a<=T;a++){
        b=b*a;
    }
    return b;
}
Bài 4
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int Tong(int ,int );
   

int main(int argc, char *argv[]) {
    int s;
    printf("Nhap So :");
    scanf("%d",&s);
    int f;
        printf("Nhap So :");
        scanf("%d",&f);
    printf("Tong = %d",Tong(s,f));
   
    return 0;
}
int Tong(int s, int f){
   

     int t;
     t= s+f;

 return t;
}

6.3


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


Bài làm

Bài 1.

#include <stdio.h>
#include <stdlib.h>
void s (int *x, int *y);
int main(int argc, char *argv[]) {
    int x,y;
 printf (" Nhap So : ");
scanf("%d",&x);
printf (" Nhap So : ");
 scanf("%d",&y);
 s(&x,&y);
 printf("Hoan Vi. : %d  %d",x,y);

 return 0;
}
void s (int *x, int *y){
 int z = x;
 x = y;
 y = z;
}

Bài 2
#include <stdio.h>
#include <stdlib.h>

void reverse(int array[10]);

int main(int argc, char *argv[]) {
 int So[10],i;
 for(i=0;i<10;i++) {
 printf("So[%d]=",i+1);
 scanf("%d",&So[i]);
 }
 reverse(So);
}
void reverse(int array[10]) {
 int i;
 for(i=9;i>=0;i--) {
  printf("\n%d",array[i]);
 }
 return reverse;
}


6.2

Bài Tập

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.
Bài Làm

Bài 1
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int LapPhuong(int );
   

int main(int argc, char *argv[]) {
    int A;
    printf("Nhap So :");
    scanf("%d",&A);
   
    printf("Lap Phuong = %d",LapPhuong(A));
   
    return 0;
}
int LapPhuong(int A){
   

     int t;
     t= A*A*A;

 return t;
}

Bài 2
 #include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char A(char );
   

int main(int argc, char *argv[]) {
    char B;
    printf("Nhap Mot Ki Tu :");
    scanf("%c",&B);
   
if(B>='0'&&B<='9')    {
    printf("True");
}
else printf("False");
   
    return 0;
}
char A(char B ){

if(B>=0&&B<=9)   
   

 return B;
}

Bài 3.
 #include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int A(int,int,int );
   

int main(int argc, char *argv[]) {
    int a,b,c;
    printf("Nhap So :");
    scanf("%d",&a);
    printf("Nhap So :");
    scanf("%d",&b);
    printf("Nhap So :");
    scanf("%d",&c);
    printf("So nho nhat trong 3 So la :%d",A(a,b,c));
    return 0;
}
int A(int a,int b, int c ){

int i=a;
if(i>b) i=b;
if(i>c) i=c;
   

 return i;
}

Bài 4
 #include <stdio.h>
#include <stdlib.h>
/* 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. */
int N(char);
int main(int argc, char *argv[]) {
     char x;
 printf ("Nhap Chu: ");

 scanf("%c",&x);
 if (N(x)) printf ("%c =Nguyen Am",x);
  else printf ("%c K lA Nguyen Am",x);


 return 0;
}
int N(char x){
 int N;
 if (x=='u'||x=='U'||x=='e'||x=='E'||x=='o'||x=='O'||x=='a'||x=='A'||x=='i'||x=='I') N= 1;
  else N=0;
 return N;
}

Thứ Năm, 5 tháng 2, 2015

Bài 5.1

I.Lý Thuyết
Cấu trúc :
*Cú pháp: <Kiểu> <Tên mảng ><[số phần tử]>
- Tên mảng: đây là một cái tên đặt đúng theo quy tắc đặt tên của danh biểu. Tên này
cũng mang ý nghĩa là tên biến mảng.
- Số phần tử: là một hằng số nguyên, cho biết số lượng phần tử tối đa trong mảng là
bao nhiêu (hay nói khác đi kích thước của mảng là gì).
- Kiểu: mỗi phần tử của mảng có dữ liệu thuộc kiểu gì.

*Mảng :Mảng là một dãy các phần tử có cùng kiểu được đặt liên tiếp trong bộ nhớ và có thể truy xuất đến từng phần tử bằng cách thêm một chỉ số vào sau tên của mảng. 
II.Bài Tập
Bài 1 : Write a program that asks the user to type 10 integers of an array. The program must compute and write how many integers are greater than or equal to 10.
 

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
        int A[10];
        int n;
        int C=0;
        for(n=0;n<10;n++){
            printf("Nhap A[%d]=",n+1);
            scanf("%d",&A[n]);
        }
        printf("\n Cac so trong lon hon = 10 trong day :");
        for(n=0;n<10;n++){
           
            if(A[n]>=10){
                printf("%d ",A[n]);
            C=C+1;
       
       
        }
       
    }

   
    return 0;
}
Bài 2 :Write a program that asks the user to type 10 integers of an array. The program must output the largest element in the array, and the index at which that element was found.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
        int A[10];
        int n;
       
        for(n=0;n<10;n++){
            printf("Nhap A[%d]=",n+1);
            scanf("%d",&A[n]);
        }
      
        int max=A[0];
        
        for(n=0;n<10;n++){
           
            if(A[n]>max){
           
            max=A[n];
       
       
        }
       
       
    }
printf("Max=%d",max);
for(n=0;n<10;n++){
    if(A[n]==max)
    printf("\nVi tri=%d",n+1);
}
   
    return 0;
}
Bài 3:Input values are accepted from the user into the array. Displays highest of the entered values. Prints average of values entered.
#include <stdio.h>

#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
     int A[5];
     int i;
     for(i=0;i<5;i++){
         printf("Nhap A[%d]=",i+1);
         scanf("%d",&A[i]);
     }
     int max=A[0];
     for(i=0;i<5;i++){
         if(A[i]>max){
             max=A[i];
         }
     }
     printf("Max=%d",max);
     int t;
     for(i=0;i<5;i++){
         t=t+A[i];
     }
     printf("\nTrung Binh Cong =%d",t/i);
        return 0;

}

Bài 4 Write a program that accepts the following numbers in an array and reverses the array
#include <stdio.h>
#include <stdlib.h>

/* Write a program that accepts the following numbers in an array and reverses the array */

int main(int argc, char *argv[]) {
 int t;
 printf("So Luong  Gia Tri Trong Mang= ");
 scanf("%d",&t);
 int A[t],B[t],i;
 for (i=0;i<t;i++){
  printf("A[%d]= ", i+1);
  scanf("%d",&A[i]);
B[t-i-1] = A[i];                                                                                                                                          
 }

 for (i=0;i<t;i++) printf("\n\tA[%d]= %d\t",i+1,A[i]);

 for (i=0;i<t;i++) printf("\nB[%d]= %d\t",i+1,B[i]);
 return 0;
}


Bài 5
. Write a program to count the number of vowels in a line of text
Bài 6 :
Write a program that asks the user to type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers. The program writes "V is in the array" or "V is not in the array".

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int A[10];
int i;
for(i=0;i<10;i++){
printf("A[%d]=",i+1);
scanf("%d",&A[i]);
}
int V;
printf("Nhap V =");
scanf("%d",&V);
int d;
d=0;
for(i=0;i<10;i++)

if(V==A[i])
d=1;
if(d==1)
printf("V co trong mang");

else printf("V khong co trong mang");


return 0;
}

Bài 7 :
Write a program that asks the user to type 10 integers of an array and an integer value V. The program must search if the value V exists in the array and must remove the first occurrence of V, shifting each following element left and adding a zero at the end of the array. The program must then write the final array

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int A[10];
    int i;
    for(i=0;i<10;i++){
        printf("A[%d]=",i+1);
        scanf("%d",&A[i]);
    }
    int V;
    printf("Nhap V =");
    scanf("%d",&V);
for(i=0;i<10;i++) if(V==A[i]) break;
 for (i;i<10;i++) A[i]=A[i+1];
 A[9]=0; printf("\n\n\n");
 for (i=0;i<10;i++) printf("%d  ",A[i]);

    return 0;
}
Bài 8 :
Write a program that asks the user to type 10 integers of an array and an integer value V and an index value i between 0 and 9. The program must put the value V at the place i in the array, shifting each element right and dropping off the last element. The program must then write the final array



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int A[10];
    int j;
    for(j=0;j<10;j++){
        printf("A[%d]=",j+1);
        scanf("%d",&A[j]);
    }
    int V,i;
    printf("Nhap V =");
    scanf("%d",&V);
    printf("Nhap i =");
    scanf("%d",&i);
    for(j=9;j>i;j--)
    A[i]=V;
    printf("final array :");
    for(j=0;j<10;j++)
    printf("\t%d",A[j]);
    return 0;
}

Bài 9 :
Write a program that asks the user to type 10 integers of an array. The program will then display either "the array is growing", "the array is decreasing", "the array is constant", or "the array is growing and decreasing."

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int A[10];
    int j,i,m,n;
    for(j=0;j<10;j++){
        printf("A[%d]=",j+1);
        scanf("%d",&A[j]);
    }
    i=m=n=0;
    for(j=0;j<9;j++){
   
    if(A[j]==A[j+1])
    i=i+1;
    if(A[j]>A[j+1])
    m=m+1;
    if(A[j]<A[j+1])
    n=n+1;
}
if(i==9){
printf("Mang lien tuc");}
else if(m==9){
    printf("Mang dang giam");
}
else if(n==9){
    printf("Mang dang tang");
}
else printf("Mang vua tang vua giam");
    return 0;
}

Bài 10 :
10. Write a program that asks the user to type 10 integers of an array. The program will then sort the array in descending order and display it.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
 int A[10],i,a,b;
 for(i=0;i<10;i++) {
  printf("A[%d]:",i+1);
  scanf("%d",&A[i]);
 }
 for(i=0;i<10;i++) {
  for(a=0;a<10;a++) {
   if(A[a]>A[a-1]) {
   b=A[a-1];
   A[a-1]=A[a];
   A[a]=b;
   }
  }
 }
 for(i=0;i<10;i++) {
  printf("%d ",A[i]);
 }
 return 0;
}

Bài 11 :
11. Write a program which takes 2 arrays of 10 integers each, a and b. c is an array with 20 integers. The program should put into c the appending of b to a, the first 10 integers of c from array a, the latter 10 from b. 




include <stdio.h>
#include <stdlib.h>

/* Write a program which takes 2 arrays of 10 integers each, a and b.
c is an array with 20 integers. The program should put into c the appending of b to a,
the first 10 integers of c from array a, the latter 10 from b. Then the program should display c */

int main(int argc, char *argv[]) {
 int A[20],i;
 for(i=0;i<10;i++){
  printf("A[%d]=",i+1);
  scanf("%d",&A[i]);
 } 
 for(i=10;i<20;i++){
  printf("B[%d]=",i-9);
  scanf("%d",&a[i]);
 } 
 for(i=0;i<20;i++) printf("C[%d]=%d\t",i+1,a[i]);
 printf("\n\n\n");
 system("pause");
 return 0;
}

5.2

I.Lý Thuyết
Định nghĩa ...Là một chuỗi kí tự kết thúc bằng kí tự NULL
Định Nghĩa không thể bịa nên em chỉ chép đúng như những lời cô giảng :-ss
II-Bài Tập.
Bài 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"

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    char a[6]={'c','s','c','2','6','1'};
    int i;
    for(i=5;i>=0;i--)
    printf("\ta[%d]=%c",i,a[i]);
    return 0;
}
Bài 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.

char a[30];
int i,j=0;
printf("Nhap Chuoi Ki Tu :");
scanf("%c",&a);

for (i=0;i<30;i++)
if (a[i] != 0)

j++;
printf("so ki tu trong chuoi la: %d",j-5);

return 0;
Bài 3 :
Write a program that counts the number of words in a string
 #include <stdio.h>
#include <stdlib.h>

/* Write a program that counts the number of words in a string */

int main(int argc, char *argv[]) {
char a[30];
int i,j;
printf("Nhap Chuoi: ");
scanf("%c",&a);
j=1;
for(i=0;i<30;i++) if (a[i]==' ') j=j+1;
printf("So Tu: %d ",j);

return 0;
}
Bài 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.
#include <stdio.h>
#include <stdlib.h>

/* Write a program that counts the number of words in a string */

int main(int argc, char *argv[]) {
char a[30];
char b[30];
printf("Nhap Chuoi: ");
scanf("%c",&a);
printf("Nhap Chuoi : ");
scanf("%c",&b);
if(a,b==0){

printf("a va b bang nhau");}

else {
printf("a va b khong bang nhau");

}

return 0;
}
Bài 5 :
Write a function that scans a character array for the character - and replaces it with _

 

 
#include <stdio.h>
#include <stdlib.h>

/* Write a program that counts the number of words in a string */

int main(int argc, char *argv[]) {
char a[30];
int i;
printf("Nhap Chuoi: ");
scanf("%s",&a);
for(i=0;i<30;i++){
if(a[i]=='-')
a[i]='_';
}
printf("%s",a);

return 0;
}