# Please help

Printable View

• 07-01-2003
supaben34
Please help
Hello guys,
I was wondering if you could help me out with a class function definition I need to make. Maybe its just getting late but I can't come up with the right definition for the function that returns the location in an integer array of maximum size 20 or 0 if there is no such value. Here's my code.
Code:

``` #ifndef LIST_H #define LIST_H #include <iostream.h> const int SIZE = 20; class List{  public:   List();   void Show();   bool Insert(int value);   bool Insert(int value, int pos);   bool Delete(int pos);   int Size();   int Sum();   float Average();   int Odds();   int HowMany(char value);   int Between(int low,int high);   int WhereIs(int value);   void Clear();   ~List();  private:   int list_array[SIZE];// the container -- an array of 20 integers   int element;// a pointer that keeps track of the position in array }; #endif```
Code:

```int List::WhereIs(int value)   // returns the position of the first occurence of a given value(or returns 0, if not found) {   int num;   int pos_counter = 0;   int i = 0;   while (num != value){     num = list_array[i];     pos_counter++;     i++;   }   return pos_counter; }```
Thanks in advance!
• 07-01-2003
Stoned_Coder
your nearly there. helpful hint is first time thru the loop num is an undefined value.
• 07-01-2003
supaben34
One more function I need help on
hi guys,
I got the WhereIs function(thank for the hint Stoned Code) but now I need your help on another one. Same header file as before but this is an Insert(int, int) function
Code:

```bool List::Insert(int value, int pos)   // inserts a number in the list, possibly in the middle(if room, and if valid position given) {   if (element < SIZE){     int new_element = element;     // shift all values at pos, one element to the right     for(int i = element; i >= pos; i--)       list_array[i + 1] = list_array[i];     // insert new value     list_array[pos] = value;     // increase the size of the array     element = (new_element + 1);     return true;   }   else{     cout << "*** Invalid list position number\n";     return false;   } }```
This function should insert a value at a specified location (if room) if there is no room it should send a false return statement. There's something in here I can't catch Hopefully, you guys can find the error. Thank you.
• 07-01-2003
Zach L.
Does element store the position of the last element, or one past the last position? If it is the former, then (element < SIZE) will still let the size increase too much (by one int).

Also, new_element seems superfluous. ++element would do the same job that new_element does in your function.
• 07-01-2003
supaben34
One more time...
Hi again,
I know what I am doing wrong I just do not know how to track the error and where to find it. My element variable is supposed to keep track of the last element in the array. Maybe my constructor will clarify the point.
Code:

```List::List()   // constructor for list object - has to create the list object and print out message that   // object has been instantiated {   element = 0;   cout << "Creating List Object\n"; }```
I then have to write two functions for Insert. The user has the option to either:
Insert(int value);
OR
Insert(int pos, int value);

My functions are as follows:
Code:

```bool List::Insert(int value)   // Inserts a number at the end of the list(if there is enough room) {   if (element < SIZE){     list_array[element] = value;     element = element++;     return true;   }   else{     cout << "*** Invalid list position number\n";     return false;   } }```
Code:

```bool List::Insert(int pos, int value)   // inserts a number in the list, possibly in the middle(if room, and if valid position given) {   if ((element < SIZE) && (pos > 0)){     // shift all values at pos, one element to the right     for(int i = element; i >= pos; i--)       list_array[i + 1] = list_array[i];       // insert new value       list_array[pos] = value;       // increase the size of the array       element = element++;       return true;   }   else{     cout << "*** Invalid list position number\n";     return false;   } }```
Please let me know if I am even close.... it really is frustrating spending three hours and getting nowhere... any feedback will be most appreciated.
• 07-02-2003
Zach L.
A couple things. The first deals with the ++ operator. What ++ does is actually modify the operand it is working on (that is, you don't need x = x++).

Similarly, x++ and ++x return different things. x++ returns the value of x before it was incremented, while ++x returns the value after it was incremented.

As a consequence, x=x++ doesn't change x at all, while x=++x is equivalent to ++x; followed by x=x. I suspect this is a large part of your problem. You are never really modifying 'element', and hence, overwriting the same value over and over.

Also, your 'element' variable would probably be best keeping track of one-past the last element (at least the way it is setup now). Note that this 'one-part' value is also the same as the current length of the list.

If it is marking the last element in the list, then you need to increment it before writing to that space in the list (otherwise this will overwrite the last element, and then move on). If it is marking length, then you'd assign to that space, and then increment. Also, note that if your marking the last element, than the condition (element < SIZE) will allow you to increment to element==SIZE, and write to that space (which is of course outside the bounds of the array).

One other thing, since an array is 0-based, setting element to 0 in the constructor actually sets it to one-past the last element (which is theoretically at the -1 index, in this case). This is because the list contains no elements, so clearly 0 is not where the last one is.

Hope this helps.
• 07-02-2003
Zach L.
>> there is no separating sequence point

Yes there is. x++ has a higher precedence than ++x which has a higher precedence than x = ...