Question: Hi, I have an error with my program. It has an infinite loop when I try to put some indexes. It needs to follow these
Hi, I have an error with my program. It has an infinite loop when I try to put some indexes. It needs to follow these steps:
This assignment involves the writing of 2 CPP's. To it's clear which CPP goes with which "part" of the assignment, make sure that you have some reference to the part number in your file names (like part1.cpp or testing.1.cpp for example).
Part 1, Write And Test An Array Class [MyDynamicArray.h]
Write and test a data structures template. The resulting template can be used in any program in place of a C++ array.
Requirements. Develop MyDynamicArray.h as you write a test driver with class MyDynamicArray, defined and fully tested. Write the public interface exactly as specified below -- do not add to, or change the public interface as specified.
Write the template for an array of 2 values (initially) of unspecified type.
Include a public square bracket getter and setter pair, both with index range-checking, returning whatever value you wish if out of range. But apply capacity auto-adjusting for the setter if out of range high.
Include a public getter named MyDynamicArray::capacity( ) to return the data structure's now-variable capacity
Include a public setter named MyDynamicArray::capacity(int) to change the capacity.
Do tests with int, double, or char. Also do tests with an object, like string.
Note that there is no good reason to copy the "dummy" value in the dynamic memory management functions, so don't include it in your testing of const object copy or object assignment.
Part 2, Write An Array Application
Write a C++ console app using your MyDynamicArray template. Use your already-tested and verified H file from part 1.
Exactly as in Lab Assignment 2, this app lets its user enter as many values as they like, and when that process is completed, lets the user look up values by matching index.
In a loop, the app should prompting the user to enter a pair of numbers on the same line: a whole number index and its corresponding floating point value. Do validate index input in the app. Quit the loop when an uppercase or lowercase Q is entered for either the index or the value. Indexes can be entered in any order -- they don't have to start with zero and go up by one thereafter. It's whatever the user enters.
Your app should keep track of which indexes got entered. Use a bool MyDynamicArray for that.
After all data entry is complete, the app should:
output how many (unique) indexes got entered,
output the list of all used indexes and their values, per the example below, and
implement an event-controlled loop that prompts for an index value and outputs whether the index is in use or not, and if in use, what is the value stored for that index. Loop until the user elects to stop by entering uppercase or lowercase Q.
Here's a sample of how this should work (user input in blue):
Input an index and a value [Q to quit]: 33 1.2 Input an index and a value [Q to quit]: 4 100 Input an index and a value [Q to quit]: 5 300 Input an index and a value [Q to quit]: x 1.7 Input an index and a value [Q to quit]: 33 120 Input an index and a value [Q to quit]: -1 23.4 Input an index and a value [Q to quit]: 2000 -999.9 Input an index and a value [Q to quit]: q You stored this many values: 5 The index-value pairs are: 0 => 1.7 4 => 100 5 => 300 33 => 120 2000 => -999.9 Input an index for me to look up [Q to quit]: 33 Found it -- the value stored at index 33 is 120 Input an index for me to look up [Q to quit]: 0 Found it -- the value stored at index 0 is 1.7 Input an index for me to look up [Q to quit]: -10 Sorry, but there is no value stored at index -10 Input an index for me to look up [Q to quit]: 38 Sorry, but there is no value stored at index 38 Input an index for me to look up [Q to quit]: 10000 Sorry, but there is no value stored at index 10000 Input an index for me to look up [Q to quit]: 2000 Found it -- the value stored at index 2000 is -999.9 Input an index for me to look up [Q to quit]: q
Header:
#ifndef DYNAMICARRAY_H_INCLUDED
#define DYNAMICARRAY_H_INCLUDED
#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
};
template
DynamicArray
{
this->cap = cap;
values = new V[cap];
for (int index = 0; index
values[index] = V();
}
}
template
V DynamicArray
{
if (index = cap)
return V(); // a copy
return values[index]; // a copy
}
template
V& DynamicArray
{
if (index
return dummy; // a copy
}
else if (index >= cap) {
capacity(2 * index);
}
return values[index]; // a copy
}
template
void DynamicArray
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
{
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
{
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 // DYNAMICARRAY_H_INCLUDED
CPP Code:
#include
#include
#include
#include
using namespace std;
#include
#include "DynamicArray.h"
const bool PLACED = true;
int main() {
DynamicArray
DynamicArray
int storeTotal = 0;
string index;
string value;
do {
cout
cin >> index;
if (index == "Q" || index == "q") {
break;
}
cin >> value;
cin.ignore(1000, 10);
valStore[atof(index.c_str())] = atof(value.c_str());
storeStatus[atof(index.c_str())] = PLACED;
} while (index != "Q" || index != "q");
cout
for (int i = 0; i
if (storeStatus[i] == PLACED) {
storeTotal++;
}
}
cout
cout
for (int i = 0; i
if (storeStatus[i] != 0) {
cout "
}
}
cout
do {
cout
cin >> index;
if (index == "Q" || index == "q") {
break;
}
if (storeStatus[atof(index.c_str())] == PLACED && atof(index.c_str()) >= 0 && atof(index.c_str())
double valFind = valStore[atoi(index.c_str())];
cout
}
else {
cout
}
} while (index != "Q" || index != "q");
}
Error picture:

clusers'ynightv Input an index and a value [Q to quit]: 33 1.2 Input an index and a value [Q to quit]: 4 188 Input an index and a value [Q to quit]: 5 388 Input an index and a value [Q to quit]: x 1.7 Input an index and a value [Q to quit]: 33 120 Input an index and a value [Q to quit]: -1 23.4 Input an index and a value [Q to quit]: 2808 -999.9 Input an index and a value [Q to quit]: q uments visual studio 2015 Projects Assignment 05bDebugAssignment 05b.exe You stored this many values: 5 The index-value pairs are: 1.7 26.27744e+66 3 6.27744e+66 4108 5->308 6 -> -6.27744e+66 6.27744c+66 86.27744c+66 9 -> -6.27744+66 e-> -6.27744+66 11 -> -6.27744e+66 12-6.27744e+66 13-6.27744e+66 14 -6.27744e+66 15-6.27744e+66 16 -6.27744e+66 17 )-6.27744e+66 18 => -6.27744e+66 19 => -6.27744e+66 20 => -6. 27744e+66 21 => -6.27744e+66 22 => -6.27744e+66 23-6.277442+66 24-6.277442+66 25-6.27744e+66 26-6.27744e+66 27-6.277442+66 28 -6.27744e+66 29 -6.27744e+66 3-6.27744e+66 316.277442+66 326.27744e466 3312 34-6.27744c+66 35-6.27744e+66 36-6.27744e+66 37-6.27744e+66 38-6.27744e+66 39-6.27744e+66 40 -6.27744e+66 41 -6.27744e+66 42 -6.27744e+66
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
