GSP 125 Week 3 iLab List of Numbers

GSP 125 GSP/125 GSP 125  Week 3 iLab   // lab3: listofnumbers // <insert your name here> // read main.cpp, and follow the instructions at the bottom of main.cpp #include <iostream> using namespace std; int main() { int numberOfElements = 0; float * elements = NULL; float userInput; bool addingNumbersToTheList; cout << "Keep entering numbers. Enter a non-number to stop." << endl; do { cin >> userInput; addingNumbersToTheList = !std::cin.fail(); if(addingNumbersToTheList) { // make a bigger array to replace the old one float * biggerArray = new float[numberOfElements+1]; if(elements != NULL) { // copy the old elements into the biggerArray for(int i = 0; i < numberOfElements; i++) { biggerArray[i] = elements[i]; } // the old array is not needed anymore, we have a better copy delete elements; } // point at the new array elements = biggerArray; numberOfElements = numberOfElements+1; // put the new number into the last element of the array elements[numberOfElements-1] = userInput; } } while(addingNumbersToTheList); // fix cin after intentionally breaking it above. if(std::cin.fail()) { std::cin.clear(); while(std::cin.get() != ‘n’); } bool hasNumbers = numberOfElements > 0; if(hasNumbers) { // print the stored numbers cout << "Entered numbers: " << endl; cout << "{"; for(int i = 0; i < numberOfElements; ++i) { if(i > 0) { cout << ", "; } cout << elements[i]; } cout << "}" << endl; } else { } float sum = 0; for(int i = 0; i < numberOfElements; ++i) { sum += elements[i]; } cout << "total: " << sum << endl; cout << "average: " << (sum / numberOfElements) << endl; cout << "no numbers entered." << endl; } return 0; // INSTRUCTIONS //   // // // ———–Compile this code. You should be able to enter any number of numbers into the console, but as soon as you enter a non-number, a list of every number you typed should display. Read through this code! Try to understand it before starting the assignment. Comment confusing lines with what you think code is doing, and experiment with existing code to test your understanding. Once you feel comfortable with this code, accomplish each of the following, and make sure your code compiles and runs after each step is completed. 1) The ManagedArray class a) Create a new class called ManagedArray. It should have a float pointer named "elements" to reference an array of floats, and an integer named "numberOfElements" to keep track of how many floats are in the array. b) Create a default constructor, which sets "elements" to NULL, and "numberOfElements" to 0. c) Create an accessor for the ManagedArray class called "int ManagedArray::size()", which returns the number of elements d) Create an accessor for the ManagedArray class called "float ManagedArray::get(int index)", which returns the value, in "elements", at the given index. e) Create a member function for ManagedArray called "void ManagedArray::add(float value)", which allocates a larger array than "elements", and replaces "elements" with it, adding ‘value’ to the end of it. This method should also increase "numberOfElements". See the code within the "if(addingNumbersToTheList) {" block. 2) Use ManagedArray a) Remove the "elements" and "numberOfElements" variables in main, and use a ManagedArray object to get the same functionality that was in main. 3) Destructor and Copy Constructor a) Create a destructor (named "ManagedArray::~ManagedArray()"), which deletes (with "delete ") the "elements" array. b) Create a copy-constructor (named "ManagedArray::ManagedArray(ManagedArray & ma)"), which copies the "numberOfElements" variable from "ma", and allocates an "elements" array the same size, with data copied from "ma". 4) Print Function a) Create a new function called "void print(ManagedArray ma)", which prints the given array, just like the code within the "if(hasNumbers) {" block. b) Use the print function to print out a ManagedArray at the end of your program. c) Use the print function to print out a ManagedArray a second time. This will crash your program if you wrote your destructor correctly but *did not* write your copy constructor correctly. If the program does not crash, comment out your copy constructor, recompile and try again,

Previous answers to this question


This is a preview of an assignment submitted on our website by a student. If you need help with this question or any assignment help, click on the order button below and get started. We guarantee authentic, quality, 100% plagiarism free work or your money back.

order uk best essays Get The Answer
Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *