Help!!! Why isn\'t my code working properly? When I test it with the test data b
ID: 3889414 • Letter: H
Question
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:
Code: MyDynamicAray.cpp
//#include <bits/stdc++.h>
#include "DynamicArray.h"
#include <iostream>
#include <string>
using namespace std;
#include<cstdlib>
const bool PLACED = true;
int main()
{
int m,n;
DynamicArray<float> storeValue;
DynamicArray<bool> 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 << "Input an index and a value [Q to quit]: ";
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 < 100; i++)
{
if(storeStatus[i] == PLACED)
{
storeTotal++;
}
}
// Output
cout << endl << "You stored this many values: " << storeTotal << endl;
cout << "The index-value pairs are:" << endl;
for(int i = 0; i < 100; i++)
{
if(storeStatus[i] != 0)
{
cout << i << " => " << storeValue[i] << endl << endl; // Show stored value
}
}
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()) < 100)
{
double valFind = storeValue[atoi(index.c_str())]; // Find value for user
cout << "Yay, I Found it! The value stored at " << index << " is " << valFind << endl;
}
else
{
cout << "Oh no! I didn't find it that one" << endl;
}
}
while(index != "Q" || index != "q");
} // Close Main
// End of program
DynamicArrayDrive.cpp :
#include <iostream>
#include <array>
#include <string>
#include "DynamicArray.h"
using namespace std;
void MyDynamicDriver()
{
DynamicArray<string> string_array;
DynamicArray<int> int_array;
cout << endl << endl;
cout << "-------------------------------------------------- ostream << Test:" << endl;
cout << "Before insert value into string_array: " << endl;
cout << "string_array: " << string_array;
cout << "int_array: " << int_array;
cout << endl;
cout << "------------------------------------------------- [] Test: " << endl;
cout << "Inserting some junk values " << endl;
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 << "After: " << endl;
cout << "string_array: " << string_array << endl;
cout << "int_array: " << int_array << endl;
cout << endl;
cout << "------------------------------------------------- Copy Test: " << endl;
const DynamicArray<string> s_arr(string_array);
const DynamicArray<int>int_arr(int_array);
cout << "s_arr: " << s_arr << endl;
cout << "int_arr: " << int_arr << endl;
cout << endl;
cout << "------------------------------------------------- = Test: " << endl;
DynamicArray<string> s_arr2;
DynamicArray<int> int_arr2;
cout << "New array s_arr2 instantiated: " << s_arr2 << endl;
cout << "Copy s_arr2 = string_array" << endl;
s_arr2 = s_arr;
int_arr2 = int_arr;
cout << "New array int_arr2 instantiated: " << int_arr2 << endl;
cout << "Copy int_arr2 = int_arr" << endl;
cout << "s_arr2: " << s_arr2 << endl;
cout << "int_arr2: " << int_arr2 << endl;
cout << endl;
cout << "-------------------------------------------------- [] const Test" << endl;
cout << "Print first 10 values : " << endl;
cout << "s_arr: ";
bool needComma;
needComma = false;
for (unsigned int i = 0; i < 10; ++i)
{
if (s_arr.ContainsKey(i))
{
if (needComma)
cout << ", ";
needComma = true;
cout << "[" << i << " : ";
cout << s_arr[i] << "]";
}
}
cout << endl;
cout << "int_arr: ";
needComma = false;
for (unsigned int i = 0; i < 10; ++i)
{
if (int_arr.ContainsKey(i))
{
if (needComma)
cout << ", ";
needComma = true;
cout << "[" << i << " : ";
cout << int_arr[i] << "]";
}
}
cout << endl;
cout << "-------------------------------------------------- Size() Test: " << endl;
cout << "string_array.Size(): " << string_array.Size() << endl;
cout << "s_arr.Size(): " << s_arr.Size() << endl;
cout << "s_arr2.Size(): " << s_arr2.Size() << endl;
cout << "int_arr.Size(): " << int_arr.Size() << endl;
cout << "int_arr2.Size(): " << int_arr2.Size() << endl;
cout << endl;
cout << "-------------------------------------------------- Capacity() Test: " << endl;
cout << "string_array.Capacity(): " << string_array.Capacity() << endl;
cout << "s_arr.Capacity(): " << s_arr.Capacity() << endl;
cout << "s_arr2.Capacity(): " << s_arr2.Capacity() << endl;
cout << "int_arr.Capacity(): " << int_arr.Capacity() << endl;
cout << "int_arr2.Capacity(): " << int_arr2.Capacity() << endl;
cout << endl;
cout << "-------------------------------------------------- ContainsKey test: " << endl;
cout << "Display first 10 data of s_arr2 : ";
needComma = false;
for (unsigned int i = 0; i < 10; ++i)
{
if (s_arr2.ContainsKey(i))
{
if (needComma)
cout << ", ";
needComma = true;
cout << "[ " << i << ": ";
cout << s_arr2[i] << "]";
}
}
cout << endl;
needComma = false;
cout << "Display first 10 int_arr2: ";
for (unsigned int i = 0; i < 10; ++i)
{
if (int_arr2.ContainsKey(i))
{
if (needComma)
cout << ", ";
needComma = true;
cout << "[ " << i << ": ";
cout << int_arr2[i] << "]";
}
}
cout << endl << endl;
cout << "-------------------------------------------------- DeleteKey Test: " << endl;
cout << "s_arr2: ";
for (unsigned int i = 0; i < s_arr2.Size(); ++i)
{
if (s_arr2.ContainsKey(i))
{
s_arr2.DeleteKey(i);
cout << "index[" << i << "]" << " deleted " << endl;
break;
}
}
cout << "int_arr2: ";
for (unsigned int i = 0; i < int_arr2.Size(); ++i)
{
if (int_arr2.ContainsKey(i))
{
int_arr2.DeleteKey(i);
cout << "index[" << i << "]" << " deleted " << endl;
break;
}
}
cout << "then display first 10 values: " << endl;
cout << "s_arr2: " ;
needComma = false;
for (unsigned int i = 0; i < 10; ++i)
{
if (s_arr2.ContainsKey(i))
{
if (needComma)
cout << ", ";
needComma = true;
cout << "[" << i << ": ";
cout << s_arr2[i] << "]";
}
}
cout << endl;
cout << "int_arr2: ";
needComma = false;
for (unsigned int i = 0; i < 10; ++i)
{
if (int_arr2.ContainsKey(i))
{
if (needComma)
cout << ", ";
needComma = true;
cout << "[" << i << ": ";
cout << int_arr2[i] << "]";
}
}
cout << endl << endl;
cout << "-------------------------------------------------- Keys Test: " << endl;
vector<unsigned int> s_indices;
s_indices = s_arr2.Keys();
cout << "s_arr2 in_use index: ";
needComma = false;
for (unsigned int i = 0; i < s_indices.size(); ++i)
{
if (needComma) cout << ", ";
needComma = true;
cout << s_indices[i];
}
cout << endl;
vector<unsigned int> i_indices;
i_indices = int_arr2.Keys();
cout << "int_arr2 in_use index: ";
needComma = false;
for (unsigned int i = 0; i < i_indices.size(); ++i)
{
if (needComma) cout << ", ";
needComma = true;
cout << i_indices[i];
}
cout << endl << endl;
cout << "-------------------------------------------------- Clear Test: " << endl;
s_arr2.Clear();
int_arr2.Clear();
cout << "s_arr2 : " << s_arr2 << endl;
cout << "int_arr2: " << int_arr2 << endl;
}
int main()
{
MyDynamicDriver();
cin.get();
return 0;
}
DynamicArray.h:
ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <vector>
using namespace std;
template <class dataT>
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<dataT> &);
virtual ~DynamicArray();
DynamicArray<dataT> & operator=(const DynamicArray<dataT> &);
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<unsigned int> Keys()const;
void Clear();
private:
void Copy(const DynamicArray<dataT> &);
void Delete();
void SetCapacity(unsigned int SIZE = 10);
};
template <class dataT>
DynamicArray<dataT>::DynamicArray(int SIZE)
{
capacity = SIZE;
data = new dataT[capacity];
in_use = new bool[capacity];
size = 0;
for (int i = 0; i < capacity; i++)
in_use[i] = false;
}
template <class dataT>
DynamicArray<dataT>::DynamicArray(const DynamicArray<dataT> & d_arr)
:data(NULL), in_use(NULL)
{
Copy(d_arr);
}
template <class dataT>
DynamicArray<dataT>:: ~DynamicArray()
{
Delete();
}
template <class dataT>
DynamicArray<dataT> & DynamicArray<dataT>::operator=(const DynamicArray<dataT> & d_arr)
{
if (this != &d_arr)
{
Delete();
Copy(d_arr);
}
return *this;
}
template <class dataT>
dataT DynamicArray<dataT>::operator[](unsigned int index)const
{
if (index >= capacity)
return dummy;
return data[index];
}
template <class dataT>
dataT& DynamicArray<dataT>::operator[](unsigned int idx)
{
if (idx + 1 > capacity)
{
SetCapacity(idx + 1);
}
if (!in_use[idx]) size++;
in_use[idx] = true;
return data[idx];
}
template <class dataT>
unsigned int DynamicArray<dataT>::Size()const
{
return size;
}
template <class dataT>
unsigned int DynamicArray<dataT>::Capacity()const
{
return capacity;
}
template <class dataT>
bool DynamicArray<dataT>::ContainsKey(unsigned int idx)const
{
if (idx < capacity)
return in_use[idx];
return false;
}
template <class dataT>
void DynamicArray<dataT>::DeleteKey(unsigned int idx)
{
if (idx < capacity)
{
if (in_use[idx])
{
size--;
in_use[idx] = false;
}
}
}
template <class dataT>
vector<unsigned int> DynamicArray<dataT>::Keys()const
{
vector<unsigned int> a;
for (unsigned int i = 0; i < capacity; ++i)
{
if (in_use[i])
a.push_back(i);
}
return a;
}
template <class dataT>
void DynamicArray<dataT>::Clear()
{
for (unsigned int i = 0; i < capacity; ++i)
in_use[i] = false;
}
template <class dataT>
void DynamicArray<dataT>::Copy(const DynamicArray<dataT> & 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 < capacity; ++i)
{
in_use[i] = false;
if (D_array.in_use[i])
{
in_use[i] = true;
data[i] = D_array.data[i];
}
}
}
template <class dataT>
void DynamicArray<dataT>::Delete()
{
if (data)
{
delete[] data;
data = NULL;
}
if (in_use)
{
delete in_use;
in_use = NULL;
}
size = capacity = 0;
}
template <class dataT>
void DynamicArray<dataT>::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 < new_size)
upperLimit = capacity;
else
upperLimit = new_size;
for (unsigned int i = 0; i < upperLimit; ++i)
{
newInUse[i] = in_use[i];
if (in_use[i])
{
size++;
newvalues[i] = data[i];
}
}
if (upperLimit < new_size)
{
for (unsigned int i = upperLimit; i < new_size; ++i)
{
newInUse[i] = false;
}
}
capacity = new_size;
if (data)
delete[] data;
if (in_use)
delete[] in_use;
data = newvalues;
in_use = newInUse;
}
template <class dataT>
ostream & operator<<(ostream & output, const DynamicArray<dataT> & D_array)
{
vector<unsigned int> a = D_array.Keys();
for (unsigned int i = 0; i < a.size(); ++i)
{
cout << "[" << a[i] << ": " << D_array[a[i]] << "]";
if (i < a.size() - 1)
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.9Explanation / Answer
#include <bits/stdc++.h>
#include "DynamicArray.h"
#include <iostream>
#include <string>
using namespace std;
#include<cstdlib>
const bool PLACED = true;
int main()
{
int m,n;
DynamicArray<float> storeValue;
DynamicArray<bool> 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 << "Input an index and a value [Q to quit]: ";
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 < 100; i++)
{
if(storeStatus[i] == PLACED)
{
storeTotal++;
}
}
// Output
cout << endl << "You stored this many values: " << storeTotal << endl;
cout << "The index-value pairs are:" << endl;
for(int i = 0; i < 100; i++)
{
if(storeStatus[i] != 0)
{
cout << i << " => " << storeValue[i] << endl << endl; // Show stored value
}
}
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()) < 100)
{
double valFind = storeValue[atoi(index.c_str())]; // Find value for user
cout << "Yay, I Found it! The value stored at " << index << " is " << valFind << endl;
}
else
{
cout << "Oh no! I didn't find it that one" << endl;
}
}
while(index != "Q" || index != "q");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.