# 2D Array. method problem.

Printable View

• 11-10-2011, 10:39 AM
Juukamen
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

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;         } }```
• 11-10-2011, 11:23 AM
masijade
Re: 2D Array. method problem.
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?
• 11-10-2011, 12:19 PM
Juukamen
Re: 2D Array. method problem.
How do this method look like ?
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;         }```
• 11-10-2011, 12:25 PM
masijade
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?

Talk about global warming.

Edit: P.S. just as a note /= also exists (just like the += you used earlier).
• 11-10-2011, 12:35 PM
Juukamen
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

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;         }```
• 11-11-2011, 12:27 AM
Juukamen
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.

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.
• 11-11-2011, 09:47 AM
masijade
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).
• 11-11-2011, 10:09 AM
Juukamen
Re: 2D Array. method problem.
something like this =
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;         }```
• 11-11-2011, 10:44 AM
masijade
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).
• 11-11-2011, 01:09 PM
Juukamen
Re: 2D Array. method problem.
Perfect!
Finally closin in on the task now =)
• 11-11-2011, 01:31 PM
Juukamen
Re: 2D Array. method problem.
Last method. to make one toString() method..

Code:

```        public String toString(){                 String result = "";                 result +=name;                 result +=temp;                 return result;         }```
Need to get one 2D array into string here, probably simple task.
• 11-11-2011, 02:08 PM
masijade
Re: 2D Array. method problem.
Another double loop, of course.
• 11-11-2011, 02:31 PM
Juukamen
Re: 2D Array. method problem.
yeh, ofcourse. never that easy is it ? :P
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;         }```