Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help!!! Why isn't my code working properly? When I test it with the test data below it goes in to an endless loop when I

Help!!! Why isn't my code working properly? When I test it with the test data below it goes in to an endless loop when I enter " x 1.7"

How the Program should work:

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 not 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 DynamicArray 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.

Test Data:

image text in transcribed

Code: MyDynamicAray.cpp

//#include

#include "DynamicArray.h"

#include

#include

using namespace std;

#include

const bool PLACED = true;

int main()

{

int m,n;

DynamicArray storeValue;

DynamicArray storeStatus;

char ch;

int sze = 2; //store size in this variable, since the default size is two

int uniquecount = 0;

int storeTotal = 0;

string index;

float value;

char buf[10];

do

{

cout

cin >> buf;

if(buf[0] == 'q' || buf[0] == 'Q')

break;

cin >> value; // Get value from user

cin.ignore(1000,10);

storeValue[atof(index.c_str())] = atof(value.c_str()); // Store value

storeStatus[atof(index.c_str())] = PLACED;

}

while(index != "Q" || index != "q"); // Q to quit

for(int i = 0; i

{

if(storeStatus[i] == PLACED)

{

storeTotal++;

}

}

// Output

cout

cout

for(int i = 0; i

{

if(storeStatus[i] != 0)

{

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 = storeValue[atoi(index.c_str())]; // Find value for user

cout

}

else

{

cout

}

}

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

} // Close Main

// End of program

DynamicArrayDrive.cpp :

#include

#include

#include

#include "DynamicArray.h"

using namespace std;

void MyDynamicDriver()

{

DynamicArray string_array;

DynamicArray int_array;

cout

cout

cout

cout

cout

cout

cout

cout

string_array[5] = "fGhslawpoi";

string_array[46] = "gsljdofua";

string_array[3] = "rsdkjbh";

string_array[51] = "iuewn";

string_array[7] = "wiyur";

string_array[10] = "nviy";

string_array[18] = "rsdkjbh";

string_array[75] = "fslkhaf";

string_array[63] = "wsuur";

string_array[15] = "sdlkhw";

int_array[32] = 723;

int_array[3] = 854;

int_array[43] = 66;

int_array[3] = 257;

int_array[7] = 101;

int_array[9] = 127;

int_array[19] = 456;

int_array[98] = 36;

int_array[63] = 13;

int_array[8] = 491;

cout

cout

cout

cout

cout

const DynamicArray s_arr(string_array);

const DynamicArrayint_arr(int_array);

cout

cout

cout

cout

DynamicArray s_arr2;

DynamicArray int_arr2;

cout

cout

s_arr2 = s_arr;

int_arr2 = int_arr;

cout

cout

cout

cout

cout

cout

cout

cout

bool needComma;

needComma = false;

for (unsigned int i = 0; i

{

if (s_arr.ContainsKey(i))

{

if (needComma)

cout

needComma = true;

cout

cout

}

}

cout

cout

needComma = false;

for (unsigned int i = 0; i

{

if (int_arr.ContainsKey(i))

{

if (needComma)

cout

needComma = true;

cout

cout

}

}

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

needComma = false;

for (unsigned int i = 0; i

{

if (s_arr2.ContainsKey(i))

{

if (needComma)

cout

needComma = true;

cout

cout

}

}

cout

needComma = false;

cout

for (unsigned int i = 0; i

{

if (int_arr2.ContainsKey(i))

{

if (needComma)

cout

needComma = true;

cout

cout

}

}

cout

cout

cout

for (unsigned int i = 0; i

{

if (s_arr2.ContainsKey(i))

{

s_arr2.DeleteKey(i);

cout

break;

}

}

cout

for (unsigned int i = 0; i

{

if (int_arr2.ContainsKey(i))

{

int_arr2.DeleteKey(i);

cout

break;

}

}

cout

cout

needComma = false;

for (unsigned int i = 0; i

{

if (s_arr2.ContainsKey(i))

{

if (needComma)

cout

needComma = true;

cout

cout

}

}

cout

cout

needComma = false;

for (unsigned int i = 0; i

{

if (int_arr2.ContainsKey(i))

{

if (needComma)

cout

needComma = true;

cout

cout

}

}

cout

cout

vector s_indices;

s_indices = s_arr2.Keys();

cout

needComma = false;

for (unsigned int i = 0; i

{

if (needComma) cout

needComma = true;

cout

}

cout

vector i_indices;

i_indices = int_arr2.Keys();

cout

needComma = false;

for (unsigned int i = 0; i

{

if (needComma) cout

needComma = true;

cout

}

cout

cout

s_arr2.Clear();

int_arr2.Clear();

cout

cout

}

int main()

{

MyDynamicDriver();

cin.get();

return 0;

}

DynamicArray.h:

ifndef DYNAMICARRAY_H

#define DYNAMICARRAY_H

#include

using namespace std;

template

class DynamicArray

{

private:

int size;

dataT dummy;

dataT *data;

bool *in_use;

static const int SIZE = 100;

unsigned int capacity;

public:

DynamicArray(int = SIZE);

DynamicArray(const DynamicArray &);

virtual ~DynamicArray();

DynamicArray & operator=(const DynamicArray &);

dataT operator[](unsigned int )const;

dataT& operator[](unsigned int);

unsigned int Size()const;

unsigned int Capacity()const;

bool ContainsKey(unsigned int)const;

void DeleteKey(unsigned int);

vector Keys()const;

void Clear();

private:

void Copy(const DynamicArray &);

void Delete();

void SetCapacity(unsigned int SIZE = 10);

};

template

DynamicArray::DynamicArray(int SIZE)

{

capacity = SIZE;

data = new dataT[capacity];

in_use = new bool[capacity];

size = 0;

for (int i = 0; i

in_use[i] = false;

}

template

DynamicArray::DynamicArray(const DynamicArray & d_arr)

:data(NULL), in_use(NULL)

{

Copy(d_arr);

}

template

DynamicArray:: ~DynamicArray()

{

Delete();

}

template

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

{

if (this != &d_arr)

{

Delete();

Copy(d_arr);

}

return *this;

}

template

dataT DynamicArray::operator[](unsigned int index)const

{

if (index >= capacity)

return dummy;

return data[index];

}

template

dataT& DynamicArray::operator[](unsigned int idx)

{

if (idx + 1 > capacity)

{

SetCapacity(idx + 1);

}

if (!in_use[idx]) size++;

in_use[idx] = true;

return data[idx];

}

template

unsigned int DynamicArray::Size()const

{

return size;

}

template

unsigned int DynamicArray::Capacity()const

{

return capacity;

}

template

bool DynamicArray::ContainsKey(unsigned int idx)const

{

if (idx

return in_use[idx];

return false;

}

template

void DynamicArray::DeleteKey(unsigned int idx)

{

if (idx

{

if (in_use[idx])

{

size--;

in_use[idx] = false;

}

}

}

template

vector DynamicArray::Keys()const

{

vector a;

for (unsigned int i = 0; i

{

if (in_use[i])

a.push_back(i);

}

return a;

}

template

void DynamicArray::Clear()

{

for (unsigned int i = 0; i

in_use[i] = false;

}

template

void DynamicArray::Copy(const DynamicArray & D_array)

{

size = D_array.size;

capacity = D_array.capacity;

data = new dataT[capacity];

in_use = new bool[capacity];

for (unsigned int i = 0; i

{

in_use[i] = false;

if (D_array.in_use[i])

{

in_use[i] = true;

data[i] = D_array.data[i];

}

}

}

template

void DynamicArray::Delete()

{

if (data)

{

delete[] data;

data = NULL;

}

if (in_use)

{

delete in_use;

in_use = NULL;

}

size = capacity = 0;

}

template

void DynamicArray::SetCapacity(unsigned int new_size)

{

if (new_size == 0)

{

Delete();

return;

}

dataT * newvalues = new dataT[new_size];

bool * newInUse = new bool[new_size];

size = 0;

unsigned int upperLimit;

if (capacity

upperLimit = capacity;

else

upperLimit = new_size;

for (unsigned int i = 0; i

{

newInUse[i] = in_use[i];

if (in_use[i])

{

size++;

newvalues[i] = data[i];

}

}

if (upperLimit

{

for (unsigned int i = upperLimit; i

{

newInUse[i] = false;

}

}

capacity = new_size;

if (data)

delete[] data;

if (in_use)

delete[] in_use;

data = newvalues;

in_use = newInUse;

}

template

ostream & operator & D_array)

{

vector a = D_array.Keys();

for (unsigned int i = 0; i

{

cout

if (i

cout

}

return output;

}

#endif

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: 4 The index-value pairs are: 4 100 5 300 33 => 120 2000 => -999.9 Input an index for me to look up [Q to quit]: 33 Input an index for me to look up [Q to quit]: -10 Input an index for me to look up [Q to quit]: 38 Input an index for me to look up [Q to quit]: 10000 Input an index for me to look up [Q to quit]: 2000 Input an index for me to look up [Q to quit]: q Found it the value stored at index 33 is 120 Sorry, but there is no value stored at index -10 Sorry, but there is no value stored at index 38 Sorry, but there is no value stored at index 10000 Found it -- the value stored at index 2000 is -999.9

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago