C Programming Tutorial 44 – Coding Challenge

Did you read the last blog? Go read it on Quiz Part 3! Are you new here? Start at the beginning, Intro to C!

You know a lot of the concepts, but now you need to practice writing some code. I am going to give you a small coding assignment. You can give it a try and then compare your answer to mine.

The Assignment:

You have a right triangle, and you are given the length of the smaller sides. Write a program that asks the user for two lengths of a right triangle, and outputs the length of the longest side (the hypotenuse).

To do this, you use the Pythagorean theorem. a^2 + b^2 = c^2

You will also need to know that for this assignment you will need to use the sqrt() function which requires you to include math.h. Another thing, when you are using some math functions, you have to do something special when you compile.

Normally, you compile like this: gcc py.c. Additionally, you can add -Wall, which will show warnings when you compile. Well, you have add -lm. This has to do with what is known as linking. We are linking the C math library to our program when we compile. If you can’t figure that part out, that’s okay. I’ll show you, but still try to write as much code as possible.

I’m going to let you research the details on how to use sqrt() and whatever else so I don’t ruin all of the fun.

The Solution:

To Compile:

Ready to learn more? Read the next blog on the intro to logic!

Leave a Reply

Your email address will not be published. Required fields are marked *