Question

1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

1. Your project will include the following three files:

A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section.

An implementation file: dynamicArray.cpp that implements the functions declared in the header file.

A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above.

2. The header file dynamicArray.h will include the following list of functions:

constructing a dynamic array of the specified size and initializing the i-th array element to i*i

   int * array_constructor(int *  &intPtr, int &size );


Specifically, the above function allocates space to intPtr and uses it to manage a dynamic array of size integers. Use proper exception handling to make sure that the allocation is successful. Then initialize the value of the i-th element to i*i. The function returns a pointer pointing to the new array. To avoid memory leakage, you should check whether intPtr already has a valid pointee when being passed into this function. If the answer is yes, you would want to first deallocate the space occupied by its pointee.

As an example, after invoking this function array_constructor( myArray, size=5); in a different function, the content of myArray will be

myArray[0]=0
myArray[1]=1
myArray[2]=4
myArray[3]=9
myArray[4]=16

resizing a dynamic array pointed to by intPtr, where the new size can be smaller or larger than the array's current size

int * array_resize(int * &intPtr, int& currSize, int& newSize);

You will need to first make sure both curSize and newSize have valid values, i.e., positive integers. Then consider the following three scenarios:

newSize=0: deallocate any space pointed to by intPtr

currSize==newSize or newSize<0: do nothing

currSize>newSize: the array's size is reduced to newSize. Furthermore, its content is reduced to its first newSize elements.

currSize<newSize: the array's size is increased to newSize. The content of the array will be expanded by inserting at the end as many elements as needed to reach newSize. Furthermore, initialize each of the new elements to i*i, where i is the index of the element.

As an example, after invoking this function array_resize( myArray, currSize, newSize); (where currSize=5, newSize=9), the content of myArray will be changed to

myArray[0]=0
myArray[1]=1
myArray[2]=4
myArray[3]=9
myArray[4]=16
myArray[5]=25
myArray[6]=36
myArray[7]=49
myArray[8]=64

Later, another invocation array_resize( myArray, currSize, newSize);(where currSize=9, newSize=2) will change the content of myArray to:

myArray[0]=0
myArray[1]=1

deallocating the memory space occupied by the dynamic array intPtr. Please make sure you check whether this array actually exists. After you finish deallocating the array space, make sure to assign nullptr to the pointer.

void array_destructor(int * &intPtr);

Randomizing the content of the dynamic array intPtr by calling the srand() and rand() functions (see zyBook section 2.19).

void array_set(int* &intPtr, int &size);

Specifically, after having set a seed value using the srand( time(0) ) function. Then invoke the rand() to assign each element in the array a random value.

As an example, after invoking array_set( myArray, currSize); (where currSize=9) in a different function, myArray will look like (yours will be different, of course):

myArray[0]=415960052
myArray[1]=981322979
myArray[2]=420899093
myArray[3]=239922833
myArray[4]=1556248812
myArray[5]=1670446471
myArray[6]=1140120866
myArray[7]=14812681
myArray[8]=1996110162

Passing a pointer to functions to a sorting function mysort() so that this function can either sort an array in ascending or descending order. Please modify the insertionSort() you implemented in the last coding lab to sort an integer array. (Please feel free to use the implementation in the sample solution posted on iLearn if yours didn't pass the test.)

void mysort( int* &intPtr, int size, bool (* comp)(int&, int&) ); 

To do this, please include the following two boolean functions for integer comparison in your header file:

bool my_less_equal(int& x, int & y);   //return true if x<=y, false otherwise.
bool my_greater_equal(int& x, int & y ); //return true if x>=y, false otherwise.

Now, if one calls mysort( myArray, size, my_less_equal); in another function, the content in myArray will be sorted in ascending order; calling mysort( myArray, size, my_greater_equal); will sort myArray in descending order.

3. The implementation file dynamicArray.cpp will implement all the functions declared in the above dynamicArray.h header file.

4. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've declared and implemented in the above two files.

IDE IS BAREBONES, very simple, the final test will test each function at each boundary (specified in details) . Will test each function

IDE will give the input values

this is my code for each file:

//dynamicArray.h

#include <iostream>
using namespace std;


int* array_constructor(int *intPtr, int &size);
int * array_resize(int *intPtr, int& currSize, int& newSize);
void array_destructor(int *intPtr);
void array_set(int *intPtr, int &size);
void mysort(int *intPtr, int size, bool(*comp)(int&, int&));
bool my_less_equal(int &x, int &y); //true if x>=y
bool my_greater_equal(int &x, int &y); //true if y>=x

//dynamicArray-main.cpp

#include"dynamicArray.h"

int main()
{
int *ptr1=NULL;
int size1=9;//invalid size -3 is assigned
ptr1=array_constructor(ptr1,size1);//- Testing the array_constructor() function with an invalid array size.
size1=9; //valid size 13 assigned
ptr1=array_constructor(ptr1,size1);//-Testing the array_constructor() function with a valid array size.
  
//print array after valid size

for (int i = 0; i <13; i++)
{
cout << ptr1[i] <<endl;
}

int newSize=2;//set invalid size
ptr1=array_resize(ptr1,size1,newSize);//-Testing array_resize() to reduce the array's size to a negative number.

//set new size is smaller than old size
size1=9;
newSize=2;
ptr1=array_constructor(ptr1,size1);//16 size array constructor
for (int i = 0; i <16; i++)
{
cout << ptr1[i] <<endl;
}
ptr1=array_resize(ptr1,size1,newSize);//-Testing array_resize() to reduce the size of an array.
  
//print array after valid resize
  
for (int i = 0; i <newSize; i++)
{
cout << ptr1[i] <<endl;
}
  
  
size1=newSize;//-Testing array_resize() to increase the size of an array.
newSize=12;//increased from 5 to 12
ptr1=array_resize(ptr1,size1,newSize);//-Testing array_resize() to increase the size of an array.
//print array after valid resize
  
for (int i = 0; i <newSize; i++)
{
cout << ptr1[i] <<endl;
}
  
size1=100;
ptr1=array_constructor(ptr1,size1);//invoking array size 100
array_set(ptr1,size1);//Invoking array_set() to randomize the content...
  
//print array after random elements

for (int i = 0; i <size1; i++)
{
cout << ptr1[i] <<endl;
}
  
//-Testing mysort() to sort a randomized array in descending order by passing your
mysort(ptr1,size1,my_greater_equal);

for (int i = 0; i <size1; i++)
{
cout << ptr1[i] <<endl;
}
  
size1=212;
ptr1=array_constructor(ptr1,size1); //--Invoking array_constructor() to allocate an array of size 212...
array_set(ptr1,size1);//set random content
mysort(ptr1,size1,my_less_equal);//Invoking mysort() to sort the array in ascending order

for (int i = 0; i <size1; i++)
{
cout << ptr1[i] <<endl;
}
  
  
  
return 0;
}

//dynamicArray.cpp

#include"dynamicArray.h"
#include<exception>
#include<ctime>

int * array_constructor(int *intPtr, int &size)
{
try
{
if (size < 0)
throw "Bad Alloc: " ;
intPtr = new int[size];
for(int i=0;i<size;i++) //repeat loop for setting content
intPtr[i]=i*i;//update square of index
}
catch (exception e)
{
cout << e.what() << endl;
}
return intPtr;
}

int *array_resize(int *intPtr, int& currSize, int& newSize)
{
int *newArr;
newArr = new int[newSize];
if (currSize <= newSize)
{
for (int i = 0; i < currSize; i++)
{
newArr[i] = intPtr[i];
}
for(int i=currSize;i<newSize;i++) //here update next part of new size
newArr[i]=i*i;//update is multiplied index
  
}
else
{
for (int i = 0; i < newSize; i++)
{
newArr[i] = intPtr[i];
}
}
array_destructor(intPtr);
return newArr;
}

void array_destructor(int *intPtr)
{
delete[]intPtr;
intPtr = NULL;
}

void array_set(int *intPtr, int &size)
{
srand(time(0));
for (int i = 0; i < size; i++)
{
intPtr[i] = rand();
}
}

void mysort(int *intPtr, int size, bool(*comp)(int &x, int&y))
{
int tmp;
for (int i = 0; i < size; i++)
{
for (int j = i; j < size - i - 1; j++)
{
if ((*comp) == my_less_equal )
{
if (intPtr[j] > intPtr[j + 1])
{
tmp = intPtr[j];
intPtr[j] = intPtr[j + 1];
intPtr[j + 1] = tmp;
}
}
if ((*comp) == my_greater_equal)
{
if (intPtr[j] < intPtr[j + 1])
{
tmp = intPtr[j];
intPtr[j] = intPtr[j + 1];
intPtr[j + 1] = tmp;
}
}
}
}
}

bool my_less_equal(int &x, int &y) //return true if x<=y
{
if (x <= y)
return true;
else
return false;
}

bool my_greater_equal(int &x, int &y) //return true if x>=y
{
if (x >= y)
return true;
else
return false;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//dynamicArray.h

#include <iostream>
using namespace std;


int* array_constructor(int *intPtr, int &size);
int * array_resize(int *intPtr, int& currSize, int& newSize);
void array_destructor(int *intPtr);
void array_set(int *intPtr, int &size);
void mysort(int *intPtr, int size, bool(*comp)(int&, int&));
bool my_less_equal(int &x, int &y); //true if x>=y
bool my_greater_equal(int &x, int &y); //true if y>=x

//end of dynamicArray.h

// dynamicArray.cpp

#include"dynamicArray.h"

#include<exception>

#include<ctime>

#include<stdlib.h>

int * array_constructor(int *intPtr, int &size)

{

          intPtr = NULL;

          try

          {

                    if (size < 0)

                    {

                              throw size;

                    }else{

                              intPtr = new int[size];

                              for(int i=0;i<size;i++) //repeat loop for setting content

                                        intPtr[i]=i*i;//update square of index

                    }

                    }catch (...)

                    {

                              cout<<"Bad Allocation"<<endl;

                    }

return intPtr;

}

int *array_resize(int *intPtr, int& currSize, int& newSize)

{

          if(currSize >= 0 && newSize >=0)

          {

                    if(newSize == 0)

                    {

                              array_destructor(intPtr);

                    }else if(currSize > newSize)

                    {

                              if(intPtr != NULL)

                              {

                                        int * newIntPtr = new int[newSize];

                                        for(int i=0;i<newSize;i++)

                                                  newIntPtr[i] = intPtr[i];

                                        delete[] intPtr;

                                        intPtr = newIntPtr;

                              }else

                              {

                                        intPtr = new int[newSize];

                                        for(int i=0;i<newSize;i++)

                                                  intPtr[i] = i*i;

                              }

                    }else if(currSize < newSize)

                    {

                              if(intPtr != NULL)

                              {

                                        int * newIntPtr = new int[newSize];

                                        for(int i=0;i<currSize;i++)

                                                  newIntPtr[i] = intPtr[i];

                                        delete[] intPtr;

                                        intPtr = newIntPtr;

                                        for(int i=currSize;i<newSize;i++)

                                                  intPtr[i] = i*i;

                              }else

                              {

                                        intPtr = new int[newSize];

                                        for(int i=0;i<newSize;i++)

                                                  intPtr[i] = i*i;

                              }

                    }

          }

          return intPtr;

}

void array_destructor(int *intPtr)

{

          if(intPtr != NULL)

          {         delete[]intPtr;

                    intPtr = NULL;

          }

}

void array_set(int *intPtr, int &size)

{

          srand(time(0));

          for (int i = 0; i < size; i++)

          {

                    intPtr[i] = rand();

          }

}

void mysort(int *intPtr, int size, bool(*comp)(int &x, int&y))

{

          int tmp;

          for (int i = 0; i < size; i++)

          {

                    for (int j = 0; j < size - i - 1; j++)

                    {

                              if ((*comp) == my_less_equal )

                              {

                                        if (intPtr[j] > intPtr[j + 1])

                                        {

                                                  tmp = intPtr[j];

                                                  intPtr[j] = intPtr[j + 1];

                                                  intPtr[j + 1] = tmp;

                                        }

                              }

                              else if ((*comp) == my_greater_equal)

                              {

                                        if (intPtr[j] < intPtr[j + 1])

                                        {

                                                  tmp = intPtr[j];

                                                  intPtr[j] = intPtr[j + 1];

                                                  intPtr[j + 1] = tmp;

                                        }

                              }

                    }

          }

}

bool my_less_equal(int &x, int &y) //return true if x<=y

{

          if (x <= y)

                    return true;

          else

                    return false;

}

bool my_greater_equal(int &x, int &y) //return true if x>=y

{

          if (x >= y)

                    return true;

          else

                    return false;

}

//end of dynamicArray.cpp

// dynamicArray_main.cpp

#include"dynamicArray.h"

int main()

{

          int *ptr1=NULL;

          int size1=-3;//invalid size -3 is assigned

          ptr1=array_constructor(ptr1,size1);//- Testing the array_constructor() function with an invalid array size.

          size1=13; //valid size 13 assigned

          ptr1=array_constructor(ptr1,size1);//-Testing the array_constructor() function with a valid array size

          //print array after valid size

          for (int i = 0; i <13; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          cout<<endl;

          int newSize=-2;//set invalid size

          ptr1=array_resize(ptr1,size1,newSize);//-Testing array_resize() to reduce the array's size to a negative number.

          //set new size is smaller than old size

          size1=16;

          newSize=2;

          ptr1=array_constructor(ptr1,size1);//16 size array constructor

          for (int i = 0; i <16; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          cout<<endl;

          ptr1=array_resize(ptr1,size1,newSize);//-Testing array_resize() to reduce the size of an array.

          //print array after valid resize

          for (int i = 0; i <newSize; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          cout<<endl;

          size1=newSize;//-Testing array_resize() to increase the size of an array.

          newSize=12;//increased from 5 to 12

          ptr1=array_resize(ptr1,size1,newSize);//-Testing array_resize() to increase the size of an array.

          //print array after valid resize

          for (int i = 0; i <newSize; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          cout<<endl;

          size1=100;

          ptr1=array_constructor(ptr1,size1);//invoking array size 100

          array_set(ptr1,size1);//Invoking array_set() to randomize the content...

          //print array after random elements

          for (int i = 0; i <size1; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          cout<<endl;

          //-Testing mysort() to sort a randomized array in descending order by passing your

          mysort(ptr1,size1,my_greater_equal);

          for (int i = 0; i <size1; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          cout<<endl;

          size1=212;

          ptr1=array_constructor(ptr1,size1); //--Invoking array_constructor() to allocate an array of size 212...

          array_set(ptr1,size1);//set random content

          mysort(ptr1,size1,my_less_equal);//Invoking mysort() to sort the array in ascending order

          for (int i = 0; i <size1; i++)

          {

                    cout << ptr1[i] <<endl;

          }

          return 0;

}

//end of dyanmicArray_main.cpp

Output:

Bad Allocation 4 16 25 36 49 64. 81 100 121 144 4 16 25 36 49 64. 81 100 121 144 169 196 225

Add a comment
Know the answer?
Add Answer to:
1. Your project will include the following three files: A header file: dynamicArray.h that includes a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int...

    #include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int basee,int powerr) { ptr[powerr].power=powerr; ptr[powerr].base=basee; } void subtract(node ptr1[],int size1,node ptr2[],int size2,node ptr3[]) { for(int j=0;j<=size1;j++) { if(ptr1[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr1[j].base); ptr3[j].power=ptr2[j].power; } } for(int j=0;j<=size2;j++) { if(ptr2[j].base!=0) { ptr3[j].base=(ptr3[j].base)-(ptr2[j].base); ptr3[j].power=ptr2[j].power; } } } void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[]) { for(int j=0;j<=size1;j++) { if(ptr1[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr1[j].base); ptr3[j].power=ptr2[j].power; } } for(int j=0;j<=size2;j++) { if(ptr2[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr2[j].base); ptr3[j].power=ptr2[j].power; } } } void display(node ptr[],int size)...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • #include <iostream> using namespace std; struct node { int base; int power; }; void insert(node ptr[],int...

    #include <iostream> using namespace std; struct node { int base; int power; }; void insert(node ptr[],int basee,int powerr) { ptr[powerr].power=powerr; ptr[powerr].base=basee; } void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[],int size3) { for(int j=0;j<=size1;j++) { ptr3[j].base=ptr3[j].base+ptr1[j].base; } for(int j=0;j<=size2;j++) { ptr3[j].base=ptr3[j].base+ptr2[j].base; } } void display(node ptr[],int size) { if(ptr[0].base!=0) cout<<ptr[0].base<<"+"; for(int i=1; i<=size; i++) { if(ptr[i].base!=0) cout<<ptr[i].base<<"x^"<<ptr[i].power<<"+"; } } int main() { bool choice1=true; bool choice2=true; int size1,size2,base1,base2,power1,power2; cout<<"enter the max power in polynominal 1"; cin>>size1; node *a= new node[size1+1]; for(int...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • 9. At your job, you are creating a library. A co-worker brought this test code to...

    9. At your job, you are creating a library. A co-worker brought this test code to you. They expect that the output would be "12 12 12 12 12". However, they are getting "Empty List" (a) Describe why the error is occurring. (b) Explain how to fix the code. #include <iostream> using namespace std; CON void increaseArray (int* array, int size, int value) { int newSize = size + 5; if (size ==0) { size = 5; O int* newArray...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT