Question
Hi, I need to fix my program using the following requeriments. Thanks Write a console app using your MyStaticArray template . Use your already-tested and
Hi, I need to fix my program using the following requeriments. Thanks
Write a console app using your MyStaticArray template. Use your already-tested and verified H file from part 1.
Exactly as in Lab Assignment 1, 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. Except that these values are doubles. Use a MyStaticArray object of capacity 100 to track the values.
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 input in the app, because your template should handle out-of-range indexes, and it should allow overwriting an already-entered index. 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 MyStaticArray 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 0 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 Input an index and a value [Q to quit]: q You stored this many values: 4 The index-value pairs are: 0 => 1.7 4 => 0 5 => 300 33 => 120 Input an index for me to look up [Q to quit]: 33 Found it -- the value stored at 33 is 120 Input an index for me to look up [Q to quit]: 38 I didn't find it Input an index for me to look up [Q to quit]: 0 Found it -- the value stored at 0 is 1.7 Input an index for me to look up [Q to quit]: -100 I didn't find it Input an index for me to look up [Q to quit]: 1000 I didn't find it Input an index for me to look up [Q to quit]: Q
Header:
#ifndef StaticArray_h
#define StaticArray_h
#define CAP 100
template
class StaticArray
{
V values[CAP];
V dummy;
public:
StaticArray();
int capacity() const { return CAP; }
V operator[](int) const;
V& operator[](int);
};
template
StaticArray
{
for (int index = 0; index < CAP; index++)
values[index] = V();
}
template
V StaticArray
{
if (index < 0 || index >= CAP)
return V(); // a copy
return values[index]; // a copy
}
template
V& StaticArray
{
if (index < 0 || index >= CAP)
return dummy; // a mutable reference
return values[index]; // a mutable reference
}
#endif
CPP File
#include
#include
using namespace std;
#include
#include "StaticArray.h"
const bool PLACED = true;
int main() {
cout << endl;
StaticArray
StaticArray
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 < 100; 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 < 100; 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()) < 100) {
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started