• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem finding average of rows and columns in 2d array.  RSS feed

 
Bryant Dinh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Honestly it's like 2 in the morning where i'm at and i think if i try to figure this out anymore its going to be sending me backwards rather than forwards. rowAverage() returns a one-dimensional array of doubles whose length is the number of rows in the two-dimensional input array and whose values are the averages of each row.

columnAverage() returns a one-dimensional array of doubles whose length is the number of columns in the two-dimensional input array and whose values are the averages of each column.

need help accumulating the total of each row in an array. then finding the average of each row and setting those values to an array.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully, you're getting some sleep and can attack it later when you're fresh, but what's the problem or your question?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is no such thing as a 2D array. What you have is an array of arrays. Since the arrays in that array can be different lengths, it is error-prone to try to calculate “column” averages. What is going to happen if you have an array like this?You will miss out the 7.89, and suffer an out of bounds Exception after the 12.34. You can calculate the averages of each of the arrays, but might have to use a default value of 0d for a 0-element array, but not for corresponding elements in the smaller arrays, unless you have ensured the larger array is a “rectangular” array.

Beside, using arrays like that suggests poor, non-object-oriented design. What do the arrays represent? Maybe they are students and their marks for their different exams? Like thisDo you see the problem? Brian doesn’t do English. If you put all those marks into an int[][] or double[][] array, what are you going to put for English? 0 isn’t right, and if you miss it out, Brian’s French mark will go towards the average for English.
so you see a linked arrays design is brittle and error-prone, depending on special circumstances, and ought to be replaced by a better object-oriented design.
 
Bryant Dinh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well reallly im kinda confused on how to find the length of the column. i was led to believe that it was just row[0].length and yes the class im testing it with does have the same number of columns throughout. but what im guessing what im really having trouble with is accumulating the sum of each row into the array total[] which i looked back and noticed that total need to be an array. i thought about it and total[] should equal the length of the column because each row would be added up.
 
Pondurai Singh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MODERATOR ACTION: Removed code.

Pondurai Singh, please DontBeACodeMill.(⇐click)
 
Bryant Dinh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

There is no such thing as a 2D array. What you have is an array of arrays. Since the arrays in that array can be different lengths, it is error-prone to try to calculate “column” averages. What is going to happen if you have an array like this?You will miss out the 7.89, and suffer an out of bounds Exception after the 12.34. You can calculate the averages of each of the arrays, but might have to use a default value of 0d for a 0-element array, but not for corresponding elements in the smaller arrays, unless you have ensured the larger array is a “rectangular” array.

Beside, using arrays like that suggests poor, non-object-oriented design. What do the arrays represent? Maybe they are students and their marks for their different exams? Like thisDo you see the problem? Brian doesn’t do English. If you put all those marks into an int[][] or double[][] array, what are you going to put for English? 0 isn’t right, and if you miss it out, Brian’s French mark will go towards the average for English.
so you see a linked arrays design is brittle and error-prone, depending on special circumstances, and ought to be replaced by a better object-oriented design.



well my book java for everyone calls it a 2 dimensional array and the class im testing it with has the same number of columns.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryant Dinh wrote:well reallly im kinda confused on how to find the length of the column. i was led to believe that it was just row[0].length


If every element of row has the same length, then you can picture it as a rectangular 2D array, where one dimension is row.length and the other dimension is row[0].length. It's worth keeping in mind though that, as Campbell pointed out, it's not actually a 2D array, just an array whose elements are all references to other arrays. That will matter more in other situations.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryant Dinh wrote:well reallly im kinda confused on how to find the length of the column. i was led to believe that it was just row[0].length . . ..
No, it is not row[0].length. If you go back to my second example, row[0].length is how many exams Alan sat, ie 5. You have confused yourself by choosing row for the name of your array. A column would be how many people sat French (5), or would it be how many people sat English (4). Now you see why I set up those examples. It is to show you that using multiple arrays can fail to mimic the real world.
You might be lucky having a situation where your array really is “rectangular”, but that is an unusual and atypical situation in real life. It says in the Java tutorials
In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays.
so it is not really a multidimensional array, and as I have shown, it is better than a 2D array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!