LearningJava101
LearningJava101

Reputation: 61

Math and Arrays

I'm new to java and still learning. I am having one heck of a time trying to figure out how to create this program. I have tried multiple ways and spent about 4 hours now trying to get this to work and it still wont outprint what I need it to. I need a 10x5 array with the first 25 digits being the index variable squared and the last 25 digits being the index times 3. What it is outprinting is 5 numbers over and over 10 times. But it's like it is not reading the "next index variable". I get: 0.0, 1.0, 4.0, 9.0, 16.0, 0.0, 1.0, 2.0, 4.0, etc.. Here is what I have so far(please don't rate down, I'm trying hard to learn this!):

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

    for (int col=0; col<=9;col++) {
      for (int row=0; row<=4;row++) {
        alpha[col][row]= Math.pow(row,2);

        System.out.println(alpha[col][row]);
      }
    }
  }
}

Upvotes: 3

Views: 282

Answers (4)

Michael Yaworski
Michael Yaworski

Reputation: 13483

This is just a generalised version, compared to the accepted answer. It will give the same output, though.

 public static void main(String args[]) {

    double alpha[][] = new double [10][5];

    int index = 0;

    for (int row = 0; row < alpha.length; row++) 
    {
        for (int col = 0; col < alpha[row].length; col++) 
        {
            if (index < 25)
                alpha[row][col] = Math.pow(index, 2);
            else
                alpha[row][col] = index * 3;

            index++;

            System.out.println(alpha[row][col]);
        }
        System.out.println("" + '\n');
    }
}

Upvotes: 1

ioreskovic
ioreskovic

Reputation: 5699

I believe you're looking for something like this:

public static void main (String[] args) {
    double[][] alpha = new double[10][5];

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 5; j++) {
            int index = (5 * i + j);
            if (index < 25) {
                alpha[i][j] = (index * index);
                System.out.println("index(" + index + ")^2 =" + alpha[i][j]);
            } else {
                alpha[i][j] = (3 * index);
                System.out.println("3*index(" + index + ") = " + alpha[i][j]);
            }
        }
    }
}

Upvotes: 2

maxivis
maxivis

Reputation: 1787

You're always using the row variable to calculate the assignment value, I think you should combine the values of row and col (or maybe use an index as aetheria says), something like this:

    double alpha[][] = new double[10][5];
    for (int col = 0; col <= 9; col++) {
        for (int row = 0; row <= 4; row++) {
            if (col < 5) //if column >= 5 that means that you just passed the 25 index
                alpha[col][row] = Math.pow(row + (col*row), 2);
            else {
                alpha[col][row] = Math.pow(row + (col*row), 3);
            }
        }
    }

I couldn't test it (my eclipse is totally crushed) but I think that will give you the idea (probably you will need to play with the "row + (col*row)" part, when one of the variables is 0 it will not fit your needs). Hope this helps, regards.

Upvotes: 0

ᴇʟᴇvᴀтᴇ
ᴇʟᴇvᴀтᴇ

Reputation: 12751

You need an index variable initialized to zero outside of the loops and incremented (index++) inside the inner loop. Then you can perform the calculations on the index (0-49) rather than the row variable, which keeps looping 0-4. You'll also need a conditional (if statement) that performs one calculation if index is < 25 and a different calculation if index >= 25.

Upvotes: 1

Related Questions