# Array Help

• 03-01-2009, 06:15 PM
ks1615
Array Help
Hello,
I'm wondering if its possible to do mathematic opeartions using array
I've been trying 20 different methods doing this (changed the double values into a string)
For example
Code:

```double [] inches = {1, 2, 3, 4, 5, 6, 7}; double [] centimeter = {inches * 2.54}; System.out.println(centimeter);```
• 03-01-2009, 06:29 PM
Fubarable
It's possible to do math with arrays, but not as you're trying. You need to write out everything yourself. For instance if you want a method that multiples a scalar (i.e., 2.54) by an array, then you should create this method and do the multiplication in a for loop. Please take care to only do manipulations on a copy of the array because since an array is an object and an array variable is a reference variable, changes made to the array inside of the method will be reflected in the array outside of the method.
• 03-01-2009, 06:45 PM
ks1615
So something like this
Code:

```double [] inches = {1, 2, 3, 4, 5, 6, 7}; for(int i = 0, i < 8; i++) { double [] cm = {inches * 2.54}; }```
Or like this?
Code:

```double [] inches = {1, 2, 3, 4, 5, 6, 7}; double [] cm = {1 * 2.54, 2 * 2.54, 3 * 2.54, etc}```
• 03-01-2009, 06:59 PM
MuslimCoder
I think i would go with the first one...
Though I think that you should not use the "magic number"-that's what someone told me when I explicitly specified the length of the array in one of my coding (of course not in this forum)-

You should use
Code:

`array.length()`
:)
• 03-01-2009, 07:09 PM
CJSLMAN
I don't think so....
Code:

```double [] inches = {1, 2, 3, 4, 5, 6, 7}; for(int i = 0, i < 8; i++) { double [] cm = {inches * 2.54}; }```
I don't think the above is going to work. It's full of errors. It would have to be somethng like:
Code:

```public class Inches2cms {         public static void main (String[] arg) { double [] inches = {1, 2, 3, 4, 5, 6, 7}; double [] cms = new double [inches.length]; for(int i = 0; i < inches.length; i++)         {         cms [i] = inches[i] * 2.54;         System.out.println(cms[i]);//added to be able to see if it works         } }```
}
• 03-01-2009, 07:42 PM
ks1615
Thx for the responses I fixed my first program but now I'm stuck on this one.
I have to use two given double array (One is tempreture, the other is humidity) and use both of them to get Heat Index. I've got the equation written out but i'm stuck on a few minor things
I have to use a for-each loop also
Here is the code
What should I place in ___.length?
How would I structure the for each loop?
Code:

```public class HeatIndex {   public static void main(String [] args)   {       System.out.println("\t\t\tHeat Index: Key West, Florida");       System.out.println("\n\t\t\t\tMonths");       String [] month = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.", "Avg."};       double [] temp = {{70.3 , 70.8, 73.8, 77.0 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0, 78.1}};       double [] humid = {69, 67, 66, 64, 66, 69, 67, 70, 69, 69, 70, 68};       double [] heatIndex = new double [______.length];       for(int i = 0; i < ; i++)       {           double heatIndex = -42.379 + (2.04901523 * temp) + (10.14333127 * humid) - (0.22475541 * temp * humid) - (.00683783 * temp * temp) - (.05481717 * humid * humid) + (.00122874 * temp * temp * humid) + (.00085282 * temp * humid * humid) - (.00000199 * temp * temp * humid * humid);         }     }```
• 03-01-2009, 07:47 PM
Fubarable
The length of the headIndex array will all depend on your requirements. You have some arrays with 12 members (one for each month) and some with 13 (each month plus a spot for the average). You have to decide how many items this array will need in order to solve your problem.
• 03-02-2009, 12:44 AM
ks1615
Well I fixed the String and the two doubles, was missing 1
They are all evened out now
But here is the current code
How would I handle the length of both humid and temp into the heat index?
Here is the current equation for the heat index

Code:

``` String [] month = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.", "Avg."};       double [] temp = {70.3, 70.8, 73.8, 77.0, 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0, 78.1};       double [] humid = {69, 67, 66, 64, 66, 69, 67, 67, 70, 69, 69, 70, 68};       double [] heatIndex = new double [];```
Code:

`double heatIndex = -42.379 + (2.04901523 * temp) + (10.14333127 * humid) - (0.22475541 * temp * humid) - (.00683783 * temp * temp) - (.05481717 * humid * humid) + (.00122874 * temp * temp * humid) + (.00085282 * temp * humid * humid) - (.00000199 * temp * temp * humid * humid);`