Simple Math Problem

  • This is driving me crazy, I have been looking around trying to solve it with no joy.

    I am trying to get the average of five values. I created an array then added the values and divided by 5:

    System/Every Tick - System Set MyVar to MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4) /5

    System/Every Tick - TextAv/Set text to MyVar

    All works perfectly apart from the divide by five part, the five values are just added together. I have tried every combination of brackets but nothing works - what am I doing wrong???

  • weisdaclick are you sure MYArray.At(4)/5 didnt get divided first and then the numbers were added?

    or does your statement look like this?

    (MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4)) /5
  • A + B + C + D / X is not the same as (A + B + C + D)/ X

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 100% sure the divide is not working, I am only using very simple numbers right now. This makes me think I can't do it like this for some reason.

    To test I changed the divide and tried adding another number or multiplying - nothing works, only the first part of the statement (adding the values in the array).

    I tried:

    MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4) /5

    (MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4)) /5

    (MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4) /5)

    (MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4) /5)

    (MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4) /(5))

    MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4) / MYArray.Width

    Stuck.

  • weisdaclick

    The second line you wrote (MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4)) /5 is correct. You have to force the additions to take place first before the division is applied. I guarantee that. As for why it's not working, I can't tell you whats wrong without looking at your exact code or syntax.

  • due to order of operations (BEDMAS) you would need to add all together then do division

    agree with that second line is a sound equation but might be your coding.

    Sorry also like said without capx or screenshot can't give definite answer

    Why is it MYArray not MyArray ??

  • Thanks for the responses so far.

    Here is a screenshot, there really is nothing else. You can see another way I have tried it with a local variable.

    I have removed the / 5 to test and the results are not correct and I can't work out why. The black image is the test result with TextTest at the top with TextTest1 next and so on. At the bottom is TextAv but the added value is never quite right which has me stumped. You can see here it is out by one.

    I have read everything I can online about arrays and can�t find an answer.

    [/IMG]

  • You should post your CAPX to speed this up, but if your 1 is set as a sting, accidentally, you will get this result.

  • You should post your CAPX to speed this up, but if your 1 is set as a sting, accidentally, you will get this result.

    yeah his 1 isn't a number if you add it with a calculator. it should be about 5.58

  • I got it working now.

    I don't know when you push/pop values to the array if it expands the size of the array but on the previous event sheet I added this line:

    System On end of layout      myArray Set size to (5,1,1)

    After I did that it works perfectly.

    Thanks for all the help.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)