Curtis
Curtis

Reputation: 1

Multi dimensional array with double and int

This is what i got so far

public static void main(String[] args)  {

  int[]aryNumbers = new int[20];

  double[]aryNumbers2 = new double [2];

  aryNumbers[0] = 10;
  aryNumbers[1] = 20;
  aryNumbers[2] = 30;
  aryNumbers[3] = 40;
  aryNumbers[4] = 50;

  aryNumbers2[2] = 6.2;
  aryNumbers2[2] = 6.3;
  aryNumbers2[2] = 6.4;
  aryNumbers2[2] = 6.5;
  aryNumbers2[2] = 6.6;

  int rows = 20;
  double columns = 2;
  int i;
  double j;


  for (i=0; i < rows ; i++) {

      for(j=0; j < columns ; j++) {
          System.out.print( aryNumbers[i] + " " );
          System.out.print( aryNumbers2[j] + " " ); 

      }
      System.out.println( "" );

  }

The program is supposed to list up like this

KM = Miles 10 6.2 15 9.3 20 12.4

showing what Km is in miles up till 100 km. Any help is greatly appreciated been stuck on this for hours.

Upvotes: 0

Views: 85

Answers (2)

qcGold
qcGold

Reputation: 200

First of all you have no multi dimensional array in your code right now.

What you could use is simply a single array called miles. You insert in this array the values of the miles and you compare it to the index of the array. Make sure you understand the difference between the element contained in the array and it's index value.

As an exemple if you use the formula for km to miles conversion; mi = km * 0.62137 you could easily fill an array go get what you need.

The following code should achieve this:

public static void main(String[] args)  {

    double[] miles = new double[101];

    for (int i = 0; i < miles.length ; i++) {
        miles[i] = i * 0.62137;

        System.out.println(i + " km = " + miles[i] + " miles");
    }
}

Note that I use the double primitive for the array since it will be much more precise using floating points than integers where you would loose precision. But if you want to round before the comma, change the array declaration to int[].

Note2: Whan you declare an array, the number 101 represents the numbers of index (length of array) that you want to create. Then you are able to loop over miles.length in order to fill those empty spots.

Edit

If you only need one number after the comma for the miles value use this line instead in your for loop: miles[i] = Math.floor((i * 0.62137) * 10) / 10;

And would give you the output of:

0 km = 0.0 miles
1 km = 0.6 miles
2 km = 1.2 miles
3 km = 1.8 miles
4 km = 2.4 miles
5 km = 3.1 miles
6 km = 3.7 miles
7 km = 4.3 miles
8 km = 4.9 miles
9 km = 5.5 miles
10 km = 6.2 miles
...

Hope this helps

Upvotes: 0

aleju
aleju

Reputation: 2386

This code generates your expected output:

package stackoverflowtmp;

public class StackoverflowTmp {
    public static void main(String[] args) {
        int[] kilometers = new int[3];
        double[] miles = new double[3];

        kilometers[0] = 10;
        kilometers[1] = 15;
        kilometers[2] = 20;

        miles[0] = 6.2;
        miles[1] = 9.3;
        miles[2] = 12.4;

        System.out.print("KM = Miles ");
        for (int i=0; i < kilometers.length; i++) {
            System.out.print(kilometers[i] + " ");
            System.out.print(miles[i] + " "); 
        }
    }
}

Outputs:
KM = Miles 10 6.2 15 9.3 20 12.4


However, I would suggest you to create a function to calculate the number of miles for a given number of kilometers according to km = 1.609344*miles or vice versa miles = 0.621371192*km instead of using predefined values.

Example code for that:

package stackoverflowtmp;

public class StackoverflowTmp {
    public static void main(String[] args) {
        System.out.print("KM => Miles: ");
        for (int i = 0; i < 50; i += 10) {
            System.out.print(i + " => " + kmToMiles((double)i) + ", ");
        }
    }

    public static double kmToMiles(double km) {
        return km * 0.621371192;
    }
}

Outputs:
KM => Miles: 0 => 0.0, 10 => 6.21371192, 20 => 12.42742384, 30 => 18.64113576, 40 => 24.85484768,

Upvotes: 1

Related Questions