Question: Consider the following code segment and method makeSquare. int [ ] list = { 1 1 , 1 2 , 1 3 , 1 4

Consider the following code segment and method makeSquare.
int[] list ={11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
int[][] square = makeSquare(list);
/** Description: Method makeSquare stores all the values from the 1D
* list array parameter into the smallest possible,
* square-shaped, 2D array.
*
* Precondition: list is a non-empty array.
* Postcondition: 1: Smallest possible square 2D array is returned.
*2: All values from list are copied into the 2d array.
*3: 99 is stored into any leftover 2d spaces.
*/
public static int[][] makeSquare(int[] list)
{
int n =(int) Math.sqrt(list.length)+1;
int[][] temp = new int[n][n];
int count =0;
for (int r =0; r < n; r++)
for (int c =0; c < n; c++)
{
if(count < list.length)
{
temp[r][c]= list[count];
count++;
}
else
temp[r][c]=99;
}
return temp;
}
Which of the three Postconditions is satisfied by the makeSquare method?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!