LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   trouble with a FOR loop not adding values from an array like it should (http://www.linuxquestions.org/questions/programming-9/trouble-with-a-for-loop-not-adding-values-from-an-array-like-it-should-4175461541/)

ashevillan 05-11-2013 09:31 AM

trouble with a FOR loop not adding values from an array like it should
 
so my $summerTotal value is coming up to equal 0 every time and i have no idea why. no error messages or anything and everything else sums up correctly

<html>
<head>
<title>Annual Rainfall Report</title>
<link rel ="stylesheet" type="text/css" href="sample.css" />
</head>

<body>
<?php
print ("<h1>Annual Rainfall Report</h1>");

$rainData = array(0.3, 0.6, 1.2, 1.1, 0.35, 0.0, 0.25, 0.6, 0.0, 0.12, 0.23, 0.2, 0.234,
1.2, 0.1, 2.1, 2.45, 1.3, 0.7, 0.0, 0.0, 0.13, 0.12, 0.07, 0.1, 0.0,
0.8, 1.0, 0.55, 0.12, 0.5, 0.21, 1.01, 0.0, 0.0, 0.08, 0.09, 1.01, 0.76,
0.08, 0.3, 0.0, 0.9, 0.7, 1.3, 0.7, 0.09, 2.32, 1.28, 0.953, 0.75, 1.01);

$totalRain = 0;
$traceCount = 0;
$heavyRainCount = 0;
$summerTotal = 0;

for ($i = 0; $i < sizeof($rainData); $i=$i+1)
{
$totalRain = $totalRain + $rainData[$i];

if ($rainData[$i] <= .02)
$traceCount = $traceCount + 1;

if ($rainData[$i] >= 2)
$heavyRainCount = $heavyRainCount + 1;
}
$averageRain = $totalRain / sizeof($rainData);

for ($i = 0; $i < sizeof($rainData) ; $i=$i+1)
{
if ($i >= 27 or $i <= 39)
$summerTotal = $summerTotal + $raindata[$i];
}
print("<p>TOTAL RAIN: $totalRain.</p>");
print("<p>AVERAGE WEEKLY RAIN: $averageRain.</p>");
print("<p>WEEKS WITH TRACE OR NO RAIN: $traceCount.</p>");
print("<p>WEKS WITH HEAVY RAIN(2in. OR MORE): $heavyRainCount.</p>");
print("<p>TOTAL SUMMER RAIN: $summerTotal.</p>");
?>

</body>
</html>

grail 05-11-2013 10:46 AM

Well I see 2 issues:

1. Assuming variables are case sensitive, you have $raindata instead of $rainData

2. Check your boolean logic for the if, currently it is ALWAYS true

David the H. 05-12-2013 05:05 PM

Not a php user here, but some general comments on the code anyway.

Can't you move the content of the second loop into the first, and calculate $SummerTotal at the same time as the full count?

Since "sizeof($rainData)" appears several times, you might consider running it once and saving the value into a variable to use instead.

I also imagine that php can accept "i++" style incrementation, instead of the clunkier "$i=$i+1"? It might even have something like "$totalRain += $rainData[$i];", for that matter.

Edit: Yes, it has both operators:
http://php.net/manual/en/language.op....increment.php

One comment says that "$i += 1" works the fastest in his tests.


Oh, yeah. Before I forget again, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


All times are GMT -5. The time now is 02:59 AM.