LearningJava101
LearningJava101

Reputation: 61

Sqrt, and Math in Arrays

I'm having difficulty understand how to write this array. I need it to out-print 10x5 (50 elements total), and have the first 25 elements equal to the sqrt of the index that it is in, and the last 25 to equal 3 * the index. Yes, this is homework but I'm not asking for you to do it for me, I just need help! I'm getting errors when using Math saying that I cant use double and the double array together. Here is what I have so far:

public class snhu4 {
    public static void main(String args[]) {
        double alpha[][] = new double[10][5];
        double[] sum, sum2;

        for (int count=0; count<=25;count++) {
            alpha[count]= Math.sqrt(count);
        }

        for (int count=26; count<=50;count++) {
            alpha[count]= count *3;
        }

        for (int count=0; count<=50;count++) {
            System.out.print(alpha[count]);
        }
    }
}

Upvotes: 0

Views: 319

Answers (3)

DwB
DwB

Reputation: 38300

The 10x5 appears to be an output constraint, not a design constraint.

You are using Java, so use Java constructs, not C-language constructs; specifically store the values in a List not an array.

Here are some hints:

  1. List<Integer> valuesList = new ArrayList<Integer>();
  2. for (int index = 0; index < 25; ++index)
  3. Integer currentValue = Math.sqrt(index);
  4. valuesList.add(currentValue);
  5. for (int index = 25; index < 50; ++index)
  6. Integer currentValue = index * 3;
  7. valuesList.add(currentValue)
  8. int count = 1;
  9. for (Integer current : valuesList)
  10. if ((count % 5) == 0) // write a newline.
  11. System.out.print(current);
  12. ++count

Upvotes: 0

ThaBomb
ThaBomb

Reputation: 722

you defined alpha as a 2D array with lets say 10 items in the first dimension and 5 in the second, and 5x10 is 50 elements.

When using your array to assign values to these elements, u must call upon the array using 2 indices, one for each dimension:

alpha[i][j] = /*double value*/; //with 0<=i<=9 and 0<=j<=4

So the first 25 elements going from left to right in dimension order is going to be:
[0to9][0] and [0to9][1] and [0to4][2]
the next 25 will be
[4to9][2] and [0to9][3] and [0to9][4]

from then on i cannot give you the answers to your homework, but the loops should look like this:

int j;
for(int i = 0; i<25; i++)
{
    j=i/10; //integer division will return 0 for i<10, 1 for 10<i<20, etc..
    alpha[i%10][j] = Math.sqrt(i);
}

and you can figure out the rest

Upvotes: 0

imulsion
imulsion

Reputation: 9040

Because alpha is a multidimensional array, you can't refer to its elements like a normal array.

int myarray[][] = new int[2][2];

In the above example, the array myarray is multidimensional. If I wanted to access the second element in the first array, I would access it like this:

int myint = myarray[0][1];

You are trying to access a multidimensional array by using the access for a normal array. Change

alpha[count]

to

alpha[0][count]

or similar.

Read here for more information on multidimensional arrays.

Upvotes: 1

Related Questions