Skip to content

luc015.c

Problem Statement

Given three points (x1, y1), (x2, y2), and (x3, y3), write a program to check if the three poins fall on one straight line.

Metadata

Property Detail
Author Amit Dutta (amitdutta4255@gmail.com)
License MIT
Difficulty Beginner (index: 1 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Sorting (possible)
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <math.h>
#define EPSILON 0.0001
// Define a small tolerance value (EPSILON) for safe floating-point comparison
// This is critical because of minor rounding errors in computer arithmetic.
int main()
{
    double x1, x2, x3, y1, y2, y3, area;
    printf("Enter the point A(x1, y1) : ");
    scanf("%lf %lf", &x1, &y1);
    printf("Enter the point B(x2, y2) : ");
    scanf("%lf %lf", &x2, &y2);
    printf("Enter the point C(x3, y3) : ");
    scanf("%lf %lf", &x3, &y3);
    area = 0.5 * ((x1 * (y2 - y3)) + (x2 * (y3 - y1)) + (x3 * (y1 - y2)));
    if (fabs(area) < EPSILON) // abs() for integer, fabs() for float, double
        printf("\nA(%g, %g), B(%g, %g) and C(%g, %g) points fall on one straight line.", x1, y1, x2, y2, x3, y3);
    else
        printf("\nA(%g, %g), B(%g, %g) and C(%g, %g) points doesn't fall on one straight line.", x1, y1, x2, y2, x3, y3);
    return 0;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc015.c):

    #include <stdio.h>
    #include <math.h>
    #define EPSILON 0.0001
    // Define a small tolerance value (EPSILON) for safe floating-point comparison
    // This is critical because of minor rounding errors in computer arithmetic.
    int main()
    {
        double x1, x2, x3, y1, y2, y3, area;
        printf("Enter the point A(x1, y1) : ");
        scanf("%lf %lf", &x1, &y1);
        printf("Enter the point B(x2, y2) : ");
        scanf("%lf %lf", &x2, &y2);
        printf("Enter the point C(x3, y3) : ");
        scanf("%lf %lf", &x3, &y3);
        area = 0.5 * ((x1 * (y2 - y3)) + (x2 * (y3 - y1)) + (x3 * (y1 - y2)));
        if (fabs(area) < EPSILON) // abs() for integer, fabs() for float, double
            printf("\nA(%g, %g), B(%g, %g) and C(%g, %g) points fall on one straight line.", x1, y1, x2, y2, x3, y3);
        else
            printf("\nA(%g, %g), B(%g, %g) and C(%g, %g) points doesn't fall on one straight line.", x1, y1, x2, y2, x3, y3);
        return 0;
    }