Compute the Slope of a Line in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the slope of a line using C programming. The lab covers two main steps: reading two points (x1, y1) and (x2, y2) from user input, and then calculating the slope using the formula (y2-y1)/(x2-x1). The program will also handle the special case of a vertical line. By the end of this lab, you will have a better understanding of how to work with Calculus and Analytical Geometry concepts in C.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435165{{"`Compute the Slope of a Line in C`"}} c/variables -.-> lab-435165{{"`Compute the Slope of a Line in C`"}} c/if_else -.-> lab-435165{{"`Compute the Slope of a Line in C`"}} c/user_input -.-> lab-435165{{"`Compute the Slope of a Line in C`"}} c/math_functions -.-> lab-435165{{"`Compute the Slope of a Line in C`"}} end

Read Two Points (x1,y1) and (x2,y2)

In this step, you will learn how to read two points' coordinates from user input in a C program to calculate the slope of a line. We'll create a simple program that prompts the user to enter the x and y coordinates for two points.

First, let's create a new C file in the project directory:

cd ~/project
nano slope_calculator.c

Now, enter the following code to read two points:

#include <stdio.h>

int main() {
    float x1, y1, x2, y2;

    // Prompt user to enter first point coordinates
    printf("Enter the x-coordinate of the first point (x1): ");
    scanf("%f", &x1);
    printf("Enter the y-coordinate of the first point (y1): ");
    scanf("%f", &y1);

    // Prompt user to enter second point coordinates
    printf("Enter the x-coordinate of the second point (x2): ");
    scanf("%f", &x2);
    printf("Enter the y-coordinate of the second point (y2): ");
    scanf("%f", &y2);

    // Print the entered coordinates
    printf("First point: (%.2f, %.2f)\n", x1, y1);
    printf("Second point: (%.2f, %.2f)\n", x2, y2);

    return 0;
}

Compile and run the program:

gcc slope_calculator.c -o slope_calculator
./slope_calculator

Example output:

Enter the x-coordinate of the first point (x1): 1
Enter the y-coordinate of the first point (y1): 2
Enter the x-coordinate of the second point (x2): 4
Enter the y-coordinate of the second point (y2): 6
First point: (1.00, 2.00)
Second point: (4.00, 6.00)

Compute Slope = (y2-y1)/(x2-x1)

In this step, you will modify the previous program to calculate the slope of a line using the point-slope formula. We'll add slope calculation and handle the special case of a vertical line.

Open the previous file and update the code:

cd ~/project
nano slope_calculator.c

Replace the contents with the following code:

#include <stdio.h>

int main() {
    float x1, y1, x2, y2, slope;

    // Prompt user to enter first point coordinates
    printf("Enter the x-coordinate of the first point (x1): ");
    scanf("%f", &x1);
    printf("Enter the y-coordinate of the first point (y1): ");
    scanf("%f", &y1);

    // Prompt user to enter second point coordinates
    printf("Enter the x-coordinate of the second point (x2): ");
    scanf("%f", &x2);
    printf("Enter the y-coordinate of the second point (y2): ");
    scanf("%f", &y2);

    // Check for vertical line (undefined slope)
    if (x2 == x1) {
        printf("Slope is undefined (vertical line)\n");
        return 0;
    }

    // Calculate slope
    slope = (y2 - y1) / (x2 - x1);

    // Print the results
    printf("First point: (%.2f, %.2f)\n", x1, y1);
    printf("Second point: (%.2f, %.2f)\n", x2, y2);
    printf("Slope: %.2f\n", slope);

    return 0;
}

Compile and run the program:

gcc slope_calculator.c -o slope_calculator
./slope_calculator

Example output:

Enter the x-coordinate of the first point (x1): 1
Enter the y-coordinate of the first point (y1): 2
Enter the x-coordinate of the second point (x2): 4
Enter the y-coordinate of the second point (y2): 6
First point: (1.00, 2.00)
Second point: (4.00, 6.00)
Slope: 1.33

Print the Slope

In this final step, you will enhance the slope calculation program by adding more detailed output and formatting the slope presentation. We'll improve the user experience by providing clear, informative slope information.

Open the previous file and update the code:

cd ~/project
nano slope_calculator.c

Replace the contents with the following code:

#include <stdio.h>
#include <math.h>

int main() {
    float x1, y1, x2, y2, slope;

    // Prompt user to enter first point coordinates
    printf("Slope Calculator\n");
    printf("================\n");
    printf("Enter the x-coordinate of the first point (x1): ");
    scanf("%f", &x1);
    printf("Enter the y-coordinate of the first point (y1): ");
    scanf("%f", &y1);

    // Prompt user to enter second point coordinates
    printf("Enter the x-coordinate of the second point (x2): ");
    scanf("%f", &x2);
    printf("Enter the y-coordinate of the second point (y2): ");
    scanf("%f", &y2);

    // Check for vertical line (undefined slope)
    if (x2 == x1) {
        printf("\nResult:\n");
        printf("First point:  (%.2f, %.2f)\n", x1, y1);
        printf("Second point: (%.2f, %.2f)\n", x2, y2);
        printf("Slope: Undefined (Vertical Line)\n");
        return 0;
    }

    // Calculate slope
    slope = (y2 - y1) / (x2 - x1);

    // Print detailed results
    printf("\nResult:\n");
    printf("First point:  (%.2f, %.2f)\n", x1, y1);
    printf("Second point: (%.2f, %.2f)\n", x2, y2);
    printf("Slope Calculation: (%.2f - %.2f) / (%.2f - %.2f) = %.2f\n",
           y2, y1, x2, x1, slope);

    // Additional slope interpretation
    if (slope > 0) {
        printf("Slope Interpretation: Positive slope (line rises from left to right)\n");
    } else if (slope < 0) {
        printf("Slope Interpretation: Negative slope (line falls from left to right)\n");
    } else {
        printf("Slope Interpretation: Horizontal line (zero slope)\n");
    }

    return 0;
}

Compile and run the program:

gcc slope_calculator.c -o slope_calculator
./slope_calculator

Example output:

Slope Calculator
================
Enter the x-coordinate of the first point (x1): 1
Enter the y-coordinate of the first point (y1): 2
Enter the x-coordinate of the second point (x2): 4
Enter the y-coordinate of the second point (y2): 6

Result:
First point:  (1.00, 2.00)
Second point: (4.00, 6.00)
Slope Calculation: (6.00 - 2.00) / (4.00 - 1.00) = 1.33
Slope Interpretation: Positive slope (line rises from left to right)

Summary

In this lab, you will learn how to read two points' coordinates from user input and then compute the slope of a line using the point-slope formula in a C program. The program will prompt the user to enter the x and y coordinates for two points, and then calculate and display the slope of the line. Additionally, the program will handle the special case of a vertical line, where the slope is undefined.

The lab covers the following key steps: 1) Read two points (x1, y1) and (x2, y2) from user input, and 2) Compute the slope using the formula (y2 - y1) / (x2 - x1). The program will then print the calculated slope.

Other C Tutorials you may like