How do I loop my pythagorean theorem program?
Hey, I made this program that could execute the pythagorean theorem. Every time I try to make it loop I get an error saying that nested functions are disabled. Can one of you tell me how I can make this program loops. Thanks.
This is the program:
#include <stdio.h>
float function (float x, float y);
float function2 (float x, float z);
float function3 (float y, float z);
float main()
{
float x;
float y;
float z;
{
printf("---------------------------------------------------------------");
getchar();
printf("Welcome to right triangle side length calculator");
getchar();
printf("If you do not know the legth of the side, enter 0");
getchar();
printf("Please insert length of the first leg: ");
scanf("%f", &x);
printf("Please insert length of the second leg: ");
scanf("%f", &y);
printf("Please insert length of the hypotenuse: ");
scanf("%f", &z);
}
{
if (z==0){
printf("The length of the hypotenuse is %f\n开发者_如何转开发", function (x, y));}
else if (y==0){
printf("The length of the second leg is %f\n", function2(x, z));}
else if (x==0){
printf("The length of the first leg is %f\n", function3(y, z));}
}
printf(" - A Laszlo Solutions Program -\n");
printf("---------------------------------------------------------------");
getchar();
}
float function(float x, float y) {
return(sqrt(((x*x)+(y*y))));
}
float function2(float x, float z) {
return(sqrt(((z*z)-(x*x))));
}
float function3(float y, float z){
return(sqrt(((z*z)-(y*y))));
}
You need to move the definitions of function1
, function2
and function3
outside main
.
(Your code contains no loops, nested or otherwise. I wonder whether you are misunderstanding the word "loop", which refers to having the same code executed repeatedly.)
There are Some Problems in your code
1. Return Type of main
float main()
{
return 0.0; //missing return statement
}
2. Check braces
float main()
{
{
//some statements
}
{
//some statements
}
}
3. Unnecessary Functions
float function1(float x, float y)
{
return(sqrt(((x*x)+(y*y)))); // since, three functions are doing same
// You can use only a function
}
精彩评论