I have gone so far entering the int, creating the list and displaying. How do I do thesummation part.
//# include <iostream.h>
//#include <conio.h>
//# include <iomanip.h>
# include "ListA.h"
List::List():si ze(0) {
}
bool List::isEmpty() const
{
return bool (size==0);
}
int List::getLength () const
{
return size;
}
void List::insert(in t index, const ListItemType & newItem, bool & success)
{
success=(index> =1) && (index<=size+1) && (size<MAX_LIST) ;
if (success)
{
for (int position=size; position>=index ; --position)
items [translate(posit ion+1)]=items [translate(posit ion)];
items[translate(index )]=newItem;
++size;
}
}
void List::retrieve( int index, ListItemType & DataItem, bool & success)const
{
success=(index> =1) && (index<=size);
if (success)
DataItem=items[translate(index )];
}
# ifndef ListA.h
# define ListA.h
const int MAX_LIST=30;
typedef int ListItemType;
ListItemType items [MAX_LIST];
int size;
class List
{
public:
List();
bool isEmpty()const;
int getLength() const;
void insert (int index, const ListItemType & newItem, bool& success);
void retrieve (int index, ListItemType & DataItem, bool& success) const;
private:
ListItemType Items[MAX_LIST];
int size;
//int sum;
int translate(int index)const;
};
#endif
//# include <iostream.h>
//#include <conio.h>
//# include <iomanip.h>
# include "ListA.h"
List::List():si ze(0) {
}
bool List::isEmpty() const
{
return bool (size==0);
}
int List::getLength () const
{
return size;
}
void List::insert(in t index, const ListItemType & newItem, bool & success)
{
success=(index> =1) && (index<=size+1) && (size<MAX_LIST) ;
if (success)
{
for (int position=size; position>=index ; --position)
items [translate(posit ion+1)]=items [translate(posit ion)];
items[translate(index )]=newItem;
++size;
}
}
void List::retrieve( int index, ListItemType & DataItem, bool & success)const
{
success=(index> =1) && (index<=size);
if (success)
DataItem=items[translate(index )];
}
# ifndef ListA.h
# define ListA.h
const int MAX_LIST=30;
typedef int ListItemType;
ListItemType items [MAX_LIST];
int size;
class List
{
public:
List();
bool isEmpty()const;
int getLength() const;
void insert (int index, const ListItemType & newItem, bool& success);
void retrieve (int index, ListItemType & DataItem, bool& success) const;
private:
ListItemType Items[MAX_LIST];
int size;
//int sum;
int translate(int index)const;
};
#endif
Comment