Chapter 9 - Let Us C

Given three variables x, y, z, write a function to circularly shift their values to right. In other words, if x = 5, y = 8, z = 10, after circular shift y = 5, z = 8, x =10. Call the function with variables a, b, c to circularly shift values.


#include <stdio.h>
void shift(int *a, int *b, int *c){
    *a = *a + *b - (*b = *a);
    *a = *a + *c - (*c = *a);
}
int main(){
    int x,y,z;
    printf("Enter the value of x,y and z : ");
    scanf("%d %d %d",&x,&y,&z);

    printf("Before Shift\nx = %d\ny = %d\nz = %d\n",x,y,z);
    shift(&x,&y,&z);
    printf("After Shift\nx = %d\ny = %d\nz = %d\n",x,y,z);
    return 0;
}
    
    
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: Define a function `shift` that takes three integer pointers `a`, `b`, and `c` as arguments. This function performs a cyclic shift of the values pointed to by these pointers.
Step 3: Inside the `shift` function, the first line `*a = *a + *b - (*b = *a);` performs the first part of the cyclic shift.
  • `*b = *a` assigns the value pointed to by `a` to the variable pointed to by `b`. This effectively stores the original value of `a` in `b`.
  • `*a + *b` adds the original value of `a` (now in `b`) to the current value of `a`.
  • Then, the original value of `a` (which is now in `b`) is subtracted. This results in the original value of `b` being stored in `a`.
  • In essence, `a` now holds the original value of `b`.
Step 4: The second line `*a = *a + *c - (*c = *a);` performs the second part of the cyclic shift.
  • `*c = *a` assigns the current value pointed to by `a` (which is the original value of `b`) to the variable pointed to by `c`.
  • `*a + *c` adds the current value of `a` (original `b`) to the current value of `a`.
  • Then, the original value of `a` (which is now in `c`) is subtracted. This results in the original value of `c` being stored in `a`.
  • In essence, `a` now holds the original value of `c`.
Step 5: The `main` function is the entry point of the program.
Step 6: Declare three integer variables `x`, `y`, and `z` to store the values entered by the user.
Step 7: Prompt the user to enter the values of `x`, `y`, and `z` and store them using `scanf`.
Step 8: Print the values of `x`, `y`, and `z` before the shift operation.
Step 9: Call the `shift` function, passing the addresses of `x`, `y`, and `z` using the `&` operator. This modifies the values of `x`, `y`, and `z` directly.
Step 10: Print the values of `x`, `y`, and `z` after the shift operation. The values are now cyclically shifted (x gets z's original value, y gets x's original value, z gets y's original value).
Step 11: The program returns 0, indicating successful execution.

Define a function that receives weight of a commodity in Kilograms and returns the equivalent weight in Grams, Tons and Pounds. Call this function from main( ) and print the results in main( ).


#include <stdio.h>
void weightConversion(float kg,float *g, float *ton, float *lbs){
    *g = kg * 1000;
    *ton = kg * 0.001;
    *lbs = kg * 2.20462;
}
int main(){
    float kg,g,ton,lbs;
    printf("Enter the weight in kg : ");
    scanf("%f",&kg);

    weightConversion(kg,&g,&ton,&lbs);
    printf("Weight in grams: %.2f\n", g);
    printf("Weight in tons: %.4f\n", ton);
    printf("Weight in pounds: %.2f\n", lbs);
    return 0;
}

        
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: Define a function `weightConversion` that takes a float `kg` (kilograms) and three float pointers `g` (grams), `ton` (metric tons), and `lbs` (pounds) as arguments. This function performs weight conversions from kilograms to the other units.
Step 3: Inside the `weightConversion` function, the first line `*g = kg * 1000;` calculates the weight in grams by multiplying the kilogram value by 1000 and stores the result in the memory location pointed to by `g`.
Step 4: The second line `*ton = kg * 0.001;` calculates the weight in metric tons by multiplying the kilogram value by 0.001 and stores the result in the memory location pointed to by `ton`.
Step 5: The third line `*lbs = kg * 2.20462;` calculates the weight in pounds by multiplying the kilogram value by 2.20462 and stores the result in the memory location pointed to by `lbs`.
Step 6: The `main` function is the entry point of the program.
Step 7: Declare four float variables `kg`, `g`, `ton`, and `lbs` to store the weight in kilograms and the converted weights in grams, tons, and pounds, respectively.
Step 8: Prompt the user to enter the weight in kilograms and store it in the `kg` variable using `scanf`.
Step 9: Call the `weightConversion` function, passing the `kg` value and the addresses of `g`, `ton`, and `lbs` using the `&` operator. This modifies the values of `g`, `ton`, and `lbs` directly.
Step 10: Print the calculated weight in grams, formatted to two decimal places using `%.2f`.
Step 11: Print the calculated weight in tons, formatted to four decimal places using `%.4f`.
Step 12: Print the calculated weight in pounds, formatted to two decimal places using `%.2f`.
Step 13: The program returns 0, indicating successful execution.

Define a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lines inside the triangle ABC, otherwise returns a value 0.


#include <stdio.h>
int isPointInsideTriangle(float ax, float ay, float bx, float by, float cx, float cy, float px, float py) {

    float det = (bx-ax)*(cy-ay) - (cx-ax)*(by-ay);
    float alpha = ((cy-ay)*(px-ax)-(cx-ax)*(py-ay)) / det;
    float beta  = ((bx-ax)*(py-ay)-(by-ay)*(px-ax)) / det;

    return (alpha >= 0 && beta >= 0 && (alpha + beta) <= 1);
}

int main() {
    float ax, ay, bx, by, cx, cy, px, py;

    printf("Enter coordinates of point A (x1 y1): ");
    scanf("%f %f", &ax, &ay);
    printf("Enter coordinates of point B (x2 y2): ");
    scanf("%f %f", &bx, &by);
    printf("Enter coordinates of point C (x3 y3): ");
    scanf("%f %f", &cx, &cy);

    printf("Enter coordinates of point P (x y): ");
    scanf("%f %f", &px, &py);

    if (isPointInsideTriangle(ax, ay, bx, by, cx, cy, px, py)) {
        printf("Point (%.2f, %.2f) lies inside or on the triangle.\n", px, py);
    } else {
        printf("Point (%.2f, %.2f) is outside the triangle.\n", px, py);
    }

    return 0;
}

        
Step 1:

Include the `stdio.h` header file.

Step 2:

Define the `isPointInsideTriangle` function.

  • This function takes the coordinates of the triangle vertices (A, B, C) and a point P as input.

  • It returns an integer: 1 if point P is inside or on the triangle, and 0 if it's outside.

Step 3:

Calculate the determinant `det`.

  • `det = (bx-ax)*(cy-ay) - (cx-ax)*(by-ay);` This determinant is crucial for determining the orientation of the triangle and for calculating α and β.

  • If `det` is zero, the points A, B, and C are collinear, forming a degenerate triangle or a line, and the function might produce unexpected results.

Step 4:

Calculate α (alpha) and β (beta).

  • These are the coefficients in the vector representation of vector AP as a linear combination of vectors AB and AC: AP = αAB + βAC.

  • The vector AP represents the displacement from point A to point P.

  • The vector AB represents the displacement from point A to point B, and AC represents the displacement from A to C.

  • `α = ((cy-ay)*(px-ax)-(cx-ax)*(py-ay)) / det;`

  • `β = ((bx-ax)*(py-ay)-(by-ay)*(px-ax)) / det;`

  • These formulas are derived from Cramer's rule applied to the vector equation AP = αAB + βAC, solving for α and β.

Step 5:

Determine if point P is inside the triangle.

  • The point P lies inside the triangle if and only if:

  • α ≥ 0 (α is non-negative).

  • β ≥ 0 (β is non-negative).

  • α + β ≤ 1 (the sum of α and β is less than or equal to 1).

  • These conditions ensure that P is within the triangle ABC.

  • `return (α >= 0 && β >= 0 && (α + β) <= 1);` This returns 1 (true) if P is inside, and 0 (false) otherwise.

Step 6:

The `main` function is the program's entry point.

  • Execution begins here.

Step 7:

Declare float variables to store the coordinates of A, B, C, and P.

  • `ax`, `ay`, `bx`, `by`, `cx`, `cy`, `px`, and `py` store the x and y coordinates of the respective points.

Step 8:

Prompt the user to enter the coordinates.

  • `printf` displays prompts, and `scanf` reads the input values.

Step 9:

Call `isPointInsideTriangle` to check if P is inside the triangle.

  • Pass the coordinates of A, B, C, and P as arguments.

Step 10:

Print the result.

  • If the function returns 1, print a message indicating that P is inside or on the triangle.

  • If the function returns 0, print a message indicating that P is outside the triangle.

  • The `%.2f` format specifier in `printf` ensures the coordinates are displayed with two decimal places.

Step 11:

Return 0 from `main`, indicating successful program execution.

Comments

Popular Posts