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:
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;
}```
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.

2. Member
Join Date
Feb 2008
Location
Oregon, USA
Posts
49
Rep Power
0
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.

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; ??????

4. 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...

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..

6. simply counter++ would work.

7. Member
Join Date
Oct 2008
Posts
33
Rep Power
0
But then I need a loop inside the function... Couldnt work..

8. 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.

9. A counter in Java is just a variable that adds one to it's self on every pass of the loop...
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]
}```
counter++ actually means:
Java Code:
`counter = counter +1;`
So now the counter variable contains the number of times the "for" loop looped.

Luck,
CJSL

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..

11. 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
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++;
}```
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.

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;
}

13. 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 07:23 PM.

14. Member
Join Date
Oct 2008
Posts
33
Rep Power
0
Guess I dont get it..

15. 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.

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,
CJSL

17. Member
Join Date
Oct 2008
Posts
33
Rep Power
0

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;
}```
Seems to be some sort of mistake somewhere.. Thanks for them answers anyway...

18. 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.

• 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];
}```

19. Member
Join Date
Oct 2008
Posts
33
Rep Power
0

Was just being frustrated having used my whole weekend on this thing..

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•