c Prg Examples

Embed Size (px)

Citation preview

  • 7/30/2019 c Prg Examples

    1/14

    /*

    * math_ops.c

    * This program makes use of the various arithmetic operators.

    */

    #include

    int main ()

    {

    int a, b;

    float x, y;

    /* Read two integer numbers. */

    printf ("Please enter two integer numbers: ");

    scanf ("%d %d", &a, &b);

    /* Perform some calculations. */

    printf ("%d + %d = %d\n", a, b, a + b);printf ("%d - %d = %d\n", a, b, a - b);

    printf ("%d * %d = %d\n", a, b, a * b);

    printf ("%d / %d = %d\n", a, b, a / b);

    printf ("%d mod %d = %d\n", a, b, a % b);

    /* Read two real numbers. */

    printf ("Please enter two real numbers: ");

    scanf ("%f %f", &x, &y);

    /* Perform some calculations. */

    printf ("%g + %g = %g\n", x, y, x + y);

    printf ("%g - %g = %g\n", x, y, x - y);

    printf ("%g * %g = %g\n", x, y, x * y);

    printf ("%g / %g = %g\n", x, y, x / y);

    return (0);

    }

  • 7/30/2019 c Prg Examples

    2/14

    /** sign_pair.c* This program read an integer number and prints its sign andits parity.*/

    #includeint main (){ int n; int sign; int is_even;/* Read the number. */printf ("Please enter a number: ");scanf ("%d", &n);

    /* Find the sign. */

    if (n < 0){sign = -1;

    /* Find the parity (check whether 2 divides n with no

    remainder). */ if (n % 2 == 0)

    is_even = 1; else

    is_even = 0;}

    elseif (n > 0){sign = 1;

    /* Find the parity - alternative syntax. */is_even = (n % 2) ? 0 : 1;

    } else/* n equals 0 */{

    sign = 0;is_even = 1;

    }/* Print the result. */printf ("sign(%d) = %d, ", n, sign);

    if (is_even)printf ("%d is even.\n", n);

  • 7/30/2019 c Prg Examples

    3/14

    elseprintf ("%d is odd.\n", n);

    return (0);

    }/** power.c* This program reads two integer numbers and raises the firstby the power* of the second.*/#includeint main (){ int base; int power; int res; int is_neg_power = 0; int i;/* Read the two numbers. */printf ("Please enter two integers: ");scanf ("%d %d", &base, &power);

    /* Check the case of a negative power. */

    if (power < 0){is_neg_power = 1;power = -power;

    }/* Compute the result. */res = 1;

    for (i = 0; i < power; i++)res *= base;

    /* Print the result. */printf ("The result is: ");

    if (! is_neg_power)printf ("%d\n", res);

    elseprintf ("%g\n", 1 / (double)res);

    return (0);}

  • 7/30/2019 c Prg Examples

    4/14

    /** prime.c* This program read and integer number and checks whether it isprime.

    */

    #include

    int main (){ int n; int is_prime = 1; int i;

    /* Read the number. */printf ("Please enter a positive integer: ");scanf ("%d", &n);

    /* Check the input. */ if (n

  • 7/30/2019 c Prg Examples

    5/14

    /** dist.c* This program read two points and computes the distancebetween them.*/

    #include#include

    int main (){ double x1, y1; /* The coordinates of the firstpoint. */ double x2, y2; /* The coordinates of the secondpoint. */ double dist;

    /* Read the two points. */printf ("Please enter the first point: ");scanf ("%lf %lf", &x1, &y1);

    printf ("Please enter the second point: ");scanf ("%lf %lf", &x2, &y2);

    /* Compute the distance between these points:* ________________________* / 2 2* dist = \/ (x2 - x1) + (y2 - y1)

    */dist = sqrt ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));

    printf ("dist((%g,%g),(%g,%g)) = %g\n", x1, y1, x2, y2, dist);

    return (0);}

  • 7/30/2019 c Prg Examples

    6/14

    /** lat2dec.c* This program reads an "alphabetic number" (in base-26),converts it* to a numerical value and prints it as a decimal number.

    */#includeint main (){ constint base = 26; /* Our alphabetic base has 26"digits":

    {A, B, ... , Z}. */ int c; /* The current "digit"(character). */ unsignedint n = 0; /* The current numerical value.*/printf ("Please enter an alphabetic number: ");

    /* Read characters until reaching the end of the line. */

    while ((c = getchar()) != '\n'){

    /* Make sure that the character represents an alphabetic"digit". */ if (! (c >= 'A' && c

  • 7/30/2019 c Prg Examples

    7/14

    /** quad_eq.c* This program reads the coefficients of a quadratic equation

    and solves* the equation.*/#include#include/* Prototypes: */int solve_quad (double a, double b, double c, double *x1, double *x2);int main (){ double a, b, c; /* The equation coefficients. */ double x1, x2; /* Its solutions. */ int n_sols; /* The number of real solutions. *//* Read the coeffcients. */printf ("Please enter the three coefficients of the quadratic

    equation: ");scanf ("%lf %lf %lf", &a, &b, &c);

    /* Solve the equation. */

    n_sols = solve_quad (a, b, c,&x1, &x2);

    /* Print the solutions. */

    switch (n_sols){

    case0:printf ("The equation has no real solutions.\n");

    break;case1:

    printf ("The equation has a single solution: (x = %g).\n",x1); break;case2:printf ("The equation has two solutions: (x1 = %g) (x2 =

    %g).\n", x1, x2); break;

  • 7/30/2019 c Prg Examples

    8/14

    default:printf ("You must be kidding!\n");

    return (1);}

    return (0);}/*------------------------------------------------------------------------* Function: solve_quad* Purpose : Solve the quadratic equation: (a*x^2 + bx + c = 0).* Input : a, b, c - The equation coefficients.* Output : x1, x2 - The solutions.* Returns : The number of real solutions to the equation (0, 1or 2).*/int solve_quad (double a, double b, double c, double *x1, double *x2){ double disc; double sqrt_disc;/* Check if this is really a linear equation. */

    if (a == 0){

    /* The equation is: (b*x + c = 0). */ if (b == 0) return (0);

    *x1 = -c / b; return (1);}

    /* Compute the discriminant and act according to its sign. */disc = b*b - 4*a*c;

    if (disc < 0){

    return (0);}

    elseif (disc == 0){*x1 = -b / (2*a);

    return (1);

  • 7/30/2019 c Prg Examples

    9/14

    }/* -b +/- sqrt(b^2 - 4ac)* Use the formula: x1,2 = ------------------------* 2a

    */sqrt_disc = sqrt (disc);*x1 = (-b + sqrt_disc) / (2*a);*x2 = (-b - sqrt_disc) / (2*a);

    return (2);}/** bubble.c* This program reads a sequence of intergers, terminated by (-1), and sorts* it using the bubble-sort algorithm.*/#include#define MAX_SIZE 100/* Prototypes: */void bubble_sort (int *arr, int n);

    int main (){ constint stop_code = -1; int array[MAX_SIZE]; int n_numbers = 0; int curr; int i;/* Read the input numbers. */

    while (n_numbers < MAX_SIZE){

    /* Read the next number. */printf ("Please enter the next number (or -1 to stop): ");scanf ("%d", &curr);

    if (curr == stop_code)

    break;

    /* Store it. */

  • 7/30/2019 c Prg Examples

    10/14

    array[n_numbers] = curr;n_numbers++;

    }/* Sort the numbers and print them. */

    bubble_sort (array, n_numbers);for (i = 0; i < n_numbers; i++)printf ("%d ", array[i]);

    printf ("\n");return (0);

    }/*------------------------------------------------------------------------* Function: bubble_sort* Purpose : Sort an array of integers in an ascending order,using the* bubble-sort algorithm.* Input : n - The number of intergers in the array.* In/Out : arr - The array.* Returns : Nothing.*/void bubble_sort (int *arr, int n){ int i, j;

    int temp;/* Let i run over all entries (except the last). */

    for (i = 0; i < n - 1; i++){

    /* Let j run over all entries from (i+1) until the last. */ for (j = i + 1; j < n; j++)

    { /* If arr[i] > arr[j], swap the two entries. */ if (arr[i] > arr[j])

    {

    temp = arr[i];arr[i] = arr[j];arr[j] = temp;

    }}

    }return;

  • 7/30/2019 c Prg Examples

    11/14

    }

    /** dates_1.c

    * This program reads a date and prints the dates of the daysbefore and after* this date.*/#includestruct Date{ int day; int month; int year;};/* Prototypes: */void print_date (struct Date date);struct Date next_date (struct Date date);struct Date prev_date (struct Date date);int main (){ int dd, mm, yy; struct Date date;

    struct Date prev; struct Date next;/* Read the date (no validity checks for now). */printf ("Please enter a date: ");scanf ("%d %d %d", &dd, &mm, &yy);

    /* Set the date. */date.day = dd;date.month = mm;date.year = yy;

    /* Compute the previous date and the next date. */prev = prev_date (date);next = next_date (date);

    /* Print out the results. */printf ("Yesterday was: ");print_date (prev);

  • 7/30/2019 c Prg Examples

    12/14

    printf ("\n");printf ("Today is: ");print_date (date);printf ("\n");

    printf ("Tomorrow will be: ");print_date (next);printf ("\n");

    return (0);

    }/*------------------------------------------------------------------------* Function: print_date* Purpose : Print a date in a nice format.* Input : date - The date.* Returns : Nothing.*/void print_date (struct Date date){ constchar* month_names[12] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    printf ("%d %s %d", date.day, month_names[date.month - 1],date.year); return;}/*------------------------------------------------------------------------* Function: next_date* Purpose : Compute the next date.* Input : date - The current date.

    * Returns : The date of the next day.*/struct Date next_date (struct Date date){ int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct Date res = date;

  • 7/30/2019 c Prg Examples

    13/14

    /* Increment the day and check whether the date is stilllegal. */res.day++;

    if (res.day > days_in_month[res.month - 1])

    { /* Move to the next month. */res.day = 1;res.month++;

    if (res.month > 12){

    /* Move to the next year. */res.month = 1;res.year++;

    }}

    return (res);

    }/*------------------------------------------------------------------------* Function: prev_date* Purpose : Compute the previous date.* Input : date - The current date.* Returns : The date of the previous day.

    */struct Date prev_date (struct Date date){ int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct Date res = date;/* Decrement the day and check whether the date is still

    legal. */res.day--;

    if (res.day == 0){

    /* Move to the previous month. */res.month--;

    if (res.month == 0){

    /* Move to the previous year. */

  • 7/30/2019 c Prg Examples

    14/14

    res.month = 12;res.year--;

    }

    /* Set the day to be the last of the month. */

    res.day = days_in_month[res.month -1

    ];}return (res);

    }