CST 150 Lab 4 Part Two – Template Classes Read the lecture notes on Templates. There are function Templates and two sets of notes on Template Classes. The array sample in the notes does pointers. We...

see attached files!


CST 150 Lab 4 Part Two – Template Classes Read the lecture notes on Templates. There are function Templates and two sets of notes on Template Classes. The array sample in the notes does pointers. We are not going to do pointers in this lab. Problem statement For this program you will be creating a template class that will deal with an array of either integers or doubles. You will be keeping the number and an integer with the number of numbers you have. The max for the array will be 25 positions. The input file is numbers.txt. When printing doubles print to 3 decimal places. You should write set, get and print methods. Set Methods You will write a few of these named appropriately. • One will take in the number to add to the list. If there is only one integer, add the number to the end of the list, if there is room and update the number of elements. If there is no room, a message should go to the screen saying what number was attempting to be added and that the array is full. • One will take in two numbers, the first will be the number to replace a number in the list and the second is the position that will be replaced. If a 0 is entered it will be the position 0. If the number to be replaced is out of range, 0 though one minus the number of elements in the array. A message should be stated and the position that is trying to be replaced should be stated. Get Methods • We will not be returning the whole list • Write a method that will take in a position to be returned. You should make sure the position is withing range and return the number at that position. If it is out of range return a 0. Print Methods • One will take in the stream to print to and an integer. This integer will be the position to print. If it is out of range, print a 0 • If it only takes in the stream, print the whole array with a heading in 5 columns. Additional Methods: Sum – will return the sum of all the filled numbers in the array Average – will return the average of all the filled numbers in the array High – will return the highest number of the filled positions in the array HowMany – will take in an integer and will tell the number of times that integer is in the array. Keep in mind that if we are looking for the number 9, 9 would be one time…99 would be two times. 9.99 would be three times. So you have to find each digit in the numbers and do an over all count. There are many ways of doing this. It may be easier to change the number to a string and parse it that way. Converting from integer to string and from double to string Int to string //an array of characters one bigger than the number of //numbers you have char buffer[31]; //x is the integer number, buffer will be filled with //the converted null terminated string and the 10 is //the base _itoa_s(x, buffer, 10); //declaring a string to store the number //casting it to a string and storing it string str; //declared above str = string(buffer); cout < str;="" double="" to="" string="" double="" d="238649.21316934;" string="" s="to_string(d);" cout="">< "conversion="" of="" double="" to="" string:="" "="">< s;="" hint:="" remember="" you="" have="" to="" compare="" integers="" to="" integers,="" characters="" to="" characters="" and="" strings="" to="" strings.="" create="" two="" arrays.="" an="" integer="" array="" and="" a="" double="" array.="" you="" will="" be="" filling="" the="" array="" from="" a="" file="" called="" numbers.txt.="" this="" will="" have="" one="" number="" on="" each="" line.="" you="" do="" not="" know="" if="" the="" number="" is="" an="" integer="" or="" a="" double.="" you="" will="" have="" to="" read="" it="" into="" a="" string="" and="" determine="" if="" there="" is="" a="" decimal="" in="" the="" number.="" if="" so,="" it="" is="" a="" double,="" if="" not="" it="" is="" an="" integer.="" here="" is="" an="" example="" of="" converting="" a="" string="" to="" a="" float="" (double)="" double="" mydouble;="" mydouble="atof(word.c_str());" word="" is="" a="" string="" and="" it="" will="" convert="" it="" to="" a="" null="" terminated="" string="" then="" convert="" it="" to="" a="" float.="" the="" function="" atof="" means="" alpha="" to="" float="" there="" is="" also="" a="" way="" to="" convert="" to="" an="" integer.="" the="" function="" atoi="" stands="" for="" alpha="" to="" integer.="" int="" myint;="" myint="atoi(word.c_str());" there="" is="" no="" error="" testing="" above,="" you="" have="" to="" make="" sure="" it="" is="" an="" integer="" or="" a="" double.="" you="" would="" have="" to="" do="" exceptions,="" that="" is="" not="" in="" this="" class.="" the="" file="" will="" have="" only="" a="" minus="" sigh,="" numbers="" and="" decimals="" in="" it.="" write="" a="" main="" that="" will="" fill="" the="" both="" arrays="" from="" the="" file="" until="" filled="" or="" there="" is="" no="" more="" data="" in="" the="" file.="" write="" a="" main="" that="" will="" test="" all="" of="" your="" member="" functions.="" send="" all="" data="" to="" an="" output="" file="" called="" templateoutput.txt.="" put="" headings="" on="" the="" output="" to="" show="" what="" you="" are="" testing.="" cst="" 150="" lab="" 4="" part="" one="" –="" overloading="" operators="" lab="" exercise="" —="" overloading="" operators="" use="" the="" date="" class="" program="" to="" start="" this="" program.="" it="" is="" found="" within="" this="" lab="" and="" in="" the="" mod="" 3="" notes="" 1.="" overload="" the="=" operator.="" remember="" that="" you="" will="" want="" to="" compare="" the="" month,="" day,="" and="" year.="" 2.="" add="" an="" overloaded="" ++="" operator="" 3.="" create="" an="" overloaded="">< operator="" as="" a="" non-member.="" the="" prototype="" in="" the="" .h="" will="" look="" like="" this:="" ostream&=""><(ostream&, const="" date&);="" the="" function="" itself="" will="" look="" something="" like="" this:="" ostream&=""><(ostream& os,="" const="" date&="" mydate)="" {="" os="">< "date="" is:="" "="">< mydate.getmonth()="">< "/"="">< mydate.getday()="">< "/"="">< mydate.getyear()="">< endl;="" return="" os;="" }="" 4.="" explain="" why="">< must be overloaded as a non-member. write this answer as a comment in your main function. date is generic for the name of the class. overload the + operator so that the following lines of code run in main: date date3, date4; date3=date1+82; date4=6+date2; more details: o define two functions. the prototypes will look like this: o date operator+ (const date&, int); date operator+ (int, const date&); are they member or non-member functions? (notice how many arguments they have) o both functions will create a temporary date and return it. o think about reusing as much code as possible. a. you have to understand the date class. there are functions in there you can use. b. for one implementation of operator+, can you call the operator++ function? c. for the second implementation of operator+, can you call the first operator+ function? d. together these two functions should be no more than 20 lines of code in the bodies. if you make it longer, you are working too hard. o which of the two functions must be defined as a non-member? why? write the answer to this question as a comment in your main. rewrite the main so that it tests all these new items. display the new dates. must="" be="" overloaded="" as="" a="" non-member.="" write="" this="" answer="" as="" a="" comment="" in="" your="" main="" function.="" date="" is="" generic="" for="" the="" name="" of="" the="" class.="" overload="" the="" +="" operator="" so="" that="" the="" following="" lines="" of="" code="" run="" in="" main:="" date="" date3,="" date4;="" date3="date1+82;" date4="6+date2;" more="" details:="" o="" define="" two="" functions.="" the="" prototypes="" will="" look="" like="" this:="" o="" date="" operator+="" (const="" date&,="" int);="" date="" operator+="" (int,="" const="" date&);="" are="" they="" member="" or="" non-member="" functions?="" (notice="" how="" many="" arguments="" they="" have)="" o="" both="" functions="" will="" create="" a="" temporary="" date="" and="" return="" it.="" o="" think="" about="" reusing="" as="" much="" code="" as="" possible.="" a.="" you="" have="" to="" understand="" the="" date="" class.="" there="" are="" functions="" in="" there="" you="" can="" use.="" b.="" for="" one="" implementation="" of="" operator+,="" can="" you="" call="" the="" operator++="" function?="" c.="" for="" the="" second="" implementation="" of="" operator+,="" can="" you="" call="" the="" first="" operator+="" function?="" d.="" together="" these="" two="" functions="" should="" be="" no="" more="" than="" 20="" lines="" of="" code="" in="" the="" bodies.="" if="" you="" make="" it="" longer,="" you="" are="" working="" too="" hard.="" o="" which="" of="" the="" two="" functions="" must="" be="" defined="" as="" a="" non-member?="" why?="" write="" the="" answer="" to="" this="" question="" as="" a="" comment="" in="" your="" main.="" rewrite="" the="" main="" so="" that="" it="" tests="" all="" these="" new="" items.="" display="" the="" new="">
Nov 08, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here