Question: I would like to add the following to my code: Add (Level 1.5) to make the list [automatically] grow when it is necessary. If there
I would like to add the following to my code:
Add (Level 1.5) to make the list [automatically] grow when it is necessary. If there are 10 spots which are all full and a new item is waiting to be placed, the programmer can request that you increase the size of the list before trying to place the new item again. You can make the growth algorithm yourself (i.e. internal to the class) or even let the programmer decide the growth algorithm (i.e. they pass an argument to the growth method that tells how much to increase the list's size).
Add (Level 4) to make your dynamic array member's type into a template. That's right: make the entire class into a template. Cool, hunh?
templist.h
#ifndef TEMPLIST_H_ #define TEMPLIST_H_
#include
struct TemperatureList{ public: double* list; long size; long MAX_SIZE; TemperatureList(int n); void add_temperature(double temperature); bool full() const; long get_size() const; double get_temperature(long position) const; void output(ostream &outs) const; double get_last() const; void detele_last(); };
#endif
templist.cpp
#include "templist.h"
TemperatureList::TemperatureList(int n){ list = new double[n]; MAX_SIZE = n; size = 0; }
void TemperatureList::add_temperature(double temperature){ if (full() == false){ list[size] = temperature; size += 1; } }
bool TemperatureList::full() const{ if (size == MAX_SIZE) return true; return false; }
long TemperatureList::get_size() const{ return size; }
double TemperatureList::get_temperature(long position) const{ if (position >= size || position < 0) return 0.0; return list[position]; }
void TemperatureList::output(ostream &outs) const{ for (long i = 0; i < size; i++) outs << list[i] << endl; }
double TemperatureList::get_last() const{ if (size == 0) return 0.0; return list[size-1]; }
void TemperatureList::detele_last(){ if (size > 0); size -= 1; } main.cpp
#include "templist.h"
int main(){ TemperatureList temp(50); temp.add_temperature(4.0); temp.add_temperature(7.0); temp.add_temperature(8.0);temp.add_temperature(9.6); cout << "Last element is : " << temp.get_last() << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
