Question

Objectives: In this lab, you need to modify your functions (createArray, getArraySize, and freeArray) based your...

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

Homework Answers

Answer #1

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 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
Be sure to create the algorithms (pseudocode or flowcharts) for all 4 functions plus the main...
Be sure to create the algorithms (pseudocode or flowcharts) for all 4 functions plus the main program. Create a very simple larger( ) function that takes 2 positive numbers as parameters and returns the larger value of the two numbers.    Use your larger function to help create the following functions -- reuse your function code. Create a simple larger3( ) function that takes 3 positive numbers as parameters and returns the largest value of the three numbers.   Create a simple...
Question Recursive Maximum: In this question you must develop and use a recurive function to find...
Question Recursive Maximum: In this question you must develop and use a recurive function to find the maximum value in array of integers. Complete the C program, Maximum.c, by implementing the recursive function maximumValue, and the regular function max, and completing the main function using the comments provided. Open the file Maximum.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. Note that the function max is not recursive....
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
C# Lab 7A: Thank you, Grace Hopper. You may have heard the story, but the programming...
C# Lab 7A: Thank you, Grace Hopper. You may have heard the story, but the programming term “bug” is attributed to the computer legend Grace Hopper who, after diagnosing the problem with a program, determined it was because a bug (in this case, a moth) physically wedged itself under a vacuum tube – causing the machine to no longer work. We’ve come a long way since then... For this part of the lab, you are going to write a program,...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to create a data type SimpleCalculator capable of performing arithmetic operations. • Creating const member functions to enforce the principle of least privilege. The follow-up questions and activities also will give you practice: • Using constructors to specify initial values for data members of a programmer-defined class. Description of the Problem Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT