Can someone help me understand what is going on here and what to do? It\'s C++ L
ID: 3728119 • Letter: C
Question
Can someone help me understand what is going on here and what to do? It's C++
Lab5: Practice using struct and dynamic arrays 2 Author: 3 Last modifed on: 4Known bug: 1 5 Note: Please use assert to check for precondtition 7 #include 8 #include 9 using namespace std; 10 11 //Define a struct type, each struct type variable has three 12 // member variables array, capacity, and length. Together these 13 // three variables represent a partially filled array. 14 struct Array f 15 16 17 18 19 20 i 21 int array; // point to the dynamically allocated array int capacity; // the capacity of the array int length; // the number of elements in the array // the array is filled from 0th, 1st, // spots (length-1)-th 23 / Initialize the array structure with the given numbers. 24 the array's capacity will be twice the length of numbers array 25 Oparam array: the array struct to be initialized 26 @param numbers: the array of values to be stored in the array struct 27 @param numbers_len: how many numbers are there in the array numbers 28 precondition: "numbers" has been filled with "numbers_len" number of ints 29 post condition: a.length== numbers-len 30 31 32 / 33 void InitArray (Array & a, int numbers], int numbers len); 34 35 /* Displays the content of an int array, both the array and 36 the length of array will be passed as parameters to the function 37 Oparam array: gives the array to be displayed 38/ 39 void DisplayArray (const Array & a); 40 41 //Extra Credit Part: Implement this function, and test in main 42 /Read a sequence of int from input, and store them in the given Array 43variable; we overwrite any existing content of the Array variable 44 Oparam array: the Array variable that is used to store the numbers 45 Note: you should allow the user to enter any many numbers as they want, 46 the number sequence will be ended with a special value of -1 47 This means that you have to "grow" your array variable when needed 48 / 49 void InitArrayFromInput (Array & a); 50 51 / Merge the contents of two arrays into one 52 Oparam array1: the target array 53 @param array2: the source array 54 Oprecondition: array1 and array2 have been set up 55 @postcondition: arrayl.length=arrayl.length-array2.1ength; 56 57 58 Note: if arrayl's capacity is not enough to hold all numbers, 59 you need to allocate a new int array, and copy old data over to the 60 new array.. 61 / 62 void MergeArray (Array & a1, const Array & a2); 63 64 a. array [0]=numbers [9] , . … a. capacity = 2*numbers-len array1 contains its previous numbers, followed by numbers from array2Explanation / Answer
This program is implementation of generic array and operations on array using structures and dynamic memory allocation which improves the efficiency of operations on arrays.
Structure array defines properties that an array object will contain. C++ provides a way to declare arrays (line 68, 69). But let us assume that there are two arrays which we want to merger into one. This operation requuires extra information like number of elements in the arrays to be merged and capacity of the array in which all the elements are going to be kept. For this purpose the program proposes to use structure with these properties. Arrays are declared in defalt way and then they are intialized in structure format (line 33). Various ooperations are then conducted on these new format of array(line 39, 49, 62).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.