1. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12

## Question about some lines of code arrays

Hi, I have a question about some code in this program, it deals with arrays. I have commented the questions on the lines.
Any help greatly appreciated. Thank you.

Java Code:
```//********************************************************************
//  SodaSurvey.java       Author: Lewis/Loftus
//
//  Demonstrates the use of a two-dimensional array.
//********************************************************************

import java.text.DecimalFormat;

public class SodaSurvey
{
//-----------------------------------------------------------------
//  Determines and prints the average of each row (soda) and each
//  column (respondent) of the survey scores.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int[][] scores = { {3, 4, 5, 2, 1, 4, 3, 2, 4, 4},
{2, 4, 3, 4, 3, 3, 2, 1, 2, 2},
{3, 5, 4, 5, 5, 3, 2, 5, 5, 5},
{1, 1, 1, 3, 1, 2, 1, 3, 2, 4} };

final int SODAS = scores.length;
final int PEOPLE = scores[0].length;

int[] sodaSum = new int[SODAS];[COLOR="green"] //I don't understand this line[/COLOR]
int[] personSum = new int[PEOPLE];[COLOR="green"]//I don't understand this line either[/COLOR]

for (int soda=0; soda < SODAS; soda++)
for (int person=0; person < PEOPLE; person++)
{
sodaSum[soda] += scores[soda][person];[COLOR="green"]//I don't understand this line[/COLOR]
personSum[person] += scores[soda][person];[COLOR="green"]//Dont understand this line either[/COLOR]
}

DecimalFormat fmt = new DecimalFormat ("0.#");
System.out.println ("Averages:\n");

for (int soda=0; soda < SODAS; soda++)
System.out.println ("Soda #" + (soda+1) + ": " +
fmt.format ((float)sodaSum[soda]/PEOPLE));

System.out.println ();
for (int person =0; person < PEOPLE; person++)
System.out.println ("Person #" + (person+1) + ": " +
fmt.format ((float)personSum[person]/SODAS));
}
}```

2. Java Code:
```int[] sodaSum = new int[SODAS];
int[] personSum = new int[PEOPLE];```
What don't you understand? Both lines declare and initialise an int array with a length of whatever value is stored in the variables: SODAS and PEOPLE. Perhaps you are more used to seeing
Java Code:
`int[] arr = new int[10];`
which is basically the same except the length is hard coded.
Java Code:
```sodaSum[soda] += scores[soda][person];
personSum[person] += scores[soda][person];```
Same for these lines. Rather having a hardcoded number for the index of the array it is using a variable. Or are you not understanding the 2D array part?

3. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
Sorry I didnt specify what I didn't understand,It just looked like greek. But now I understand more of what I don't understand lol. I thought the above code should be int [] sodasum = SODAS; instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an array. and sodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array? Sorry for the confusion, thank you for helping.

I thought the above code should be int [] sodasum = SODAS;
No, that is attempting to assign a single int to an int array.
instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an array.
Yes it is creating an array. sodasum is a variable that references a 1D array of ints.
sodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array?
It isn't. It is accessing a single value in the 2D array at location [soda][person] and assigning it to location [soda] in the 1D array

5. To help clarify, the 2 d array also contains ints. It is simply an array of arrays where each array is filled with ints, so if you index into the first dimension you are looking at an array of ints. If you index into the first and second you are looking at an int. So it's safe to add the item at index [x][y] to a element of the one d array.

I hope this clarifies it somewhat, if not, let me know and I will try to do a better job explaining.

6. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
Great thank you Junky and Sunde887. I now understand it after writing out the loops and putting in the values like you would a math problem. That is the best way I think to figure out code. Treat it as a math problem. It is nice to see you Sunde887, I was away for a while. But now I'm learning java again every day or I try to. I am starting to see the art of computer programming, especially in these "elegant" little for loop applications that I have to stretch my non programmer brain to understand. The more compact the code, the more ingenious it is, and kind of an art form. Like looking at someone take a huge engine (bad programmer or non programmer) and redoing it the size of a lunch box (good programmer or experienced). I hope I can write an app that good as the one I just showed you. Since I don't have the mindset for it yet. The previous code I showed you is very ingenius to me. Thanks both again. Another hurdle jumped. Derek

7. Glad to see you again and hope all is well. How's the book coming? How far in have you gotten? The more you read and practice, the easier things get.

8. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
Thank you. Yes I still am reading "Java Software Solutions" after I had to take a long break due to other life things. But now I am back, and I am on page 405 out of about 700. Then I go back to "Head First Java" and finish that. Then I go to "Thinking in Java" and finish that. Then I go to effective java. I am not sure whether to do effective java or thinking in java first, but I will have to take a closer look at effective java. Also, the "Java Software Solutions" program exercises would have too many future-chapter-dependent requirements, so I decided to read the whole book then tackle the chapter program exercises. Because I just got frustrated by the first few chapter exercises when they would require something not covered yet. But it is great to be doing the book again and being on the forum, great people on this forum.

9. I know how the life interference stuff goes. I've been busy with some stuff lately and haven't been reading as much as I'd like. I'd say read head first java before effective java. I am just finishing effective java and I am really enjoying it. It isn't necessarily an intro book though. It gives you a lot of great things to keep in mind when designing and writing code.

If you are feeling good, you could consider trying effective java and thinking in java at the same time and back off whichever is too challenging.

10. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
Good idea. I will try effective java and thinking in java at the same time and like you said, back off on the harder one. thank you.

#### Posting Permissions

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