Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-11-2013, 10:31 AM   #1
LQ Newbie
Registered: Mar 2013
Posts: 5

Rep: Reputation: Disabled
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

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

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>");

Old 05-11-2013, 11:46 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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
1 members found this post helpful.
Old 05-12-2013, 06:05 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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:

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.

Last edited by David the H.; 05-12-2013 at 06:10 PM. Reason: as stated



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
print values from array (Is there a better way?) awreneau Programming 2 04-29-2013 09:27 PM
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 04:41 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 10:38 PM
Comparing Multiple Values in while loop rahulruns Programming 5 10-27-2009 06:59 AM
extracting values from an array bahadur Programming 2 08-14-2006 02:07 AM

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration