I would like to add the following to my code: Add (Level 1.5) to make the list [
ID: 3915562 • Letter: I
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 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 <iostream>
using namespace std;
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;
}
Explanation / Answer
Given below is the modified code for the question. Since its a template class we now add the implementations also in .h file.
Please do rate the answer if it was helpful. Thank you
MyList.h
-------
#ifndef MyList_h
#define MyList_h
#include <iostream>
using namespace std;
template <typename T>
struct MyList{
public:
T* list;
long size;
long MAX_SIZE;
MyList(int n);
void add(const T &value);
bool full() const;
long get_size() const;
T get(long position) const;
void output(ostream &outs) const;
T get_last() const;
void detele_last();
void expand(int capacity);
};
template <typename T>
MyList<T>::MyList(int n){
list = new T[n];
MAX_SIZE = n;
size = 0;
}
template <typename T>
void MyList<T>::add(const T &value){
if (full())
expand(2 * MAX_SIZE); //double its current capacity
list[size] = value;
size += 1;
}
template <typename T>
bool MyList<T>::full() const{
if (size == MAX_SIZE) return true;
return false;
}
template <typename T>
long MyList<T>::get_size() const{
return size;
}
template <typename T>
T MyList<T>::get(long position) const{
T val;
if (position >= size || position < 0) return val;
return list[position];
}
template <typename T>
void MyList<T>::output(ostream &outs) const{
for (long i = 0; i < size; i++)
outs << list[i] << endl;
}
template <typename T>
T MyList<T>::get_last() const{
if (size == 0) return 0.0;
return list[size-1];
}
template <typename T>
void MyList<T>::detele_last(){
if (size > 0)
size -= 1;
}
template <typename T>
void MyList<T>::expand(int capacity){
MAX_SIZE = capacity;
T* temp = new T[MAX_SIZE];
for(int i = 0; i < size; i++)
temp[i] = list[i];
delete[] list;
list = temp;
}
#endif /* MyList_h */
main.cpp
--------
#include <iostream>
#include "MyList.h"
using namespace std;
int main(){
MyList<double> temp(3); //initially only 3 elements
temp.add(4.0);
temp.add(7.0);
temp.add(8.0);
temp.add(9.6); //adding additional element, should expand
cout << "Last element is : " << temp.get_last() << endl;
return 0;
}