How to use square root in c programming?

Martyz

Baseband Member
Messages
47
Location
USA
I am making a program that computes for the hypotenuse of a right triangle using the pythagorian theorem.
THis is the code i made:

Code:
#include <stdio.h>

float Hypotenuse(float side1, float side2);

int main()
{
    float side1;
    float side2;

	printf("Enter the first side:");
	scanf("%f", side1);
	printf("Enter the second side:");
	scanf("%f", side2);

	float Hypotenuse(float side1, float side2);

	printf("The hypotenuse is %f", Hypotenuse);
}

float Hypotenuse(float side1, float side2)
{
	float Hypotenuse;

	square_of_side_1 = side1 * side1
	square_of_side_2 = side2 * side2
	sum_of_the_square_of_both_sides = square_of_side_1 + square_of_side_2
	Hypotenuse =

	return;
}
The code is incomplete, because i don't know how to make it solve for the square root of a number. Also, i don't really get how to use functions. Can someone please explain the return thing at the end of the function.

Thanks in Advance
 
Last edited by a moderator:
Incoming wall of text/info...

sqrt - C++ Reference

You need to include the Math library.

Code:
#include <math.h>

As far as using it, you just pass in a number. For example:

Code:
double num1, num2;

num1 = 2 * 2;
num2 = sqrt(num1);

printf(num2);

Also, you need to be using double instead of float. Don't think the C library supports sqrt() taking in a float, only a double (C++ does, however). You can try it, though.

As for using functions... it's a way to break up and modularize your code. It makes it easier to follow than having one great big block of code inside your main function. The "return" at the bottom of the function is what's going to be returned when execution is finished in that function. So, you have your function definition returning a float (because you have "float Hypotenuse(float side1, float side2)"). So you'll want to return your variable once you've calculated it.

BTW, don't make your float Hypotenuse variable the same name as your function. Also, always use lower-camel case when naming variables; i.e. "tempNumber". And for functions, use upper-camel case (like you already are), but name them after "actions" somewhat. I.e., instead of just naming it "Hypotenuse", name it "CalculateHypotenuse" that way it's clear exactly what the function is doing. This is important once you make larger programs that have a lot going on.

So, I would do this:

Code:
double CalculateHypotenuse(double side1, double side2)
{
	double hyp;

	//calculate logic here
        hyp = //sqrt logic here

	return hyp;
}

Now..for your main function... you're not calling your function correctly. You're putting a data type in front of the function call. You'll need to define a variable, and when the function returns the data, it will assign the data to that variable. You also don't need to declare the data type of your arguments that you're passing in, inside the function call.

Code:
double hyp = CalculateHypotenuse(side1, side2);

You also need to add "return 0;" at the end of your code for your main() function. You have "int main" which says main is going to return an integer. So you need to use return.

Code:
int main()
{
    //code
    return 0;
}
 
Back
Top Bottom