Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I have an error in my code. How can I fix it? Thanks: Header: #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class

Hi I have an error in my code. How can I fix it? Thanks:

image text in transcribed

Header:

#ifndef DynamicArray_h

#define DynamicArray_h

#include

using namespace std;

template

class DynamicArray

{

V* values;

int cap;

V dummy;

public:

DynamicArray(int = 2);

DynamicArray(const DynamicArray&);

~DynamicArray() { delete[] values; }

int capacity() const { return cap; }

void capacity(int);

V operator[](int) const;

V& operator[](int);

DynamicArray& operator=(const DynamicArray&);

};

template

DynamicArray::DynamicArray(int cap)

{

this->cap = cap;

values = new V[cap];

for (int index = 0; index

values[index] = V();

}

}

template

V DynamicArray::operator[](int index) const

{

if (index = cap)

return V(); // a copy

return values[index]; // a copy

}

template

V& DynamicArray::operator[](int index)

{

if (index

return dummy; // a copy

}

else if (index >= cap) {

capacity(2 * index);

}

return values[index]; // a copy

}

template

void DynamicArray::capacity(int newCap) {

V* temp = new V[newCap];

// get the lesser of the new and old capacities

int limit = min(newCap, cap);

// copy the contents

for (int i = 0; i

temp[i] = values[i];

}

// set added values to their defaults

for (int i = limit; i

temp[i] = V();

}

// deallocate original array

delete[] values;

// switch newly allocated array into the object

values = temp;

// update the capacity

cap = newCap;

}

template

DynamicArray::DynamicArray(const DynamicArray& original)

{

cap = original.cap; // still copy

values = new V[cap]; // not copy, is new

for (int i = 0; i

values[i] = original.values[i];

}

}

template

DynamicArray& DynamicArray::operator=(const DynamicArray& original)

{

if (this != &original) //check if copy or not, better not be tho

{

// same as destructor

delete[] values;

// same as copy constructor

cap = original.cap;

values = new V[cap]; // not copy, is new

for (int i = 0; i

values[i] = original.values[i];

}

}

return *this; // return self reference

}

#endif

Code:

#include

#include

using namespace std;

#include

#include "DynamicArray.h"

int main()

{

DynamicArray a(100);

cout

cout

for (int i = 0; i

assert(a[i] == 0);

}

cout

a[1] = 123;

a[2] = 546;

cout

cout

assert(123 == a[1]);

cout

cout

assert(546 == a[2]);

a[-1000] = 222;

cout

cout

assert(123 == a[1]);

assert(546 == a[2]);

assert(222 == a[-3]);

assert(0 == a[100]); // should increase array capacity here and make new values 0 (for int)

assert(222 != a[11]);

assert(222 != a[0]);

cout

const DynamicArray b = a;

for (int i = 0; i

assert(a[i] == b[i]);

}

cout

const DynamicArray c;

assert(c.capacity());

assert(c[0] == c[0]);

cout

DynamicArray d(a); // making a copy, c = a would have worked too

assert(a.capacity() == d.capacity());

for (int i = 0; i

assert(a[i] == d[i]); // uses the setter version for both a and b

}

// object assignment test

cout

DynamicArray e;

e = a;

assert(a.capacity() == e.capacity());

for (int i = 0; i

assert(a[i] == e[i]);

////////////////////////////////////////////////////////////////////////////////////////////////////////////

//testing for char

cout

DynamicArray ch(10);

cout

cout

for (int i = 0; i

assert(ch[i] = 'A');

}

cout

ch[1] = 'A';

ch[2] = 'B';

cout

cout

assert('B' == ch[2]);

ch[-1000] = 'Z';

cout

cout

assert('A' == ch[1]);

assert('B' == ch[2]);

cout

const DynamicArray ch2 = ch;

for (int i = 0; i

assert(ch[i] == ch2[i]);

}

cout

const DynamicArray ch3;

assert(ch3.capacity());

cout

DynamicArray ch4(ch); // making a copy, ch4 = ch would have worked too

assert(ch.capacity() == ch4.capacity());

for (int i = 0; i

assert(ch[i] == ch4[i]); // uses the setter version for both a and b

}

// object assignment test

cout

DynamicArray ch5;

ch5 = ch;

assert(ch.capacity() == ch5.capacity());

for (int i = 0; i

assert(ch[i] == ch5[i]);

////////////////////////////////////////////////////////////////////////////////////////////////////////////

//testing for float

cout

DynamicArray f1(10);

cout

cout

for (int i = 0; i

{

assert(f1[i] = 10.5);

}

cout

f1[1] = 12.5;

f1[2] = 20.5;

cout

cout

assert(20.5 == f1[2]);

f1[-1000] = 100.5;

cout

cout

assert(12.5 == f1[1]);

assert(20.5 == f1[2]);

cout

const DynamicArray f2 = f1;

for (int i = 0; i

assert(f1[i] == f2[i]);

}

cout

const DynamicArray f3;

assert(f3.capacity());

cout

DynamicArray f4(f1); // making a copy, ch4 = ch would have worked too

assert(f1.capacity() == f4.capacity());

for (int i = 0; i

assert(f1[i] == f4[i]); // uses the setter version for both a and b

}

// object assignment test

cout

DynamicArray f5;

f5 = f1;

assert(f1.capacity() == f5.capacity());

for (int i = 0; i

assert(f1[i] == f5[i]);

return 0;

}

Microsoft Visual C+Runtime Library Debug Error! Program: ..al studio 2015 Projects Assignment 05 DebugAssignment 05.exe abort) has been calle (Press Retry to debug the application) Abort Retry gnore

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

Different types of Grading?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago

Question

HOW MANY TOTAL WORLD WAR?

Answered: 1 week ago

Question

Discuss the scope of financial management.

Answered: 1 week ago