Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I have an error with my program. It has an infinite loop when I try to put s

ID: 3722812 • Letter: H

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 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):

Header:

#ifndef DYNAMICARRAY_H_INCLUDED

#define DYNAMICARRAY_H_INCLUDED

#include <algorithm>

using namespace std;

template <typename V>

class DynamicArray

{

V* values;

int cap;

V dummy;

public:

DynamicArray(int = 2);

DynamicArray(const DynamicArray<V>&);

~DynamicArray() { delete[] values; }

int capacity() const { return cap; }

void capacity(int);

V operator[](int) const;

V& operator[](int);

DynamicArray<V>& operator=(const DynamicArray<V>&);

};

template <typename V>

DynamicArray<V>::DynamicArray(int cap)

{

this->cap = cap;

values = new V[cap];

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

values[index] = V();

}

}

template <typename V>

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

{

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

return V(); // a copy

return values[index]; // a copy

}

template <typename V>

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

{

if (index < 0) {

return dummy; // a copy

}

else if (index >= cap) {

capacity(2 * index);

}

return values[index]; // a copy

}

template <typename V>

void DynamicArray<V>::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 <typename V>

DynamicArray<V>::DynamicArray(const DynamicArray<V>& 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 <typename V>

DynamicArray<V>& DynamicArray<V>::operator=(const DynamicArray<V>& 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 <algorithm>

#include <iostream>

#include<cassert>

#include <string>

using namespace std;

#include<cstdlib>

#include "DynamicArray.h"

const bool PLACED = true;

int main() {

DynamicArray<double> valStore;

DynamicArray<bool> 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");

}

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

Explanation / Answer

just check the capacity before below loop in your program,you will get to know why it is putting you into infinite loop.

try to run code in debug mode,you can you eclips for c++ for the same.

cout <<storeStatus.capacity();


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

if (storeStatus[i] != 0) {

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

}

Note:-if you are able to perform the same let me know in comments,I will help you ahead.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote