1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <assert.h>
using namespace std;
struct Array {
int* array; // point to the dynamically allocated array
int capacity; // the capacity of the array
int length; // the number of elements in the array
};
/* Initializes the array structure with the given numbers. The array's capacity can be set to double the length of numbers array.
@param array: the array struct to be initialized
@param numbers: the array of values to be stored in the array struct
@param numbers_len: how many numbers are there in the array numbers
precondition: "numbers" has been filled with "numbers_len" number of ints
post condition: array.length == numbers_len
array.array[0] == numbers[0],...,
array. capacity = 2*numbers_len
*/
void InitArray (Array & array, int numbers[], int numbers_len);
/* Displays the content of an int array, both the array and the length of array will be passed as parameters to the function
@param array: gives the array to be displayed
*/
void DisplayArray (const Array & array);
int main()
{
int numbers1[5] = {12, 23, 34, 56, 78};
int numbers2[11] = {89, 7, 14, 22, 98, 101, 112, 34, 11, 5, 99};
Array NumArray1, NumArray2;
InitArray (NumArray1, numbers1, 5);
InitArray (NumArray2, numbers2, 11);
cout << "***** NumArray1: \n";
DisplayArray(NumArray1);
cout << "***** NumArray2: \n";
DisplayArray(NumArray2);
}
void InitArray (Array & a, int numbers[], int numbers_len)
{
a.length = numbers_len;
a.capacity = 2*numbers_len;
for (int i=0; i < numbers_len; i++)
{
a.array = new int[numbers_len];
a.array[i] == numbers[i];
}
a.array[numbers_len-1] == numbers[numbers_len-1];
}
void DisplayArray (const Array & a)
{
for (int i=0; i < a.length; i++)
{
cout << a.array[i] << " ";
}
cout << endl;
|