{"id":217639,"date":"2018-09-15T22:59:40","date_gmt":"2018-09-15T22:59:40","guid":{"rendered":"http:\/\/localhost\/wordpress\/index.php\/2018\/09\/15\/gsp-125-week-3-ilab-list-of-numbers\/"},"modified":"2018-10-24T08:41:09","modified_gmt":"2018-10-24T08:41:09","slug":"gsp-125-week-3-ilab-list-of-numbers","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/gsp-125-week-3-ilab-list-of-numbers\/","title":{"rendered":"GSP 125 Week 3 iLab List of Numbers"},"content":{"rendered":"<p>GSP 125 GSP\/125 GSP 125 \u00a0Week 3 iLab\t\u00a0\t\/\/ lab3: listofnumbers\t\/\/ &lt;insert your name here&gt;\t\/\/ read main.cpp, and follow the instructions at the bottom of main.cpp\t#include &lt;iostream&gt;\tusing namespace std;\tint main()\t{\tint numberOfElements = 0;\tfloat * elements = NULL;\tfloat userInput;\tbool addingNumbersToTheList;\tcout &lt;&lt; &quot;Keep entering numbers. Enter a non-number to stop.&quot; &lt;&lt; endl;\tdo\t{\tcin &gt;&gt; userInput;\taddingNumbersToTheList = !std::cin.fail();\tif(addingNumbersToTheList) {\t\/\/ make a bigger array to replace the old one\tfloat * biggerArray = new float[numberOfElements+1];\tif(elements != NULL)\t{\t\/\/ copy the old elements into the biggerArray\tfor(int i = 0; i &lt; numberOfElements; i++)\t{\tbiggerArray[i] = elements[i];\t}\t\/\/ the old array is not needed anymore, we have a better copy\tdelete elements;\t}\t\/\/ point at the new array\telements = biggerArray;\tnumberOfElements = numberOfElements+1;\t\/\/ put the new number into the last element of the array\telements[numberOfElements-1] = userInput; }\t}\twhile(addingNumbersToTheList); \/\/ fix cin after intentionally breaking it above.\tif(std::cin.fail())\t{\tstd::cin.clear();\twhile(std::cin.get() != &#8216;n&#8217;);\t}\tbool hasNumbers = numberOfElements &gt; 0;\tif(hasNumbers) {\t\/\/ print the stored numbers\tcout &lt;&lt; &quot;Entered numbers: &quot; &lt;&lt; endl;\tcout &lt;&lt; &quot;{&quot;;\tfor(int i = 0; i &lt; numberOfElements; ++i)\t{\tif(i &gt; 0)\t{\tcout &lt;&lt; &quot;, &quot;;\t}\tcout &lt;&lt; elements[i]; }\tcout &lt;&lt; &quot;}&quot; &lt;&lt; endl; }\telse\t{ } float sum = 0;\tfor(int i = 0; i &lt; numberOfElements; ++i)\t{\tsum += elements[i];\t}\tcout &lt;&lt; &quot;total: &quot; &lt;&lt; sum &lt;&lt; endl;\tcout &lt;&lt; &quot;average: &quot; &lt;&lt; (sum \/ numberOfElements) &lt;&lt; endl; cout &lt;&lt; &quot;no numbers entered.&quot; &lt;&lt; endl;\t}\treturn 0; \/\/ INSTRUCTIONS \/\/\t\u00a0\t\/\/\t\/\/\t\/\/ &#8212;&#8212;&#8212;&#8211;Compile this code. You should be able to enter any number of numbers into\tthe console, but as soon as you enter a non-number, a list of every number\tyou typed should display.\tRead through this code! Try to understand it before starting the assignment.\tComment confusing lines with what you think code is doing, and experiment\twith existing code to test your understanding.\tOnce you feel comfortable with this code, accomplish each of the following,\tand make sure your code compiles and runs after each step is completed.\t1) The ManagedArray class\ta) Create a new class called ManagedArray. It should have a float pointer\tnamed &quot;elements&quot; to reference an array of floats, and an integer named\t&quot;numberOfElements&quot; to keep track of how many floats are in the array.\tb) Create a default constructor, which sets &quot;elements&quot; to NULL, and\t&quot;numberOfElements&quot; to 0.\tc) Create an accessor for the ManagedArray class called\t&quot;int ManagedArray::size()&quot;, which returns the number of elements\td) Create an accessor for the ManagedArray class called\t&quot;float ManagedArray::get(int index)&quot;, which returns the value, in\t&quot;elements&quot;, at the given index.\te) Create a member function for ManagedArray called\t&quot;void ManagedArray::add(float value)&quot;, which allocates a larger array\tthan &quot;elements&quot;, and replaces &quot;elements&quot; with it, adding &#8216;value&#8217; to the\tend of it. This method should also increase &quot;numberOfElements&quot;. See the\tcode within the &quot;if(addingNumbersToTheList) {&quot; block.\t2) Use ManagedArray\ta) Remove the &quot;elements&quot; and &quot;numberOfElements&quot; variables in main, and use\ta ManagedArray object to get the same functionality that was in main.\t3) Destructor and Copy Constructor\ta) Create a destructor (named &quot;ManagedArray::~ManagedArray()&quot;), which\tdeletes (with &quot;delete &quot;) the &quot;elements&quot; array.\tb) Create a copy-constructor (named\t&quot;ManagedArray::ManagedArray(ManagedArray &amp; ma)&quot;), which copies the\t&quot;numberOfElements&quot; variable from &quot;ma&quot;, and allocates an &quot;elements&quot;\tarray the same size, with data copied from &quot;ma&quot;.\t4) Print Function\ta) Create a new function called &quot;void print(ManagedArray ma)&quot;, which\tprints the given array, just like the code within the\t&quot;if(hasNumbers) {&quot; block.\tb) Use the print function to print out a ManagedArray at the end of your\tprogram.\tc) Use the print function to print out a ManagedArray a second time. This\twill crash your program if you wrote your destructor correctly but\t*did not* write your copy constructor correctly. If the program does\tnot crash, comment out your copy constructor, recompile and try again,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GSP 125 GSP\/125 GSP 125 \u00a0Week 3 iLab \u00a0 \/\/ lab3: listofnumbers \/\/ &lt;insert your name here&gt; \/\/ read main.cpp, and follow the instructions at the bottom of main.cpp #include &lt;iostream&gt; using namespace std; int main() { int numberOfElements = <a href=\"https:\/\/www.benedictsol.com\/blogs\/gsp-125-week-3-ilab-list-of-numbers\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-217639","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=217639"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217639\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=217639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=217639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=217639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}