1 What are data types?

  • Use sizeof to check the size of data type on your system
  • int/long
  • char
  • float/double
#include <stdio.h>
int main()
{
    long sum  = 23L;
    float x = 2.181828;
    printf("sizeof(x) = %d\n",sizeof(x));
    printf("sizeof(float) = %d\n",sizeof(float));
    printf("sizeof(3.1) = %d\n",sizeof(3.1));
    printf("sizeof(3.14159) = %d\n",sizeof(3.14159));
    printf("sizeof(3.14159f) = %d\n",sizeof(3.14159f));
    printf("sizeof(3.1L) = %d\n",sizeof(3.1L));
    printf("sizeof(3.14159l) = %d\n",sizeof(3.14159l));
  
    printf("x = %f\n",x);
    printf("sum = %ld, sum = %ld\n",sum, 23l);
    return 0;
}

2 Common errors for beginners

  • recognize ASCII table
  • know the difference between characters ('1') and numeric numbers (1)
  • know the result of int/int is int
  • use 5.0/3 to obtain 1.666667 instead of 5/3
  • use 1.0*x/y to obtain 1.666667 if declaring int x=5, y=3;
#include <stdio.h>
#include <math.h>
int main(){
    int a,b,x=5,y=3;
    double z;

    char c='1';
    printf("'1'+'1'=%d\n",'1'+'1');
    printf("'1'+49=%d\n",'1'+49);
    printf("'A' = %d,\t 'a' = %d, \t'A'+32 = %c \n", 'A', 'a', 'A'+32);

    printf("5/3=%d,\t 5.0/3 = %f,\t 5/3.0 = %f\n",5/3,5.0/3,5/3.0);
    printf("(double)5/3 = %f,\t (double)(5/3)=%f\n",(double)5/3,(double)(5/3));
    printf("(double)x/y = %f, \t z = x/y = %f\n",(double)x/y, z = x/y);
    printf("1.0*x/y = %f, \t 1.0*(x/y) = %f\n",1.0*x/y,1.0*(x/y));
    
    
    printf("What you entered are %d, %d\n",a,b);
    scanf("%d,%d",&a,&b);   
    printf("sqrt(1.0*a*b)=%f\n",sqrt(1.0*a*b));

    return 0;
}

3 Examples

3.1 The greatest common divisor of x and y

#include <stdio.h>
#define MIN(a,b) a<b?a:b
int main(){
    int i,x,y;

    printf("Please input two integers x and y \n");
    scanf("%d %d",&x,&y);
    for(i=x<y?x:y; i>0; i--){
        if(x%i==0 && y%i==0){
            printf("the greatest common divisor of %d and %d is %d\n",x,y,i);
            break;
        }
    }
    
    for(i=MIN(x,y); i>0; i--){
        if(x%i==0 && y%i==0){
            printf("the greatest common divisor of %d and %d is %d\n",x,y,i);
            break;
        }
    }   
    return 0;
}

3.2 The lowest common multiple of x and y

  • one sugar function
#include <stdio.h>
#define MAX(a,b) a>b?a:b
int main(){
    int i,x,y,max;

    printf("Please input two integers x and y \n");
    scanf("%d %d",&x,&y);
    max = x*y+1;
    for(i=x>y?x:y; i<max; i++){
        if(i%x==0 && i%y==0){
            printf("The lowest common multiple of %d and %d is %d\n",x,y,i);
            break;
        }
    }
    
    for(i=MAX(x,y); i<max; i++){
        if(i%x==0 && i%y==0){
            printf("The lowest common multiple of %d and %d is %d\n",x,y,i);
            break;
        }
    }   
    return 0;
}
  • another sugar function. The following codes achieve a cheaper loop
  • please check the number of iterations
#include <stdio.h>
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a<b?a:b
int main(){
    // the lowest common multiple of x and y
    int i=2,x,y,z1,z0,z,max;
    printf("Please input two integers x and y \n");
    scanf("%d %d",&x,&y);

    z1 = MAX(x,y);
    z0 = MIN(x,y);
    max = x*y+1;
    z = z1;
    while(z<max)
    {
        if(!(z%z0)) break;
        z =z1*i++;
    }
    printf("The lowest common multiple of %d and %d is %d\n",x,y,z);
    return 0;
}

4 Problem in the previous week

  • Approximate \(\sin(x)\). It can be written as the series \(\sin(x) = x-x^3/3!+x^5/5!-x^7/7!+\cdots\). Thus, we can approximate \(\sin(x)\) by partial sum of this sequence with a given precision, say \(1e-6\).
#include <stdio.h>
#include <math.h>
int main(){
    // sin(x) = x/1-x^3/3!+x^5/5!-x^7/7!+...
    int count = 0, sign = 1;
    double x;
    double denominator,numerator,sum;
    printf("please input x = ");
    scanf("%lf",&x);
    numerator = x;
    denominator = 1.0;
    sum = numerator;
    while(numerator/denominator>=1e-6){
        count += 2;
        sign *= -1;
        numerator *= x*x;
        denominator *= count*(count+1);
        sum += sign*numerator/denominator;
    }
    printf("sin(x) ~= %f\n",sum);
    printf("sin(x) = %f\n",sin(x)); //require math.h
    return 0;
}

5 Problem

  • Find two nonnegative integers a and b such that the four-digit positive integer a2b3 can be divided by 23, where a2b3 = a\*1000+2\*100+b\*10+3.