help to add sumation part in my program that i have

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anjola
    New Member
    • Sep 2006
    • 11

    help to add sumation part in my program that i have

    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
  • m013690
    New Member
    • Sep 2006
    • 23

    #2
    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;
    }

    Comment

    Working...