Jump to content

Help with calculating and return


kakes

Recommended Posts

Hello all!

Got another code that I need a helpful nudge with. I need to create a function to calculate the area of a circle. The function has to take in two arguments (a number and a string). The number is the diameter; the string is the word "diameter." The function has to return the area of the circle or a -1 for an error. The script has to initialize test variables, call the function and display the results. I'm such a noob so don't laugh when you see my code thus far. I appreciate any help you can offer.

 

<? php
// variables
$diameter = 4.0;
$pi = 3.14;
$area = ($diameter * $pi);
$title = "Circle";
         
function circle_function (4.0, "diameter")
          if ($diameter * $pi) !=$area)
       {
         return $area;
        }
         
         {
         else 
         return -1; 
         }
         if ($area == -1)
         {
         echo "Invalid input";
         exit -1;
         }
         
         echo "<html>
	<head>
		<title>
		$title
		</title>
	</head>
<body>
   <h1>Area of a Circle</h1>\n";
      echo circle_function();
   
   echo    "</body>
		</html>";
?>

Link to comment
Share on other sites

Okay, I've worked really hard on this and almost have it. There's just one small thing that I can't get to work right. Here's my new code.  :-\

 

<?php
function circleArea($total, $area, $diameter)
     {
         // Calculate area of circle or returns a -1 if area is not diameter x pi
         if ($area == "Area")
            {
               $answer = $diameter * $pi;
               $answer .= "Area";
            } 
         elseif ($radius == "Diameter")
            {
                $answer = $raduis * $radius;
                $answer .= "Diameter";
            }
         else
            {
                $answer = -1;
            }
         return $answer;
     }

$title = "Calculate Area of Circle";
$diameter=4.0;
$radius=2.0;
$pi=3.14;
$total=$diameter * $pi;

$total = circleArea($area, "Total", $diameter);
$area = circleArea($total, "Area", $area);
$error = circleArea(1, "Stuff", 10);



echo "<html>
   <head>
      <title>
         $title 
      </title>
   </head>
   <body>
       <h1>
          $title
       </h1>
       <h3>
          If the diameter of a circle is $diameter the total area is $total.
       </h3>

   </body>
</html>\n";
                      
?>

Link to comment
Share on other sites

You have a few scope issues. Variables like $pi, $radius, etc.. that are defined outside of the function and aren't passed in won't be accessible in that context (You don't even need to define $pi yourself, you can use the predefined constant M_PI - it will be more accurate anyway).

 

You've named things in a strange way that don't really make it clear as to what your intentions are; For example, "if($radius == "Diameter")", that doesn't really make sense, and I'm not sure what you're trying to accomplish with that.

 

You also seem to have some problems with your formulas. For example, the area of a circle is: [imath]A = \pi r^2[/imath] (you can do exponents in PHP using the pow function).

Link to comment
Share on other sites

Your description of what you need to do doesn't seem complete, or consistent with your example. Why do you need to take a parameter "diameter"? Where should that parameter be used?

 

A simple example of what it sounds like you want:

 

function area_from_diameter($diameter, $str) {
if($diameter < 0) {
	return -1;
}
return M_PI * pow($diameter / 2, 2);
}

$diameter = 5;
echo area_from_diameter($diameter, "diameter");

Link to comment
Share on other sites

I've been working on this as you'll see but it still doesn't work quite right. Any suggestions? Thanks!!

 

<?php
// variables
$diameter = 6;
$pi = 3.14;
$area = ($diameter * $pi);
$title = "Circle";
        function circle_area($num,$type="radius")
{	
if(is_numeric($num) && is_string($type) && !empty($num) && !empty($type))	
{		
$radius = ($type == "diameter") ? $num/2 : $num; // Radius is set to half the given number if the type is diameter, or unchanged if radius		
$area = pi()*pow($radius,2); // Pi as defined by php multiplied by radius to the power of 2 (squared)		
return $area; // Return result	
}	
else	
{		return -1; // Return Pi when invalid/empty values are given	
}
}
        
        echo "<html>	
        <head>
        <title>			
            $title			
         </title>		
        </head>	
         <body>   
            <h1>Area of a Circle</h1>\n";      

         echo circle_area(6,"diameter");      
         echo    "</body>			</html>";
?>

 

I know, I need to clean up my code.  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.