The problem presented to me:
Declare a two-dimensional array of ints in main. Write a method to read in the size of each dimension, allocate memory for the array and return it (DON'T DO THIS IN MAIN, BUT CALL THIS METHOD IN MAIN).
Write another method to initialize each element to the sum of its indices (for example, the element at 3, 4 would have the value 7) (DON'T DO THIS IN MAIN, BUT CALL THIS METHOD IN MAIN).
IF TIME, write a method to print the 2-dim. array (use enhanced for loop if you want). (DON'T DO THIS IN MAIN, BUT CALL THIS METHOD IN MAIN)
The output generated by my code:
This is the output which I think is what the above question was asking:
0 1 2 3 4 5 1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 4 5 6 7 8 9
I finished this program to print a 2D array. I got the right output, but I'm not sure if I did what was being asked in the problem below.
Can someone please look at the question below and tell me if I miss any part of the question? The part where I got confused was when it said to read in the size of each dimension:
package twoDarray;
import java.util.Scanner;
public class TwoDarray {
public static void main(String[] args) {
int[][] array= getArray();
init_array(array);
printArray(array);
}
public static int[][] getArray() {
int[][] array = new int[5][6];
return array;
}
public static int[][] init_array(int[][] array) {
for(int i = 0; i< array.length;i++)
for(int j =0; j< array[i].length;j++)
array[i][j]= i+j;
return array;
}
public static void printArray(int[][] array) {
for(int i = 0; i< array.length;i++)
for(int j =0; j< array[i].length;j++)
System.out.printf("%d\n",array[i][j]);
}
}