By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

help to add sumation part in my program that i have

P: 11
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():size(0) {
}
bool List::isEmpty() const
{
return bool (size==0);
}

int List::getLength() const
{
return size;
}

void List::insert(int 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(position+1)]=items [translate(position)];
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
Sep 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: 23
I'd just do a simple for loop;

First, create a temporary variable of whatever the type is for the list of items and initialize it to 0, or some appropriate value. Then, starting at the first list element,

Temp = Temp + ListItem[index];
or just Temp += ListItem[index];

Once the for loop is finished, exit from the function Summation by returning the value of the temp variable you created.

int List::Summation ( ) {
int Temp = 0;
for ( int c = 0; c < MAX_INT; c++ )
Temp += ListItem[c];

return Temp;
}
Sep 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.