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

Username:   Password: 
 RegisterRegister   
 How do you compare a bunch of different numbers from a loop?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
varman




PostPosted: Wed Feb 01, 2012 8:30 pm   Post subject: How do you compare a bunch of different numbers from a loop?

What is it you are trying to achieve?
I need to compare a bunch of different numbers from a loop in order to show which gave the smallest output of an equation.


What is the problem you are having?
I don't know how to compare the numbers. Basically, the question asks to compute values of the function f(x) = 3x2 ? 2x+1 in steps of 0.1 between x=0 and x=1 inclusive and find the
value of x for which f(x) is a minimum. So I am having trouble with the last part.


Describe what you have tried to solve this problem
I tried to think of a solution but couldn't.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<var fx : real := 0.0
var ans : real
loop
    fx := fx + 0.1
    ans := 3 * (fx ** 2) - 2 * fx + 1
    put " When x is ", fx, " the answer is ", ans
    exit when fx > 0.9
end loop
if fx < fx then
put "The answer was the smallest when f(x) was "
end if
>



Please specify what version of Turing you are using
4.1
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Wed Feb 01, 2012 9:36 pm   Post subject: Re: How do you compare a bunch of different numbers from a loop?

Perhaps you could keep track of the minimum value as you iterate over your input. Then for each input, you can compare the value of the function to the minimum.
varman




PostPosted: Wed Feb 01, 2012 10:23 pm   Post subject: RE:How do you compare a bunch of different numbers from a loop?

But there is no input. Everything happens without the user typing in anything
DemonWasp




PostPosted: Wed Feb 01, 2012 10:48 pm   Post subject: RE:How do you compare a bunch of different numbers from a loop?

So...keep track of the minimum value as you loop over the values of x.
varman




PostPosted: Wed Feb 01, 2012 11:26 pm   Post subject: RE:How do you compare a bunch of different numbers from a loop?

How do I do that? Is there a variable automatically assigned to every value of x?
DemonWasp




PostPosted: Thu Feb 02, 2012 9:12 am   Post subject: RE:How do you compare a bunch of different numbers from a loop?

Well, right now you have the value of x (which increases by 0.1 and so on) in a variable called 'fx' (not sure why). You have the value of f(x), which I would ordinarily call 'f', as 'ans'.
Velocity




PostPosted: Tue Feb 07, 2012 5:34 pm   Post subject: RE:How do you compare a bunch of different numbers from a loop?

so at the beginning of the loop state the new fx value so that it tracks it ever time it executes the loop
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  [ 7 Posts ]
Jump to:   


Style:  
Search: