Excercise 03: Calculations

For this exercise you will write several programs where you will ask the user for input and perform calculations using the provided values.

Rectangle

In rectangle.c, write a program that asks the user for the length and width of a rectangle, and then prints the area and perimeter of that rectangle.

Use the scanf() function to read input from the user. Remember that when using scanf() you do not want to include a newline (\n) in the format string. Remember to look at example code for help.

Use variables of type unsigned long to store the length and the width. The tests will use values that would overflow an int. Use %lu as the format specifier for unsigned long in printf() and scanf().

Below is what an example run of your program should look like. When git-keeper runs tests on your code, it will be looking for output that matches the last two lines exactly.

Enter the length: 5
Enter the width: 11
The area of the rectangle is 55
The perimeter of the rectangle is 32

Triangle

In triangle.c, write a program that asks the user for the length and width of a triangle and prints out the area. For this program, store the inputs and the area using the double type. The format specifier for double is %lf.

Below is what an example run of your program should look like:

Enter the length: 4.5
Enter the width: 7
The area of the triangle is 15.750000

Distance

In distance.c, write a program that calculates the distance between two points in two dimensional space. You will read in each coordinate individually as a double. You will need to use the Pythagorean theorem to calculate the distance, which means you will need to take a square root. The math.h library has the function sqrt() that you can use. You will need to add the line #include <math.h> to your program in order to use this function. Additionally, the extra argument -lm is needed on the command line when compiling a program that uses math.h, like so:

$ gcc distance.c -lm -o prog-dist

When printing the output, round values to 2 digits after the decimal point by using %.2lf for the format specifiers.

Below is what an example run of your program should look like:

Enter x1: 1
Enter y1: 2
Enter x2: 5
Enter y2: 6
The distance between (1.00,2.00) and (5.00,6.00) is 5.66

Running the tests locally

You can run the same testing that GitHub does locally using:

Note that these will create a tests-out folder after running a test which will show the output your program made.

Submission

When you have convinced yourself that your programs work (compile and run them yourself!), push your programs to GitHub. If any of the tests do not pass, you can fix your program(s) and try again.

Your grade for this exercise will be out out of 10 points: