Class arrays.

Nov 25, 2013 at 12:35pm
Hi.
The main code of the program is not to be changed.
The questions I have are that I am trying to add list1 and list2 together and put it into list 3 with operator +. It is not working for me.
Other thing is bool full (), It is supposed to stop at 20 as that is max size, but it goes over that and does not go to the bool full if it exceeds 20.

 
Last edited on Nov 25, 2013 at 2:18pm
Nov 25, 2013 at 12:43pm
In operator + you do not update temp.staerd which remains 0.

The simpliest solution would be this:
1
2
3
4
5
6
7
8
9
10
IntList operator +(const IntList& list1, const IntList& list2)
{
  IntList temp;
  temp.staerd = max(list1.staerd, list2.staerd);
  for(int i = 0 ; i < temp.staerd ; i++)
  {
      temp.fylki[i] = list1.fylki[i] + list2.fylki[i];
  }
  return temp;
}
Nov 25, 2013 at 12:49pm
Hi,
thanks for that. That did work.
But now in the output, it goes further,
it should output 0 3 6 9 12
but it does 0 3 6 9 12 then random numbers until it reaches a total of 10 outputted.
Last edited on Nov 25, 2013 at 12:52pm
Nov 25, 2013 at 12:53pm
The replace max with min
Nov 25, 2013 at 1:11pm
That did the trick.

Do you have any input on the bool full(), on why it doesn´t work for me.
The max for the array size is 20, but when the numbers from 10 to 25 are added it does not activate the bool full function. Instead it just outputs the 25 numbers, instead of 20 and activating the bool full.
Nov 25, 2013 at 2:06pm
bool full() isn't the problem. The problem is add(int i). You add values regardless whether the limit is exceeded or not. You need to check if it is allowed to add something before you actually do it.
Nov 25, 2013 at 2:17pm
Hi.
I found out the problem with the bool and it all works. Thanks for the assistance.
Topic archived. No new replies allowed.