# Thread: 2D Array. method problem.

1. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## 2D Array. method problem.

making one temperature class for my homework and I'm lost is outer space now
Troubble is to get the methods done as they should be now.
getAverageEachDay() shall be taking the hours at the day, add them up and divide by the hours length. That iknow i should be doing. but the troubble is to execute it. :P

Java Code:
```class Temperature {
private String name;
private double[][] temp;

public Temperature(String name, double[][] temp2) {
this.name = name;
int day = temp2.length;
int hour = temp2[0].length;
temp =new double[day][hour];

for(int i=0;i<day;i++){
for(int j=0;j<hour;j++){
temp[i][j] = temp2[i][j];
}
}
}

public int getHours() {
return temp[0].length;
}

public int getDays(){
return temp.length;
}

public double[] getAverageEachDay() {

int Days = getDays();
int Hours = getHours();

double[] middelTemp = new double[Days];

for (int i=0; i<Days; i++) {
for (int j=0;j<Hours;j++) {
middelTemp[Days] = temp[i][j];
}
}
middelTemp = middelTemp / Hours;
return middelTemp;
}
}```

2. Senior Member
Join Date
Jun 2008
Posts
2,366
Rep Power
7

## Re: 2D Array. method problem.

Java Code:
`middelTemp = middelTemp / Hours;`
middelTemp is an array, right? So would it not be better if that line (adding the index of course) be better within the outer loop, but after the inner loop?

And, should you not also be using the "day" index (i) in the inner loop rather than using the length (Days) of the array as the index?

3. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

How do this method look like ?
Java Code:
```	public double[] getAverageEachDay() {

double Average = 0;

int Days = getDays();
int Hours = getHours();

double[] middelTemp = new double[Days];

for (int i=0; i<Hours; i++) {
for (int j=0;j<Days;j++) {
Average += temp[j][i];
}
Average = Average / Hours;
middelTemp[i] = Average;
}
return middelTemp;
}```

4. Senior Member
Join Date
Jun 2008
Posts
2,366
Rep Power
7

## Re: 2D Array. method problem.

You need to re-initiate "Average" for every iteration of the outer loop, of course, or the daily average will just get higher and higher, right?

Edit: P.S. just as a note /= also exists (just like the += you used earlier).

5. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

Thanks again! Global warming is just a kitten compared to what's gonna happen in the next weeks in this program :D

Java Code:
```	public double[] getAverageEachDay() {

double Average = 0;

int Days = getDays();
int Hours = getHours();

double[] middelTemp = new double[Days];

for (int i=0; i<Hours; i++) {
for (int j=0;j<Days;j++) {
Average += temp[j][i];
}
[B]Average /= Hours;[/B]
middelTemp[i] = Average;
[B]Average = 0;[/B]
}
return middelTemp;
}```

6. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

Working on a new methos now. I'm gonna get each columm and then get the average of those.
I'm not sure how the for loops should be at all either.

Java Code:
```	public double getHourAverage(){
int Days = getDays();
int Hours = getHours();
double avgHours = 0;
double[] avgHours2 = new double[Days];;

for(int i=0;i<Days;i++){
for(int j=0;j<Hours;j++){
}
}

return avgHours;```
the first loop will take the day and walk trough the days.
the seccond loop will take each hour in that day, witch is wrong.

I want hour[i] of all days and make the average out of that.

7. Senior Member
Join Date
Jun 2008
Posts
2,366
Rep Power
7

## Re: 2D Array. method problem.

Well, when the outer loop is days you are iterating over the rows, what do you think would happen if the outer loop used the hours (with the respective trading of which position the index is entered in).

8. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

something like this =
Java Code:
```	public double getHourAverage(){
int Days = getDays();
int Hours = getHours();
double avgHours = 0;
double[] avgHours2 = new double[Days];;

for(int i=0;i<Days;i++){
for(int j=0;j<temp[i].length;j++){
avgHours += temp[i][j];
}
avgHours /=temp[i].length;
avgHours2[i] = avgHours;
avgHours = 0;
}

return avgHours;
}```

9. Senior Member
Join Date
Jun 2008
Posts
2,366
Rep Power
7

## Re: 2D Array. method problem.

No. You need to use "Hours" in the outer loop and "Days" in the inner loop and swap the index references in the += line. Your result array will also need to be the correct length, of course (hint, the outer loop variable as it should be).

10. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

Perfect!
Finally closin in on the task now =)

11. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

Last method. to make one toString() method..

Java Code:
```	public String toString(){
String result = "";
result +=name;
result +=temp;
return result;
}```
Need to get one 2D array into string here, probably simple task.

12. Senior Member
Join Date
Jun 2008
Posts
2,366
Rep Power
7

## Re: 2D Array. method problem.

Another double loop, of course.

13. Member
Join Date
Oct 2011
Location
Tromsø
Posts
41
Rep Power
0

## Re: 2D Array. method problem.

yeh, ofcourse. never that easy is it ? :P
Java Code:
```	public String toString(){
String result = "";
String result2 = "";
result +=name+"\n";
int Days = getDays();
int Hours = getHours();
for (int i=0; i<Days; i++) {
for (int j=0;j<Days;j++) {
result2 += temp[i][j];
result2 +=" ";
}
result +=result2+"\n";
result2 = "";
}

return result;
}```

#### Posting Permissions

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