Evaluate Absolute Value Expressions in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to evaluate absolute value expressions in C programming. The lab covers the following steps: reading integer or float values, using the abs() or fabs() functions to compute the absolute value, and printing the result. You will gain hands-on experience with variable declaration, input/output operations, and working with built-in mathematical functions in C.

The lab provides a step-by-step guide to help you understand the process of evaluating absolute value expressions, which is a fundamental concept in algebraic expressions. By the end of the lab, you will be able to write C programs that can handle both integer and floating-point absolute value calculations.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435174{{"`Evaluate Absolute Value Expressions in C`"}} c/variables -.-> lab-435174{{"`Evaluate Absolute Value Expressions in C`"}} c/user_input -.-> lab-435174{{"`Evaluate Absolute Value Expressions in C`"}} c/math_functions -.-> lab-435174{{"`Evaluate Absolute Value Expressions in C`"}} end

Read an Integer or Float

In this step, you will learn how to read integer and float values in C programming. We'll demonstrate different methods to input numeric values and understand the basics of variable declaration and input.

Declaring Variables for Numeric Input

First, let's create a C program to read integer and float values. Open a new file called numeric_input.c in the ~/project directory:

nano ~/project/numeric_input.c

Now, enter the following code:

#include <stdio.h>

int main() {
    int integerValue;
    float floatValue;

    // Reading an integer
    printf("Enter an integer: ");
    scanf("%d", &integerValue);

    // Reading a float
    printf("Enter a float value: ");
    scanf("%f", &floatValue);

    // Displaying the input values
    printf("Integer value: %d\n", integerValue);
    printf("Float value: %f\n", floatValue);

    return 0;
}

Let's break down the code:

  • int integerValue; declares an integer variable
  • float floatValue; declares a float variable
  • scanf("%d", &integerValue); reads an integer input
  • scanf("%f", &floatValue); reads a float input
  • %d is used for integer format specifier
  • %f is used for float format specifier

Compile and Run the Program

Compile the program using gcc:

gcc ~/project/numeric_input.c -o ~/project/numeric_input

Run the program:

~/project/numeric_input

Example output:

Enter an integer: 42
Enter a float value: 3.14
Integer value: 42
Float value: 3.140000

Use abs() or fabs() to Compute Absolute Value

In this step, you will learn how to compute absolute values in C using built-in functions abs() for integers and fabs() for floating-point numbers.

Understanding Absolute Value Functions

First, create a new file called absolute_value.c in the ~/project directory:

nano ~/project/absolute_value.c

Enter the following code:

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

int main() {
    // Absolute value for integers
    int intNumber = -42;
    int intAbsolute = abs(intNumber);

    // Absolute value for floating-point numbers
    float floatNumber = -3.14;
    float floatAbsolute = fabs(floatNumber);

    // Displaying absolute values
    printf("Integer: %d, Absolute Value: %d\n", intNumber, intAbsolute);
    printf("Float: %f, Absolute Value: %f\n", floatNumber, floatAbsolute);

    return 0;
}

Let's break down the code:

  • abs() is used for integer absolute values (from <stdlib.h>)
  • fabs() is used for floating-point absolute values (from <math.h>)
  • Both functions return the non-negative magnitude of a number

Compile and Run the Program

Compile the program with the math library:

gcc ~/project/absolute_value.c -o ~/project/absolute_value -lm

Run the program:

~/project/absolute_value

Example output:

Integer: -42, Absolute Value: 42
Float: -3.140000, Absolute Value: 3.140000

Handling Different Number Types

Note the different header files and functions for integer and floating-point absolute values:

  • For integers: #include <stdlib.h> and abs()
  • For floating-point numbers: #include <math.h> and fabs()

Print the Result

In this step, you will learn how to format and print absolute value results with different formatting options in C.

Creating a Comprehensive Absolute Value Program

Create a new file called absolute_value_print.c in the ~/project directory:

nano ~/project/absolute_value_print.c

Enter the following code:

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

int main() {
    // Input variables
    int intNumber;
    float floatNumber;

    // Prompt for input
    printf("Enter an integer: ");
    scanf("%d", &intNumber);

    printf("Enter a float number: ");
    scanf("%f", &floatNumber);

    // Compute absolute values
    int intAbsolute = abs(intNumber);
    float floatAbsolute = fabs(floatNumber);

    // Print results with different formatting
    printf("Original Integer: %d\n", intNumber);
    printf("Absolute Integer: %d\n", intAbsolute);

    printf("\nOriginal Float: %.2f\n", floatNumber);
    printf("Absolute Float: %.2f\n", floatAbsolute);

    // Scientific notation
    printf("\nAbsolute Float (Scientific): %e\n", floatAbsolute);

    return 0;
}

Compile and Run the Program

Compile the program with the math library:

gcc ~/project/absolute_value_print.c -o ~/project/absolute_value_print -lm

Run the program:

~/project/absolute_value_print

Example output:

Enter an integer: -42
Enter a float number: -3.14
Original Integer: -42
Absolute Integer: 42

Original Float: -3.14
Absolute Float: 3.14

Absolute Float (Scientific): 3.140000e+00

Understanding Print Formatting

Key formatting options demonstrated:

  • %d for integers
  • %f for floating-point numbers
  • %.2f for two decimal places
  • %e for scientific notation

Summary

In this lab, you will learn how to read integer and float values in C programming, and how to compute the absolute value of these numbers using the built-in functions abs() and fabs(). First, you will declare variables for integer and float input, and use the scanf() function to read the values. Then, you will use the abs() function for integers and the fabs() function for floating-point numbers to calculate the absolute value, and print the results.

Other C Tutorials you may like