Objectives:
In this lab, you need to modify your functions (createArray, getArraySize, and freeArray) based your pre-lab. In createArray function, an integer array needs to be created with its size and the maximum value in this array stored in front as two integers. After creating the array, your array should look like this:
max size Array[0] Array[1] ... Array[n-1]
You also need to modify the other two functions accordingly. Main program steps:
1.Create an array like mentioned above with 10 random integer numbers.
2.Obtain the size of the array using your “getArraySize” function.
3.Print out all the numbers in your array, the size of the array, and the maximum value in thisarray.
4 Free the created array using your “freeArray” function.
Example output:
Elements in array are: 6, 8, 6, 6, 3, 6, 8, 8, 4, 4,
Array size is 10, and the maximum value in array is 8
Solution for the above question are as follows -
Code :
public class ArrayDemo
{
// getArraySize - get the array size
static public int getArraySize(int[] a) {
return a.length;
}
// freeArray - free the array
static public int[] freeArray(int[] a) {
a = null;
return a;
}
public static void main(String[] args) {
// local variable declaration
int[] array = new int[]{
6,8,6,6,3,6,8,8,4,4 };
int max = 0;
int n = getArraySize(array);
System.out.print("Elements in array
are : ");
// print the elements of array and
find the max number
for (int i = 0; i<n ; i++ )
{
if (array[i] > max) {
max = array[i];
}
System.out.print(array[i] + "
");
}
System.out.println("\nArray size is
: " + n + " and the maximum value in array is " + max);
// free the array data
array = freeArray(array);
}
}
Code Screen Shot :
Output :
Get Answers For Free
Most questions answered within 1 hours.