# [SOLVED] Multidimensional array

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-21-2009, 06:48 PM
Torgero
[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:
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;         }```
This outputs all the temperature together.. But I need a method that divides this on all the temperatuers.

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, 08:16 PM
Bluefox815
Quote:

Originally Posted by Torgero
This outputs all the temperature together.. But I need a method that divides this on all the temperatuers.

What exactly are you asking for when you ask for a method that divides the sum on all temperatures? What do you need the sum to be divided by? Think about what you really want to do, mathematically, then see if you can figure out how to write the function.
• 03-21-2009, 09:28 PM
Torgero
I need it to be divided by hours in the whole array..

sum = sum / temp[time].length; ??????
• 03-21-2009, 10:22 PM
Fubarable
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, 01:43 AM
Torgero
I understand why dividing by temp.length dont work, but cant figure out how to use a counter in the function..
• 03-22-2009, 04:08 AM
Fubarable
simply counter++ would work.
• 03-22-2009, 02:51 PM
Torgero
But then I need a loop inside the function... Couldnt work..
• 03-22-2009, 03:09 PM
Fubarable
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, 03:48 PM
CJSLMAN
A counter in Java is just a variable that adds one to it's self on every pass of the loop...
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]                 }```
counter++ actually means:
Code:

`counter = counter +1;`
So now the counter variable contains the number of times the "for" loop looped.

Luck,
CJSL
• 03-22-2009, 05:34 PM
Torgero
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..
• 03-22-2009, 05:43 PM
Fubarable
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
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++; }```
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.
• 03-22-2009, 07:14 PM
Torgero
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;
}
• 03-22-2009, 07:21 PM
Fubarable
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?
• 03-22-2009, 07:34 PM
Torgero
Guess I dont get it..
• 03-22-2009, 07:37 PM
Fubarable
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, 08:07 PM
CJSLMAN
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,
CJSL
• 03-22-2009, 10:36 PM
Torgero
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;         }```
Seems to be some sort of mistake somewhere.. Thanks for them answers anyway...
• 03-22-2009, 11:11 PM
Fubarable
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, 11:36 PM
CJSLMAN
• 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.

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];     }```
• 03-22-2009, 11:55 PM
Torgero