Question

For some reason I followed the steps in my project and I am getting the incorrect...

For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. 

Printing empty array -- next line should be blank


Testing append: Shouldn't crash!

Should print 100 through 110 below, with 110 on a new line:
100
101 102 103 104 105 106 107 108 109
110

Checking capacity of new array:
OK

Append test #2:

Should print 100 through 120 below, on 3 separate lines:
100
101 102 103 104 105 106 107 108 109
110 111 112 113 114 115 116 117 118
119 120

Checking capacity of new array:
OK

Testing at:
Should print 109 103 105 100 118
109 103 105 100 118
Should print your chosen value and not crash:
-1111

Testing sum: Should print 2310
2310

Testing remove. If you don't see 'Uh-oh,' that's a good sign.

Checking capacity of new array:
OK

Eliminating elements, one by one, from array, and testing resize.
Resize error! Removed 105, capacity is 26, should be 25
Resize error! Removed 106, capacity is 26, should be 25
Resize error! Removed 107, capacity is 26, should be 25
Resize error! Removed 108, capacity is 21, should be 20
Resize error! Removed 109, capacity is 21, should be 20
Resize error! Removed 110, capacity is 21, should be 20
Resize error! Removed 111, capacity is 17, should be 25
Resize error! Removed 112, capacity is 17, should be 25
Resize error! Removed 113, capacity is 14, should be 25
Resize error! Removed 114, capacity is 12, should be 20
Resize error! Removed 115, capacity is 10, should be 20
Resize error! Removed 116, capacity is 8, should be 20
Resize error! Removed 117, capacity is 7, should be 16
Resize error! Removed 118, capacity is 6, should be 16
Resize error! Removed 119, capacity is 5, should be 12
Resize error! Removed 120, capacity is 4, should be 12

Remove and resize test passed if no errors reported above.

Adding 152 to array. Final print should print just 152.
152


Rather than

Main.cpp

#include <iostream>
#include "dynamicarray.h"

using namespace std;

int main( ) {
DynamicArray a;

cout << "Printing empty array -- next line should be blank\n";
a.print();
// Loop to append 100 through 110 to a (11 elements total)
cout << "\nTesting append: Shouldn't crash!\n";
// a.append(100);
for (int i = 100; i < 111; i++) {
a.append(i);
}

cout << "\nShould print 100 through 110 below, with 110 on a new line:\n";
a.print();

cout << "\nChecking capacity of new array:\n";
if (a.cap() == 20) cout << "OK\n";
else cout << "Error: cap is " << a.cap() << ", should be 20\n";

// Loop to append 111 through 120 (21 elements total):
cout << "\nAppend test #2:\n";

for (int i = 111; i < 121; i++) {
a.append(i);
}
cout << "\nShould print 100 through 120 below, on 3 separate lines:\n";
a.print();


cout << "\nChecking capacity of new array:\n";
if (a.cap() == 40) cout << "OK\n";
else cout << "Error: cap is " << a.cap() << ", should be 40\n";


//declaration of indices
int indices[] = { 9, 3, 5, 0, 18 };
//Loop to access some arbitrary elements of a
cout << "\nTesting at:\n";
cout << "Should print 109 103 105 100 118\n";

for (int i = 0; i < 5; i++) {
cout << a.at(indices[i]) << " ";
}
cout << endl;
// Save me from myself
cout << "Should print your chosen value and not crash:\n";
cout << a.at(10000000) << endl;

// Print sum
cout << "\nTesting sum: Should print 2310\n";
cout << a.sum() << endl;

// Test remove:
// // Remove (a) 102
// // (b) 122 (should return false)
cout << "\nTesting remove. If you don't see 'Uh-oh,' that's a good sign.\n";
if (!a.remove(102)) cout << "Uh-oh, can't remove 102\n";
if (a.remove(122)) cout << "Uh-oh, shouldn't be able to remove 111\n";

// More tests: first, add 102 back in

a.append(102);
cout << "\nChecking capacity of new array:\n";
if (a.cap() == 40) cout << "OK\n";
else cout << "Error: cap is " << a.cap() << ", should be 40\n";

cout << "\nEliminating elements, one by one, from array, and testing resize.\n";
int capacities[] = {40, // len is 20
32, 32, 32, 32, // len is 19, 18, 17, 16
25, 25, 25, // len is 15, 14, 13
20, 20, 20, // len is 12, 11, 10,

25, 25, 25, // len is 15, 14, 13
20, 20, 20, // len is 12, 11, 10,
16, 16, 12, 12, 10, // len is 9, 8, 7, 6, 5
10, 10, 10, 10, 10 // 4, 3, 2, 1, 0
};
for (int i = 0; i < 21; i++) {
a.remove(100 + i);
if (a.cap() != capacities[i]) {
cout << "Resize error! Removed " << 100 + i << ", capacity is " << a.cap()
<< ", should be " << capacities[i] << endl;
}
}
cout << "\nRemove and resize test passed if no errors reported above.\n";


cout << "\nAdding 152 to array. Final print should print just 152.\n";
a.append(152);
a.print();
cout << endl;

return 0;

}


dynamicarray.h


class DynamicArray {
public:
DynamicArray();

~DynamicArray();
void append(int newVal);
int at(int index);
int sum();
bool remove(int valToDelete);
void print();
int cap() { return capacity; }

private:
int* arr;
int len; // Number of elements actually populated
int capacity; // New variable - this is the current capacity of the array

};


dynamicarray.cpp


#include <iostream>
#include "dynamicarray.h"
using namespace std;

const int INITIAL_CAP = 10;

DynamicArray::DynamicArray() {
capacity = INITIAL_CAP;
arr = new int[capacity];
len = 0;
}
DynamicArray::~DynamicArray()
{
if(arr)
{
delete[] arr;
}
}
// Print all array elements
void DynamicArray::print() {
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
if (i % 9 == 0) {
cout << endl; // newline every 10 elements
}
}
cout << endl;
}

//Append function
void DynamicArray::append(int num)
{
if (len < capacity)
{
arr[len++] = num;
}
else
{
capacity = capacity * 2;
int *arr1 = new int[capacity];
for(int s=0;s<len;s++)
arr1[s] = arr[s];
delete[] arr;

arr = arr1;
arr[len++] = num;
}
}

int DynamicArray::at(int index)
{
if(index>=len)
return -1111;
else
return arr[index];
}
int DynamicArray::sum()
{
int sum=0;
for(int s=0;s<len;s++)
sum+=arr[s];
return sum;
}
bool DynamicArray::remove(int valToDelete)
{
for(int i=0;i<len;i++)
{
if (arr[i] == valToDelete)
{
for(int s=i;s<len;s++)
arr[s] = arr[s+1];
len--;
if (len < (capacity/2))
{
float x = capacity/5;
capacity = capacity-x;
int *arr1 = new int[capacity];
for(int s=0;s<len;s++)
arr1[s] = arr[s];
delete[] arr;
arr = arr1;
}
return true;
}
}

return false;
}

Homework Answers

Answer #1

For me, there are no compilation error occuring. Please see below screenshot. I am getting the output as mentioned by you.

Please let me know if any other issue/concern.

Hope it helps. Thanks!

Know the answer?
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
Similar Questions
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i is saved in the array ascend[], in order. Compile and run the program; it should print 0 4 5. In this exercise, you’ll try to translate the C++ program to MIPS. Some of the more tedious parts are already given in Assignment3.F19.s. You won’t have to write the data allocation sections, some of the initializations, and the output loop at the end. So the...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT