Simple Math Problem

Get help using Construct 2

Post » Mon May 13, 2013 12:56 am

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???
B
10
Posts: 30
Reputation: 940

Post » Mon May 13, 2013 1:38 am

@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?

[CODE](MYArray.At(0) + MYArray.At(1) + MYArray.At(2) + MYArray.At(3) + MYArray.At(4)) /5[/CODE]                                 OrangeTapioca2013-05-13 01:41:09
B
40
S
10
G
5
Posts: 102
Reputation: 6,602

Post » Mon May 13, 2013 2:16 am

A + B + C + D / X is not the same as (A + B + C + D)/ X
B
10
S
3
Posts: 102
Reputation: 1,888

Post » Mon May 13, 2013 3:46 am

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.
B
10
Posts: 30
Reputation: 940

Post » Mon May 13, 2013 3:49 am

@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.
B
10
S
3
Posts: 102
Reputation: 1,888

Post » Mon May 13, 2013 4:04 am

ImageImageImage
B
67
S
19
G
122
Posts: 3,201
Reputation: 73,389

Post » Mon May 13, 2013 4:05 am

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

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

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

Why is it MYArray not MyArray ??
B
39
S
12
G
6
Posts: 136
Reputation: 6,322

Post » Mon May 13, 2013 2:09 pm

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 cant find an answer.


[/IMG]

B
10
Posts: 30
Reputation: 940

Post » Tue May 14, 2013 12:42 am

You should post your CAPX to speed this up, but if your 1 is set as a sting, accidentally, you will get this result.
ImageImageImage
B
67
S
19
G
122
Posts: 3,201
Reputation: 73,389

Post » Tue May 14, 2013 12:52 am

[QUOTE=blackhornet] You should post your CAPX to speed this up, but if your 1 is set as a sting, accidentally, you will get this result.[/QUOTE]

yeah his 1 isn't a number if you add it with a calculator. it should be about 5.58
B
40
S
10
G
5
Posts: 102
Reputation: 6,602

Next

Return to How do I....?

Who is online

Users browsing this forum: Braus, Yahoo [Bot] and 14 guests