Doing Calculations in C

3-minute read
Table of Contents

Now that we know how to ask the computer for space to store values, we can do some math! Consider the following code:

#include <stdio.h>

int main(void){
    // step 1
    float mass = 105.7;
    // step 2
    float height = 2.1;
    // step 3
    float bmi = mass/(height*height);
    // step 4
    printf("The body mass index of someone with a mass of %.2f kilograms and height of %.2f meters is %.2f\n",mass,height,bmi);
    // step 5
    return 0;
}
bmi.c
Copy

Describe what is being done in each step. C is a language hence it is important that we learn to read it.

Recall the formula for BMI is $$\text{BMI}=\frac{\text{mass}}{\text{height}^2}$$

Right goes into left

In step 3, notice that we do not simply store a value but rather the result of a calculation (dividing mass by the square of height). Whatever is on the right of the equal sign (called the assignment operator) is computed and stored in what is on the left of the equation.

The description/reading of the code might have sounded like:

#include <stdio.h>

int main(void){
    // declare a float variable called mass and store 105.7 in it
    float mass = 105.7;
    // declare a float variable called height and store 2.1 in it
    float height = 2.1;
    // declare a float variable called bmi and store the result of dividing mass by the square of height in it
    float bmi = mass/(height*height);
    // output the result in a nice formatted string
    printf("The body mass index of someone with a mass of %.2f kilograms and height of %.2f meters is %.2f\n",mass,height,bmi);
    // return 0 on program success
    return 0;
}

bmi-solution.c
Copy

This mode of thinking enables us to easily convert algorithms used in math class to equivalent C code!

Mission details

Convert the method for finding the gradient of a straight given two points $(x_1,y_1)$ and $(x_2,y_2)$ on the line, into C code.

Recall the formula for the gradient of a straight line: $$m=\frac{y_2-y_1}{x_2-x_1}$$

Solution

#include <stdio.h>

int main(void){
    float x1 = 0.4;
    float x2 = 2.1;
    float y1 = 7.3;
    float y2 = 11.2;
    float m = (y2-y1)/(x2-x1);
    printf("The gradient of the line passing through (%.1f,%.1f) and (%.1f,%.1f) is %.2f\n",x1,y1,x2,y2,m);
    return 0;
}
finding-gradient.c
Copy

What do you think?

1) The value on the left of the assignment operator (=) is always stored in the value on the right.

  1. False
  2. True
  3. The assignment operator (=) stores the value on the right into the variable on the left

The modulo operator

We can use the % operator in order to find the remainder of a division:

#include <stdio.h>

int main(void){
    // shorthand way of writing:
    // int a = 3;
    // int b = 2;
    int a = 3, b = 2;

    // displaying the remainder
    printf("When %d is divided by %d, the remainder is %d\n",a,b,a%b);
    return 0;
}
modulo.c
Copy

This will be particularly useful when checking to see if a number is even/odd as well as determining if one number is the factor of another.


What do you think?

2) What does 14%7 evaluate to?

  1. 7
  2. 0
  3. 2
  4. 14
  5. 14 divided by 7 is 2 with a remainder of 0

Support us via BuyMeACoffee