Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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& operator=(const DynamicArray&);

};

template

DynamicArray::DynamicArray(int cap)

{

this->cap = cap;

values = new V[cap];

for (int index = 0; index < cap; index++) {

values[index] = V();

}

}

template

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

{

if (index < 0 || index >= cap)

return V(); // a copy

return values[index]; // a copy

}

template

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

{

if (index < 0) {

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 < limit; i++) {

temp[i] = values[i];

}

// set added values to their defaults

for (int i = limit; i < cap; 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 < cap; i++) { // contents copy original to new

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 < cap; i++) { // contents copy original to new

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 valStore;

DynamicArray storeStatus;

int storeTotal = 0;

string index;

string value;

do {

cout << "Input an index and a value [Q to quit]: ";

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 << endl;

for (int i = 0; i < storeStatus.capacity(); i++) {

if (storeStatus[i] == PLACED) {

storeTotal++;

}

}

cout << "You stored this many values: " << storeTotal << endl;

cout << "The index-value pairs are:" << endl;

for (int i = 0; i < storeStatus.capacity(); i++) {

if (storeStatus[i] != 0) {

cout << i << " => " << valStore[i] << endl;

}

}

cout << endl;

do {

cout << "Input an index and a value [Q to quit]: ";

cin >> index;

if (index == "Q" || index == "q") {

break;

}

if (storeStatus[atof(index.c_str())] == PLACED && atof(index.c_str()) >= 0 && atof(index.c_str()) < valStore.capacity()) {

double valFind = valStore[atoi(index.c_str())];

cout << "Found it -- the value stored at " << index << " is " << valFind << " ";

}

else {

cout << "I didn't find it ";

}

} while (index != "Q" || index != "q");

}

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

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

4. Apply the principles of accepted mechanics to your writing.

Answered: 1 week ago