Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public static int[] make1DArray(int [][] anArray) : The method creates a one-dim

ID: 3689652 • Letter: P

Question

public static int[] make1DArray(int [][] anArray): The method creates a one-dimensional array out of a two-dimensional array. It does so by copying everything in a row-by-row fashion. See the sample output below for specifics.

The following shows a sample run in which the user types a non-positive value when prompted for one of the array values (after entering correct values for the rows and columns):

How many rows (> 0) should the array have? 1
How many columns (> 0) should the array have? 2
Enter a positive (> 0) integer value: 3
Enter a positive (> 0) integer value: -1
The array entry failed. The program will now halt.

Explanation / Answer

Java Method:

public static int[] make1DArray(int [][] anArray){
int[] arr=new int[(anArray.length)*(anArray[0].length)];
int count=0;
for(int i=0;i<anArray.length;i++){
for(int j=0;j<anArray[0].length;j++){
arr[count]=anArray[i][j];
count++;
}
}
return arr;
}