Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/ INSTRUCTIONS // ------------ // Compile this code. You should be able to enter

ID: 3839421 • Letter: #

Question

/ 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,
//      just to make sure you wrote your copy constructor correctly!

CODE:
#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() != ' ');
    }
    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;

        float sum = 0;
        for(int i = 0; i < numberOfElements; ++i)
        {
            sum += elements[i];
        }
        cout << "total: " << sum << endl;
        cout << "average: " << (sum / numberOfElements) << endl;
    }
    else
    {
        cout << "no numbers entered." << endl;
    }
    return 0;
}

Explanation / Answer

class ManagedArray{

public:
   ManagedArray();
   ManagedArray(MannagedArray & other);
   int size();
   float get(int index);
   void add(float value);

private:
   float *elements;
   int numberOfElements;
};


ManagedArray::ManagedArray() {
   elements = null;
   numberOfElements = 0;
}

ManagedArray::ManagedArray(ManagedArray & ma) {
   numberOfElements = ma.size();
   elements = new float(numberOfElements);
   for (int i = 0; i < numberOfElements; i++)
      element[i] = ma.get[i];
}

float ManagedArray::get(int index) {
   return element[index];
}

void ManagedArray::add(float value) {
   flat *tmp = new float[numberOfElements + 1];
   for (int i = 0; i < numberOfElements; i++)
      tmp[i] = element[i];
   tmp[numbherOfElement] = value;
   element = tmp;
   numberOfElemtnet++;
}

ManagedArray::~ManagedArray() {
   delete [] element;
}

void print(ManagedArray ma) {
   int size = ma.size;
   for (int i = 0; i < size; i++)
      cout<<ma.get(index)<<endl;
}


This set of code should pass you test case. Let me know the changed you need. I shall try my best to resolve all your issues.