Results 1 to 20 of 21
Thread: [SOLVED] Multidimensional array
- 03-21-2009, 05:48 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
[SOLVED] Multidimensional array
I am trying to make a multidimensional array that saves temperatures in an month by day and hour. What I need is a method that put all the temperatures together and outputs and mean temperature for the whole month.. What I got now is a method that adds all temps together, put how can I make it so that it divides this number on all the hours in the hour??
Here is code:
This outputs all the temperature together.. But I need a method that divides this on all the temperatuers.PHP Code:// Overall temp, all put together: public double finnGjesnittTotal(){ double sum = 0; for (int dag = 0;dag <temp.length; dag++){ for (int time = 0; time < temp[dag].length; time++){ sum += temp[dag][time]; } } return sum; }
I tried to divide this on this:
sum = sum / (lenth[time].length * length[dag].length);
Does not work..
Dag = day
Time = hour
Any clues.
- 03-21-2009, 07:16 PM #2
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
- 03-21-2009, 08:28 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
I need it to be divided by hours in the whole array..
sum = sum / temp[time].length; ??????
-
so you basically need to count the number of times you add a temp to the sum.... if only you could use a counter int variable that incremented each time a temp were added...
- 03-22-2009, 12:43 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
I understand why dividing by temp.length dont work, but cant figure out how to use a counter in the function..
-
simply counter++ would work.
- 03-22-2009, 01:51 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
But then I need a loop inside the function... Couldnt work..
-
No you don't. No extra loop needed. You just use the one you are currently using to add sums. Come on think on this. It's not that hard.
- 03-22-2009, 02:48 PM #9
A counter in Java is just a variable that adds one to it's self on every pass of the loop...
counter++ actually means:Java Code:[B][COLOR="Blue"]int counter = 0;[/COLOR][/B] for (int i= 0; i < someThing.length; i++) { System.out.println("Array value = " + someThing[i]); [COLOR="blue"][B]counter++;[/B][/COLOR] }
So now the counter variable contains the number of times the "for" loop looped.Java Code:counter = counter +1;
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-22-2009, 04:34 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Yes but outside the loop, time equals zero..
I tried:
sum += temp[dag][time] / time; (Divided by zero message..)
I tried it all..
And may be easy, but everything is easy if you know it..
-
1) declare counter as an int variable before the loop.
2) increment counter via counter++; inside the loop
3) do the division of sum / counter after the loop.
What you need to do is pretend your mind is the computer and walk through each step of your code including looping imagining what the computer is doing at that moment.
For instance if I have code like so:I
I walk through each step in the code, and then I loop in my mind (or on paper) the 5 times through the loop, thinking what is happening to sum, what is happening to counter, each iteration through the loop. If the loop is huge, then I just do a few iterations, whatever it takes to get a feel for the code. This is how you learn to know what your program is doing, and how to make it work better.Java Code:int[] myArray = {1, 2, 3, 4, 5}; int sum = 0; int counter = 0; for (int i = 0; i < myArray.length; i++) { sum += myArray[i]; counter++; }
- 03-22-2009, 06:14 PM #12
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Does not work..
// Gjennomsnittet totalt:
public double finnGjesnittTotal(){
double sum = 0;
double counter;
for (int dag = 0;dag <temp.length; dag++){
for (int time = 0; time < temp[dag].length; time++; counter++){
sum += temp[dag][time] / (double) counter;
}
}
return sum;
}
-
Your code looks as if you've not read any of the recent posts, and it appears that you are not following any of our recommendations. Why?
Last edited by Fubarable; 03-22-2009 at 06:23 PM.
- 03-22-2009, 06:34 PM #14
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Guess I dont get it..
-
You're doing your division within the loop and we told you not to do this.
I think that you're at the point where you should to talk to your instructor, get some remedial help asap.
- 03-22-2009, 07:07 PM #16
hhhmmm...
I find it hard to understand that somebody can understand nested "for" loops, but can't grap the concept of a counter and averages.
Strange...
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-22-2009, 09:36 PM #17
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Ok, tried to follow your example..
Seems to be some sort of mistake somewhere.. Thanks for them answers anyway...PHP Code:// Gjennomsnittet totalt: public double finnGjesnittTotal(){ double sum = 0; for (int dag = 0; dag <temp.length; dag++){ int counter = 0; for (int time = 0; time < temp[dag].length; time++){ sum += (temp[dag][time]); counter++; sum = sum / (double) counter; } } return sum; }
-
I'm about to pull my hair out.
How can I tell you so that you'll understand not to do the division inside of the for loop? What about this statement doesn't make sense. Again, please see your teacher asap. You are missing some basic concepts that we can't help you with.
- 03-22-2009, 10:36 PM #19
- Take the counter variable initializaion out of the "for" loop...
- Don't reuse the sum variable... call it something like "avg" because that is what it is.
Java Code:public double finnGjesnittTotal(){ double sum = 0; [COLOR="Blue"][B]int counter = 0;[/B][/COLOR] [COLOR="Blue"][B]double avg = 0.0;[/B][/COLOR] for (int dag = 0; dag <temp.length; dag++){ for (int time = 0; time < temp[dag].length; time++){ sum += (temp[dag][time]); counter++; } } [B][COLOR="Blue"]avg = sum / (double) counter;[/COLOR][/B] return [B][COLOR="Blue"]avg[/COLOR][/B]; }Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-22-2009, 10:55 PM #20
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
Multidimensional hashtable?
By jklsemicolon in forum New To JavaReplies: 6Last Post: 08-17-2008, 05:23 AM -
Make Java codes more simplier (Multidimensional Arrays)
By javanewbie in forum JCreatorReplies: 9Last Post: 06-25-2008, 04:48 AM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM -
Multidimensional arrays
By Java Tip in forum Java TipReplies: 0Last Post: 11-05-2007, 05:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks