Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing Assignment
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
umaza




PostPosted: Thu Oct 27, 2011 4:46 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Oct 27, 2011 5:48 pm   Post subject: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
crossley7




PostPosted: Thu Oct 27, 2011 5:56 pm   Post subject: 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




PostPosted: Thu Oct 27, 2011 6:34 pm   Post subject: 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




PostPosted: Thu Oct 27, 2011 8:59 pm   Post subject: Re: Turing Assignment

Sorry, I am a big noob, and I need to know how to add up the range

For example:

Turing:

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




PostPosted: Thu Oct 27, 2011 9:08 pm   Post subject: 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




PostPosted: Thu Oct 27, 2011 9:38 pm   Post subject: 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




PostPosted: Fri Oct 28, 2011 6:51 am   Post subject: Re: Turing Assignment

I'll give you a hint for adding the numbers from: x .. y

Turing:

var x, y : int

get x, y

for i : x .. y
     put i, ", " ..
end for
Sponsor
Sponsor
Sponsor
sponsor
umaza




PostPosted: Fri Oct 28, 2011 8:31 am   Post subject: Re: Turing Assignment

I got 3a, I did:

Turing:

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 Smile
umaza




PostPosted: Fri Oct 28, 2011 3:40 pm   Post subject: Re: Turing Assignment

Well have tried without success Sad

Turing:
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 Hit Wall
Tony




PostPosted: Fri Oct 28, 2011 3:52 pm   Post subject: 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:
umaza @ Thu Oct 27, 2011 8:59 pm wrote:
v = 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 (= 165)

and you have
code:

put i, ", " ..

to let you know what the values of variables are. Good. Grab that value and see what you are doing. Lets try 10 to 20
umaza @ Fri Oct 28, 2011 3:40 pm wrote:
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
umaza @ Thu Oct 27, 2011 8:59 pm wrote:
v = 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 (= 165)

?


umaza @ Fri Oct 28, 2011 3:40 pm wrote:
and "a:= i + .."

".." is not a number, so that would not even compile. What expression were you thinking of there?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
umaza




PostPosted: Fri Oct 28, 2011 5:13 pm   Post subject: 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:

Turing:
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 <<< x + 1 is v

I have tried to find a sum expression or function, but have had no luck, also is there a when expression, do I need a procedure or function, and is it possible to give each value of the range to a variable?

My teacher wanted me to read this (link below) up to loops and for loops:
http://compsci.ca/v3/viewtopic.php?t=8808

I have read it, yet I can't figure out how to do this.
Tony




PostPosted: Fri Oct 28, 2011 6:14 pm   Post subject: Re: Turing Assignment

umaza @ Fri Oct 28, 2011 5:13 pm wrote:
I have tried to find a sum expression or function

There isn't one, because you don't need it. You have not been using any sum expression in
umaza @ Thu Oct 27, 2011 8:59 pm wrote:
v = 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 (= 165)

only the + operator. Which is available.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Beastinonyou




PostPosted: Fri Oct 28, 2011 7:24 pm   Post subject: Re: Turing Assignment

I think you really need to review how For repetitive structures work.


When I gave you the example:

Turing:

var x, y : int

get x, y

for i : x .. y
     put i, ", " ..
end for


this was to show you what the 'i' variable would be used for. I didn't mean that the line 'put i, ", " ..' was to be incorporated.


how about if I make this Even MORE obvious ?

Turing:

var start, stop, sum : int

put "Enter The Starting Number: " ..
get start

put "Enter The Finishing Number: " ..
get stop

put skip
put "You Want To Add These Numbers!!!!!!!!"
for counter : start .. stop
    put counter
end for
put ""
put "Woah, How Did I Display All The Numbers Between 'Start' and 'Stop'?"
put "Maybe I Should Create A Variable, called 'sum' and add these Values, "
put "Rather than Displaying them. Hmmmm... How do I add numbers to a Variable? =P"


Just run that code in Turing. Should be a very obvious hint.
umaza




PostPosted: Fri Oct 28, 2011 10:33 pm   Post subject: Re: Turing Assignment

Thanks, I reviewed for loops, and I finished 3c

Turing:
var x, y : int
var a, v : real

put "Input first number:"
get x

put "Input second number:"
get y

a := 0
v := (x+y)/2

if x<y then
for i : x .. y
   a := a + i
end for
else if y<x then
for i : y .. x
   a := a + i
end for
end if
end if
 
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 3b tomorrow, with loops (not for loops)

EDIT:
I got 3b Smile

I got it to work,

Turing:
var x, y : int
var a, b, v  : real

put "Input first number:"
get x

put "Input second number:"
get y

b := 0

a := x - 1
loop
a := a + 1
b := b + a
exit when a = y
end loop

put "The sum of the range of ", x, " and ", y, " is ", b


Only one problem, if I make x a greater value, and y the smaller, then it won't work, is there a way I can fix that without if statements?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: