English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

Can someone explain these lines of code? This program is for generating temperature of ten locations. I’m learning Array and having difficulty to understand some things.


public class WeatherFan
{
public static void main(String[] args)
{
//Temperature Array
float[][] temperature = new float[10][365];

//Generate Temperatures
for (int i = 0; i < temperature.length; i++)
for (int j = 0; j < temperature[i].length; j++)
temperature[i][j] = (float)(45.0*Math.random() - 10.0);

// Calculate the average per location
for (int i = 0; i < temperature.length; i++)
{
// place to store the average
float average = 0.0f;

for (int j = 0; j < temperature.length; j++)
average += temperature[i][j];
// Output the average temperature of the current location
System.out.println("Average temperature at location +"
+ (i+1) + " = " + average/(float)temperature[i].length);
}
}
}




Explain temperature[i].length and temperature[i][j] in these lines. I’m having difficulty to understand [i] and [i][j] what will these lines do and how will? Please explain these.


for (int j = 0; j < temperature[i].length; j++)
temperature[i][j] = (float)(45.0*Math.random() - 10.0);

2007-06-30 02:47:00 · 2 answers · asked by Adnan A 1 in Computers & Internet Programming & Design

2 answers

The array is:
float temperature[10][365]

The way to think of a two-dimensional array of floats is that it's an "array of (arrays of floats)."

temperature[0] is an array of 365 floats.
temperature[0][0] is the first element of that array.

So, for example this code, with comments

for (int i = 0; i < temperature.length; i++) {
// For each array-of-floats
for (int j = 0; j < temperature[i].length; j++) {
// For each float in the i'th array
temperature[i][j] = (float)(45.0*Math.random() - 10.0);
// Set its value to a random number
}
}

2007-06-30 03:02:50 · answer #1 · answered by McFate 7 · 0 0

A [4][10] or [i][j] array would probably look something like this:
(where the values 0-9 indicate the j values, 0-3 show the i values)

0 ►| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ▲
1 ►| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |- || temperature[i].length
2 ►| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |- || from 0-3, all 4 slots
3 ►| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ▼ filled by FOR loop
||||||||||◄▬▬▬▬▬▬▬▬▬▬▬►
||||||||||||||||| temperature.length
||||||||||||||||| from 0-9, all 10 slots filled by FOR loop

Before you assign any value, this is probably what your float array looks like.

0 ►| null | null | null | null | null | null | null | null | null | null |
1 ►| null | null | null | null | null | null | null | null | null | null |
2 ►| null | null | null | null | null | null | null | null | null | null |
3 ►| null | null | null | null | null | null | null | null | null | null |

lets say you assign a value to the array
temperature[0][0]=45.6; and another statement
temperature[3][3]=47.9;
the result will be your array looking something like this:

0 ►| 45.6 | null | null | null | null | null | null | null | null | null |
1 ►| null | null | null | null | null | null | null | null | null | null |
2 ►| null | null | null | null | null | null | null | null | null | null |
3 ►| null | null | null | 47.9 | null | null | null | null | null | null |


for (int i = 0; i < temperature.length; i++)
for (int j = 0; j < temperature[i].length; j++)
temperature[i][j] = (float)(45.0*Math.random() - 10.0);

so what this code extract does is assign each and every part of the array with a randomised value.
Likewise you could interchange temperature.length with value 365 and temperature[i].length with value 10 in the for loop. This will still achieve the same effect as the statement before, and help to facilitate a better understanding of how the array works.

if my own explanation wasn't that clear, you can check this link out:
http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm
Hope this helps.
cheers

2007-06-30 06:30:18 · answer #2 · answered by Sheqi Nonda 3 · 0 0

fedest.com, questions and answers