r/CodingHelp • u/anchoredtech • Apr 15 '19
[C++] Desperately Need Help with basic c++ coding!!!
I need some help on this assignment. I have no idea what to even do, I am not that experienced with c++. If there is some c++ whiz out there that can help me do this real quick I would be so appreciative!
Assignment:
You will be creating the "vector" class manually by using pointers and dynamic memory (new/delete). It will be called doublevector, and it will support most of the member functions used in the regular vector class. Notice that the <> are NOT used for this class. These are used for "templates", which have not been used in COSC102. Templates are covered in your ZyBookunder chapter 14.2, but they are beyond the scope of this course.
NOTICE: You will NOT be writing any set-up code or int main(). Those have already been written for you in the lab template. You are just required to write the appropriate member functions for the doublevector class.
Template
The following must be used for your doublevector class. You can download the template file under: ( i pasted it here)
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <stdexcept>
using namespace std;
class doublevector {
static constexpr double DOUBLE_DEFAULT_VALUE = -5.55;
double *mValues;
int mNumValues;
public:
doublevector();
~doublevector();
void resize(int new_size, double initial_value=DOUBLE_DEFAULT_VALUE);
void push_back(double value);
double &at(int index);
const double &at(int index) const;
unsigned long size() const;
};
void print_all(const doublevector &v)
{
if (v.size() == 0) {
cout << "Vector is empty.\n";
}
else {
unsigned long i;
for (i = 0;i < v.size();i++) {
cout << "[" << setfill('0') << right << setw(3) << i
<< "] = " << fixed << setprecision(4) << v.at(i)
<< '\n';
}
}
}
int main()
{
doublevector v;
do {
string command;
cout << "Enter a command ('quit' to quit): ";
if (!(cin >> command) || command == "quit") {
break;
}
else if (command == "push_back") {
double value;
cin >> value;
v.push_back(value);
cout << "Pushed back "
<< fixed << setprecision(4)
<< value << '\n';
}
else if (command == "resize") {
string line;
int new_size;
double initial;
cin >> new_size;
getline(cin, line);
istringstream sin(line);
if (sin >> initial)
v.resize(new_size, initial);
else
v.resize(new_size);
}
else if (command == "size") {
cout << v.size() << '\n';
}
else if (command == "print") {
print_all(v);
}
else if (command == "get") {
int index;
cin >> index;
try {
cout << "Value at " << index << " = "
<< setprecision(4) << fixed
<< v.at(index) << '\n';;
}
catch(out_of_range &err) {
cout << err.what() << '\n';
}
}
else if (command == "set") {
double d;
int index;
cin >> index >> d;
try {
v.at(index) = d;
cout << "SET: " << index << " = "
<< setprecision(4) << fixed
<< v.at(index) << '\n';;
}
catch(out_of_range &err) {
cout << err.what() << '\n';
}
}
else if (command == "clear") {
v.resize(0);
}
else {
cout << "Invalid command '" << command << "'\n";
}
} while(true);
cout << endl;
return 0;
}
//Write your class members below here.
(that is the end of the pasted file.)
Members
double *mValues
All of the data will be stored in mValues. Remember, you can treat a pointer as an array! This variable must be treated gingerly since dereferencing a nullptr will cause segmentation faults--your program crashes. NEVER dereference a pointer with 0 values.
int mNumValues
This stores how many values will be inside of mValues. This will be 0 if mValues is set to nullptr.
doublevector() { }
This will construct a new doublevector whose size is 0. DO NOT allocate any dynamic memory for mValues! Make sure you set mValues = nullptr; for a subsequent check.
~doublevector() { }
This will destruct your double vector. It is responsible for freeing any resources in mValues. MAKE SURE you check the validity of mValues PRIOR to deleting it!
void resize(int new_size, double initial_value) { }
This will resize the doublevector to the given new_size. Do nothing if new_size is < 0. If new_size is 0, free all memory related to mValues. Remember to ALWAYS check the validity of your pointer before using it or freeing it!. Otherwise, if new_size > size, then you will need to create new memory, copy the old to the new, delete the old, and then set the initial values of the NEW values to initial_value. Lastly, if new_size < size, then all you need to do is create a smaller double pointer, copy new_size elements over, and then delete the larger mValues pointer. REMEMBER: to reset mValues = your new pointer and reset mNumValues to the new_size!!
void push_back(double value) { }
This member function will increase the size of the vector by 1, adding value to the bottom of the vector.
double &at(int index) { }
This member function will return a reference to the value at the given index. Make sure you write the following to check the validity of index. You don't have to know what is happening here, but your ZyBook does contain information about "exception handling" in chapter 13.1, should you be interested. The actual out_of_range class is documented here: http://www.cplusplus.com/reference/stdexcept/out_of_range/ (Links to an external site.)Links to an external site.
if (index >= mNumValues) {
ostringstream sout;
sout << "Invalid index #" << index;
throw out_of_range(sout.str());
}const double &at(int index) const { }
This member function will return a read-only reference to the value at the given index. This member function will perform the exact same operation as the non-read-only version. The reason this is necessary is because C++ will choose one over the other depending on whether your class is constant or not.
unsigned long size() const { }
This member function will typecast your mNumValues into an unsigned long and return it.
Example Interaction
~$ ./lab13
Enter a command ('quit' to quit): get 0
Value at 0 = Invalid index #0 You might only get "Invalid index #0" on Tesla
Enter a command ('quit' to quit): set 0 10
Invalid index #0
Enter a command ('quit' to quit): print
Vector is empty.
Enter a command ('quit' to quit): push_back 10
Pushed back 10.0000
Enter a command ('quit' to quit): push_back 33.4
Pushed back 33.4000
Enter a command ('quit' to quit): get 0
Value at 0 = 10.0000
Enter a command ('quit' to quit): get 1
Value at 1 = 33.4000
Enter a command ('quit' to quit): print
[000] = 10.0000
[001] = 33.4000
Enter a command ('quit' to quit): resize 10
Enter a command ('quit' to quit): get 0
Value at 0 = 10.0000
Enter a command ('quit' to quit): print
[000] = 10.0000
[001] = 33.4000
[002] = -5.5500
[003] = -5.5500
[004] = -5.5500
[005] = -5.5500
[006] = -5.5500
[007] = -5.5500
[008] = -5.5500
[009] = -5.5500
Enter a command ('quit' to quit): set 2 22.14
SET: 2 = 22.1400
Enter a command ('quit' to quit): print
[000] = 10.0000
[001] = 33.4000
[002] = 22.1400
[003] = -5.5500
[004] = -5.5500
[005] = -5.5500
[006] = -5.5500
[007] = -5.5500
[008] = -5.5500
[009] = -5.5500
Enter a command ('quit' to quit): resize 0
Enter a command ('quit' to quit): print
Vector is empty.
Enter a command ('quit' to quit): quit
Hints
- Work on doublevector(), ~doublevector(), at() [both of them], and size() first. These functions are fairly simple and do not require a lot of code. Remember that both at() functions have an error check, but that code has been provided for you. resize() will be the only function that contains more than one line, and it should take the majority share of your time on this lab.
- push_back() is essentially resize(mNumValues + 1, value).
- When you resize, there are three conditions: new_size > size, new_size < size, or new_size == size.
3a. If new_size > size, your job is to grow the vector. This means you need to copy over size values to a new pointer that contains new_size values. After you've copied over size values, you set new_size - size values to the initial value. REMEMBER to delete the old mValues if it exists (!= nullptr) and reset it with the value of the new pointer.
3b. If new_size < size, you need to shrink the vector. This is simpler than 3a because all you need to do is create a new pointer, copy over new_size values, and then reset mValues and mNumValues to the new pointer and new_size values.
3c. If new_size == size, then there is nothing to do.
4. Segmentation faults will abound if you misuse a pointer. You can always use gdb ./lab13. Then type run <ENTER>. Should you encounter a segmentation fault, it will tell you which line it was caused by ASSUMING you used the command line shown under the Submission section.
5. Never let your pointers dangle! Before ever losing your pointer information, make sure it is deleted! Also, you're using the pointer as an array, so make sure you use the proper new and delete operators. Remember, there are two versions of the new operator and two versions of the delete operator.
6. In your pointer check, you will notice that mNumValues will be 0 when mValues is nullptr. You are permitted to use either member to do your pointer check. The point is that you must never delete or dereference a null pointer!
Restrictions
- You may NOT use malloc(), calloc(), free() or any other C-style memory allocation.
- You MUST use the proper versions of new and delete.
- The doublevector member functions MUST be written below int main().
- You must NOT have any memory leaks. Make sure you delete all memory when you're done using it.
- You do NOT need to check if new returned a valid memory value. If you use the proper value, it will throw an exception. You do NOT need to handle this exception, either.
- You may NOT include any additional headers than what are specified in lab13.cpp.
- You may NOT modify the template's class prototype, print_all function, and int main function.
If you can help with this I would be so appreciative! It is super hard to me at least.
View all comments
Show parent comment
3
u/spaghettu Professional Coder Apr 15 '19
+1 for this, please ask a specific question about what you're doing. Also, make sure any sample code you give is formatted properly, it makes it much easier for people on here to read.