
-----------------------------------
umaza
Thu Oct 27, 2011 4:46 pm

Turing Assignment
-----------------------------------
What is it you are trying to achieve?
I have an assignment (I am a noob), I have to make 5 programs, but I need help for one of them:
3. a) Input two integers at which to start and stop. Output the sum and average of all numbers between the two integers (including integers inputed)
    b) Do same as above with "loop" command
    c) Do same as above with "if ... then" and counter(s)

What is the problem you are having?
I don't know how to add all numbers in between (including two inputed integrers)


Describe what you have tried to solve this problem
I tried  code below


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
I tried this code without loops or if statementd

var x, y, v : int

put "Enter first number:"
get x
put "Enter second number:"
get y

v := x .. y

put "The sum of all the numbers is", v + v
put "The average of all the numbers is", v/x .. y

Please specify what version of Turing you are using
4.1.1a

-----------------------------------
Tony
Thu Oct 27, 2011 5:48 pm

RE:Turing Assignment
-----------------------------------
That's... not even a valid syntax. The code doesn't compile, does it?

Since you need to do all 3 different implementations anyway, you could start with one that will be the easiest for you, and work on the rest after. How would you do it with a loop?

If you get really stuck on the solution without any loops and if statements, you could ask Carl Friedrich Gauss if he has any thoughts on this problem.

-----------------------------------
crossley7
Thu Oct 27, 2011 5:56 pm

RE:Turing Assignment
-----------------------------------
give yourself an example on paper and figure out the sum/average manually.  Do a couple of examples and look for a pattern.  The one for average should be really easy, and you can use that to find the sum. (number of numbers in range*average)

The first part seems to be asking you to solve it mathematically and then write a program that does that math

-----------------------------------
Beastinonyou
Thu Oct 27, 2011 6:34 pm

Re: Turing Assignment
-----------------------------------
They're all really easy if you think about it for a moment.


Someone tells you they want you to find the sum of numbers 5 to 10. This means you add 5 + 6 + 7 + 8 + 9 + 10.

5 is their start, 10 is their stop. How do you think you could go about finding a way to add numbers, starting at 5, ending at 10? a For repetitive structure is by far the easiest to accomplish this task. However, using a regular loop should be just as easy, and with if's, it's just a bunch of decisions. Computers are good at making decisions.

As for the average, you need the sum, and the amount of numbers you added to get the sum. So it would be, 10 - 5, = 5. You added 5 numbers together. Divide the sum by 5, you get an average. Example : Start - 32, Stop - 40.  Add up numbers from 32 to 40, and divide by 8, because 40 - 32 = 8.

-----------------------------------
umaza
Thu Oct 27, 2011 8:59 pm

Re: Turing Assignment
-----------------------------------
Sorry, I am a big noob, and I need to know how to add up the range

For example: 


var x, y : int 

get x, y 

for v : x .. y 
end for 


How would I add up the values in between x and y 

For example: 
You input: 
x = 10 
y = 20 

The how do you add the numbers in between such as: 
v = 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 (= 165)

-----------------------------------
mirhagk
Thu Oct 27, 2011 9:08 pm

RE:Turing Assignment
-----------------------------------
Well you will probably want a variable to store the total, and add to this each time in the for loop.

-----------------------------------
crossley7
Thu Oct 27, 2011 9:38 pm

RE:Turing Assignment
-----------------------------------
(x+y)/2 is your average and you can multiply it by abs(x-y)+1 to get the sum.

note: it is abs(x+y)+1 since the difference gives the range, but it includes both values so you add 1.  think 1-5 difference is 4, but there are 5 values.  abs means you just take that positive value of the difference.

Look at any sequence of consecutive integers.  The average will always be first + last divided by 2

-----------------------------------
Beastinonyou
Fri Oct 28, 2011 6:51 am

Re: Turing Assignment
-----------------------------------
I'll give you a hint for adding the numbers from:   x .. y


var x, y : int

get x, y

for i : x .. y
     put i, ", " ..
end for


-----------------------------------
umaza
Fri Oct 28, 2011 8:31 am

Re: Turing Assignment
-----------------------------------
I got 3a, I did:


var x, y, v, a : real

put "Enter first number:"
get x
put "Enter second number:"
get y

v := (x+y)/2

a := v * abs(x-y)+v


put "The sum of the range of ", x, " and ", y, " is ", a 
put "The average of the range of ", x, " and ", y, " is ", v


I will try it with loops next  :)

-----------------------------------
umaza
Fri Oct 28, 2011 3:40 pm

Re: Turing Assignment
-----------------------------------
Well have tried without success   :( 

var x, y : int
var v, a : real

put "Enter first number:"
get x
put "Enter second number:"
get y

for i : x .. y 
     put i, ", " .. 
     a := i
end for


I have tried stuff like "a := i + i" and "a:= i + ..", each time I fail   :wall:

-----------------------------------
Tony
Fri Oct 28, 2011 3:52 pm

Re: Turing Assignment
-----------------------------------
Are you just randomly throwing expressions in, to see if anything sticks?

You know what the calculation is supposed to look like:
v = 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 (= 165)
and you have
I have tried stuff like "a := i + i"
in the first iteration "i" will print to have the value 10, so then your next line
a := i + i
evaluates to
a := 10 + 10

does that look like any part of
v = 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 (= 165)
?


and "a:= i + .."
".." is not a number, so that would not even compile. What expression were you thinking of there?

-----------------------------------
umaza
Fri Oct 28, 2011 5:13 pm

Re: Turing Assignment
-----------------------------------
@Tony

Yes I have tried random expressions to see if anything will work, but I have also thought of some which I can't even do.
I have tried:

var x, y : int
var v : real
 
put "Enter first number:"
get x
put "Enter second number:"
get y

for i : x .. y
v := x + 1
exit when v < y
end for

put x + v + y



This basically does x + (x + 1) + y 