Question: I'm having trouble with the below Java programming problem: Purpose This lab will introduce you to working with multi-dimensional arrays and the enhanced for loop.
I'm having trouble with the below Java programming problem:
Purpose
This lab will introduce you to working with multi-dimensional arrays and the enhanced for loop. We'll be creating a function signature that will return a multi-dimensional array taking a one-dimensional array as its argument.
Options
To test your function, you can either call print (or println) to output the array contents and verify them manually, or you can compare the array contents in a unit test.
Task
For this lab, create code that will generate a histogram. A histogram is a type of structure for displaying data for the visual analysis of the data. You should pass in a one-dimensional array to a function that you create and return a two-dimensional array.
For your output (or test comparison) use the enhanced for (foreach) loop rather than the for loop. Note that if you are using the unit testing approach, you'll need to keep track of the indexes from the "expected" array as you loop through for your comparison.
Example
Pass in an array {2,4,5} and return the array {{2,2},{4,4,4,4},{5,5,5,5,5}}. So, whatever the number value v is in the input, repeat v, v times.
My code :
public class Histogram {
public static int [][] getArray(int[] a) {
int [][] b = new int[a.length][a.length];
int i, j;
for(i = 0; i < a.length; i++)
{
for(j = 0; j < a[i]; j++) {
}
}
return b;
}
public static void main(String[] args)
{
int [] a = {2,4,5};
getArray(a);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
