05 Jun GSP 125 Week 3 iLab List of Numbers NEW
// 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,
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
