Compute Inverse Cosine (arccos) in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the inverse cosine (arccos) function in C. The lab covers two main steps: reading a valid input value between -1 and 1, and then using the acos() function from the C math library to calculate the arccos of the input. The step-by-step instructions provide a comprehensive guide on input validation and performing the arccos calculation, ensuring you have a solid understanding of this trigonometric operation in C programming.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") 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-435347{{"`Compute Inverse Cosine (arccos) in C`"}} c/if_else -.-> lab-435347{{"`Compute Inverse Cosine (arccos) in C`"}} c/user_input -.-> lab-435347{{"`Compute Inverse Cosine (arccos) in C`"}} c/math_functions -.-> lab-435347{{"`Compute Inverse Cosine (arccos) in C`"}} end

Read a Value Between -1 and 1

In this step, you will learn how to read a valid input value for calculating the inverse cosine (arccos) function in C. The arccos function requires input values between -1 and 1.

First, create a new C file to demonstrate input validation:

cd ~/project
nano arccos_input.c

Now, add the following code to the file:

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

int main() {
    double input;

    printf("Enter a value between -1 and 1 for arccos calculation: ");
    scanf("%lf", &input);

    // Input validation
    if (input < -1 || input > 1) {
        printf("Error: Input must be between -1 and 1\n");
        return 1;
    }

    printf("Valid input: %f\n", input);
    return 0;
}

Compile and run the program:

gcc arccos_input.c -o arccos_input -lm

Example outputs:

Valid input:

Enter a value between -1 and 1 for arccos calculation: 0.5
Valid input: 0.500000

Invalid input:

Enter a value between -1 and 1 for arccos calculation: 2
Error: Input must be between -1 and 1

This code demonstrates:

  • Using scanf() to read a double-precision floating-point number
  • Validating input to ensure it's within the valid range for arccos
  • Providing user-friendly error messages for invalid inputs

Use acos() Function

In this step, you will learn how to use the acos() function from the C math library to calculate the inverse cosine (arccos) of a given input value.

Continue working in the same project directory and modify the previous program:

cd ~/project
nano arccos_calculation.c

Add the following code to implement arccos calculation:

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

int main() {
    double input;

    printf("Enter a value between -1 and 1 for arccos calculation: ");
    scanf("%lf", &input);

    // Input validation
    if (input < -1 || input > 1) {
        printf("Error: Input must be between -1 and 1\n");
        return 1;
    }

    // Calculate arccos
    double angle_radians = acos(input);

    printf("Input value: %f\n", input);
    printf("Arccos (in radians): %f\n", angle_radians);

    return 0;
}

Compile the program:

gcc arccos_calculation.c -o arccos_calculation -lm

Example outputs:

Enter a value between -1 and 1 for arccos calculation: 0.5
Input value: 0.500000
Arccos (in radians): 1.047198
Enter a value between -1 and 1 for arccos calculation: -0.5
Input value: -0.500000
Arccos (in radians): 2.094395

Key points about acos():

  • Part of the <math.h> library
  • Takes a value between -1 and 1
  • Returns the angle in radians
  • Requires -lm flag during compilation to link math library

Print the Angle in Radians

In this step, you will learn how to convert and print the arccos angle in different formats, including radians, degrees, and a more readable representation.

Continue working in the same project directory:

cd ~/project
nano arccos_angle_print.c

Add the following code to demonstrate angle printing:

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

#define PI 3.14159265358979323846

int main() {
    double input;

    printf("Enter a value between -1 and 1 for arccos calculation: ");
    scanf("%lf", &input);

    // Input validation
    if (input < -1 || input > 1) {
        printf("Error: Input must be between -1 and 1\n");
        return 1;
    }

    // Calculate arccos
    double angle_radians = acos(input);

    // Convert radians to degrees
    double angle_degrees = angle_radians * (180.0 / PI);

    // Print angle in different formats
    printf("Input value: %f\n", input);
    printf("Angle in Radians: %f\n", angle_radians);
    printf("Angle in Degrees: %f\n", angle_degrees);

    // Print a descriptive representation
    printf("Angle Description: %.2f radians (%.2f degrees)\n",
           angle_radians, angle_degrees);

    return 0;
}

Compile the program:

gcc arccos_angle_print.c -o arccos_angle_print -lm

Example outputs:

Enter a value between -1 and 1 for arccos calculation: 0.5
Input value: 0.500000
Angle in Radians: 1.047198
Angle in Degrees: 60.000000
Angle Description: 1.05 radians (60.00 degrees)
Enter a value between -1 and 1 for arccos calculation: -0.5
Input value: -0.500000
Angle in Radians: 2.094395
Angle in Degrees: 120.000000
Angle Description: 2.09 radians (120.00 degrees)

Key points:

  • Used PI constant for degree conversion
  • Converted radians to degrees using angle_radians * (180.0 / PI)
  • Demonstrated different formatting options for angle representation
  • Used %.2f for two decimal place precision

Summary

In this lab, you learned how to read a valid input value between -1 and 1 for calculating the inverse cosine (arccos) function in C. You also learned how to use the acos() function from the C math library to perform the arccos calculation and print the angle in radians.

The key takeaways from this lab are input validation to ensure the input is within the valid range, and the use of the acos() function to compute the arccos of the given value.

Other C Tutorials you may like