float* dynamicArray(int numberOfNumbers) The function creats a dynamic array.whi
ID: 3655064 • Letter: F
Question
float* dynamicArray(int numberOfNumbers) The function creats a dynamic array.which contains float numbers. The parameter is the number of numbers which the dynamic array shall contain. Let the main function print all the numbers in the dynamic array.Explanation / Answer
public class DynamicArrayOfInt { private int[] data; // An array to hold the data. public DynamicArrayOfInt() { // Constructor. data = new int[1]; // Array will grow as necessary. } public int get(int position) { // Get the value from the specified position in the array. // Since all array positions are initially zero, when the // specified position lies outside the actual physical size // of the data array, a value of 0 is returned. if (position >= data.length) return 0; else return data[position]; } public void put(int position, int value) { // Store the value in the specified position in the array. // The data array will increase in size to include this // position, if necessary. if (position >= data.length) { // The specified position is outside the actual size of // the data array. Double the size, or if that still does // not include the specified position, set the new size // to 2*position. int newSize = 2 * data.length; if (position >= newSize) newSize = 2 * position; int[] newData = new int[newSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; // The following line is for demonstration purposes only. System.out.println("Size of dynamic array increased to " + newSize); } data[position] = value; } } // end class DynamicArrayOfInt
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.