Jump to content


Photo

Calculate area of a rectangle?


  • Please log in to reply
10 replies to this topic

#1 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 08 December 2010 - 06:23 PM

the idea is that the function to calculate the area to be used inside the function perimeter, so that the perimeter function handles all printing.

This is what I've done but it's not the same as what I have mentioned above.
What Should I do?


This is what I want to do

Create a method called "Area ()" that calculates the area of a rectangle.
Call the method "Area ()" method from within "perimeter ()", submit
length and width as an argument to the method "Area ()" and use these to
calculate the area. Modify Approach "perimeter () "so that it prints the perimeter
and area when the user presses the button "CALCULATE". No printing will be done in
Approach "Area () "



<body>

		<h1>PHP-sida 4</h1>

		<form action="php4.php" method="post">

			<p>Längd: <input type="text" name="length" size="20" /></p>

			<p>Bredd: <input type="text" name="width" size="20" /></p>

			<p><input type="submit" value="CALCULATE" name="calc" /></p>

		</form>




<?php



if (isset($_POST['calc']))
{
	$length= $_POST['length'];
	$width= $_POST['width'];
	echo "length:" . $length. "<br><br>";
	echo "width:" . $width. "<br><br>";
	

function perimeter($length, $width)
{

return  2* ($length+ $width);

}
$perimeter= perimeter($length, $width);

echo "perimeter:" . $perimeter. "<br>", "<br>";
	
function area($length, $width)
{
	return $length* $width;
}
$area = area($length, $width);
echo "area:" . $area ;

}
 
?>



	</body>


#2 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 08 December 2010 - 06:57 PM

Anyone  :confused: please help?

#3 litebearer

litebearer

    Advanced Member

  • Members
  • PipPipPip
  • 2,357 posts
  • Locationwhite lake michigan

Posted 08 December 2010 - 07:07 PM

do you mean...
$length=20;
$width=10;
function area($a,$b) {
  return $a*$b;
}
function perimeter($a,$b) {
  echo "Length: " . $a . "<br>":
  echo "Width  : " . $b . "<br>";
  echo "Perimeter: " .  (2 * ($a + $b)) . "<br>";
  echo "Area : " . area($a,$b);
  return;
}

  
}

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#4 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 08 December 2010 - 07:19 PM

I am  not allowed to give the length and the width.

and I tried not to give the length and width but it did not work.

It's not giving me the area and perimeter :(

#5 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 08 December 2010 - 07:44 PM

how can I solve this?  HELP :confused:

#6 litebearer

litebearer

    Advanced Member

  • Members
  • PipPipPip
  • 2,357 posts
  • Locationwhite lake michigan

Posted 08 December 2010 - 09:53 PM

Not sure what you mean by

I am  not allowed to give the length and the width.

How are the functions supposed to work if they are not supplied the necessary information?

BTW - I had a small typo above - here is corrected and TESTED version (it DOES work)
<?PHP

function area($a,$b) {
  return $a*$b;
}

function perimeter($a,$b) {
  echo "Length: " . $a . "<br>";
  echo "Width  : " . $b . "<br>";
  echo "Perimeter: " .  (2 * ($a + $b)) . "<br>";
  echo "Area : " . area($a,$b);
  return;
}
$length = 10;
$width = 5;
echo perimeter($length,$width);
?>

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#7 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 09 December 2010 - 06:56 AM

Create a method called "perimeter ()" that calculates the perimeter of a rectangle. The function to receive the two parameters "long" and "width" of a form. perimeter shall then be printed on the page when the user presses a button titled "CALCULATE".

Create a method called "Area ()" that calculates the area of a rectangle.
Call the method "Area ()" method from within "perimeter ()", submit
length and width as an argument to the method "Area ()" and use these to
calculate the area. Modify Approach "perimeter () "so that it prints the perimeter
and area when the user presses the button "CALCULATE". No printing will be done in
Approach "Area () "


It's like this that the person that visits this page is going to input any number and that presses the button calculate and than the output
is like:

length: the number given by the person
width: the number given by the person
perimeter: answer
area: answer

#8 Buddski

Buddski

    Advanced Member

  • Members
  • PipPipPip
  • 1,236 posts
  • LocationVictoria, Australia

Posted 09 December 2010 - 07:21 AM

From what I understand.. litebearer has hit the nail right on the head. Does exactly as specified.
"The problem with a UDP joke is that you have no idea if people got it."
--------------------------------
When posting an error that contains a line number, please show what that line actually contains.

#9 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 09 December 2010 - 07:32 AM

this is what I mean

<?php



if (isset($_POST['calc']))
{

	

$length= $_POST['length'];

	

$width= $_POST['width'];

	

echo "length:" . $length. "<br><br>";

	

echo "width:" . $width. "<br><br>";




I am not allowed to give the
$length = 10;
$width = 5;


It's like this anyone that going to see this page can give what ever length and what ever width that they want in the text-box and than enters the calculate button and gets the length, the width that he/she gave and the area and the perimeter that was calculated.

#10 Buddski

Buddski

    Advanced Member

  • Members
  • PipPipPip
  • 1,236 posts
  • LocationVictoria, Australia

Posted 09 December 2010 - 07:41 AM

You need to merge your code with what has been provided...
<?php
// FUNCTIONS //

function area($a,$b) {
  return $a*$b;
}

function perimeter($a,$b) {
  echo "Length: " . $a . "<br/>";
  echo "Width  : " . $b . "<br/>";
  echo "Perimeter: " .  (2 * ($a + $b)) . "<br/>";
  echo "Area : " . area($a,$b);
  return;
}

?>
<html>
	<body>
		<h1>PHP-sida 4</h1>
		<form action="php4.php" method="post">
			<p>Längd: <input type="text" name="length" size="20" /></p>
			<p>Bredd: <input type="text" name="width" size="20" /></p>
			<p><input type="submit" value="CALCULATE" name="calc" /></p>
		</form>
		<?php
		
		if (isset($_POST['calc']) && !empty($_POST['length']) && !empty($_POST['width'])) {
			echo 'Processing...<br/>';
			echo perimeter($_POST['length'],$_POST['width']);
		} else {
			echo 'You need to specify a length and a width.';
		}
		
		?>
	</body>
</html>

"The problem with a UDP joke is that you have no idea if people got it."
--------------------------------
When posting an error that contains a line number, please show what that line actually contains.

#11 newbe123

newbe123

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts

Posted 09 December 2010 - 08:01 AM

Thank you so much for helping me

The part that I did not understand and had problem with was
"the idea is that the function to calculate the area to be used inside the function perimeter, so that the perimeter function handles all printing."






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com