Posted: Thu Mar 27, 2003 9:50 pm Post subject: LCD,GCM
i wanted to know whether there was any posts on GCD and LCM. i think i saw it. but i browsed thorugh a few pages of the post but coudnt find it. did anyone had any post about GCD and LCM ?
Sponsor Sponsor
Tony
Posted: Thu Mar 27, 2003 10:07 pm Post subject: (No subject)
no, there were not posts about them before... but I've found a nice site explaining how to program your own algorythms on them
Posted: Sat Mar 29, 2003 7:16 pm Post subject: (No subject)
i dont know much about function. i saw the tutorials. but it didnt give me any help. I sort of did how much i understood. i know that there are lot of errors . i cant figure out how to get rid of them.
code:
function getpos () : int
var m : int
var n : int
put "Please enter a number"
get m
put ""
put "Please enter another number"
get n
end getpos
function GCD (n: int, m: int) : int
var a, b, t : int
getpos
a:= m
b := n
loop
t := a mod b
exit when t = 0
GCD := b
a := b
b := t
end loop
end GCD
function LCM () : int
getpos
LCM (m, n) = (m + n) / gcd (m, n)
end LCM
function prime (n: int, m: int) : boolean
GCD(m,n) = 1
end prime
getpos
put GCD
put LCM
Tony
Posted: Sat Mar 29, 2003 7:45 pm Post subject: (No subject)
function returns a value, so it must have result followed by a value. In your case, you want to use a procedure instead.
I'm sorry that tutorial didnt help you much... is there anything I can improve in it?
Posted: Sun Mar 30, 2003 12:25 am Post subject: (No subject)
But i have to use functions. i cant use procedures yet. So can u check the code again and see if u tell me what is wrong
The tutorial can u a bit more example codes and a bit long ones like which use atleast 3 functions/ procedures (i guess) followed by what the program is doing - like what it the innput and the output stuff.
yuethomas
Posted: Sun Mar 30, 2003 1:52 am Post subject: (No subject)
All functions must return values. So, what you can do is to return a dummy value (that doesn't actually accomplish anything), like:
code:
function a (b : int) : int
...
result 0
end a
What you attach after result is really up to you. Conventionally, it is put as 0 or 1 (to indicate success) and -1 (to indicate failure).
MysticAngel
Posted: Sun Mar 30, 2003 2:30 am Post subject: (No subject)
Dp u gave to sat like result = (something)?
azndragon
Posted: Sun Mar 30, 2003 9:49 am Post subject: (No subject)
Okay, let me use an example from my game:
code:
function caps (word : string) : string %Converts a word into pure caps, so that the inputs can be more easily recognized.
word2 := ""
for i : 1 .. length (word)
if ord (word (i .. i)) > 96 then
if ord (word (i .. i)) < 123 then
word2 := word2 + chr (ord (word (i)) - 32)
else
word2 := word2 + chr (ord (word (i)) + 0)
end if
else
word2 := word2 + chr (ord (word (i)) + 0)
end if
end for
result word2
end caps
First thing is the function name. That should be pretty simple. The variable in the brackets is the text that you input when you call the function. If you use the code:
caps(randomword)
The value stored in randomword will be sent to the function under the variable name of word. The middle section of the function can pretty much be ignored, since you can change it to whatever you want to do. However, notice that in the function, a new variable, called word2 is being created, based on the word variable. At the end of the function is "result word2" This is the final result of function. This will be set as a new variable if you call it sometime later. Ex.
code:
var someword : string
var newword : string
put "Enter a word"
get someword
newword := caps (someword)
put newword
Lets say the user enters the word "Hello". The function will run through it's course, and word2 will now be "HELLO". The result is given, and the final output of the program is "HELLO". Hope this helps.
Sponsor Sponsor
MysticAngel
Posted: Mon Mar 31, 2003 4:31 pm Post subject: (No subject)
k sort og know what functions are and how they work and stuff. but i am not sure whether my code is ok or not. plz check it, cuz there are a few errors and i cant figure them out. plz tell me what are are the mistakes/ solutions
MysticAngel
Posted: Mon Mar 31, 2003 9:49 pm Post subject: (No subject)
code:
function getPositiveInteger : int
var n : int
loop
get n
exit when n > 0
put " A positive value please :"
end loop
result n
end getPositiveInteger
%------------------------------------------------------
function GreatestCommonDivisor (n : int) : int
var greatest,t, a , b,m : int
m:= getPositiveInteger
a := m
b := n
loop
t := a mod b
exit when t < 1
a:= b
b:= t
result b
end loop
end GreatestCommonDivisor
%------------------------------------------------------
function LeastCommonMultiple (m,n : int) :int
var lcm : int
lcm := (m*n) / GreatestCommonDivisor(m,n)
result lcm
end LeastCommonMultiple
%-----------------------------------------------------
function relativelyprime(m,n : int) : boolean
if GreatestCommonDivisor = 1 then
relativelyprime = true
else
relativelyprime = false
end if
result relativelyprime (m,n)
end relativelyprime
%-----------------------------------------------------------
var num1, num2,a1, b1 : int
var c1 : boolean
loop
put "Please enter a number"
num1:= getPositiveInteger (n)
put "Please enter another number"
num2 := getPositiveInteger(m)
put "GCD = "
a1:= GreatestCommonDivisor
put a1
put "LCM = "
b1 = LeastCommonMultiple (m,n)
put b1
put "Relatively prime"
c1 := relativelyprime
put c1
end loop
Tony
Posted: Mon Mar 31, 2003 10:47 pm Post subject: (No subject)
Posted: Mon Mar 31, 2003 11:16 pm Post subject: (No subject)
I was supposed to attached the code to the previous reply. i forgot to.
k sort og know what functions are and how they work and stuff. but i am not sure whether my code is ok or not. plz check it, cuz there are a few errors and i cant figure them out. plz tell me what are are the mistakes/ solutions
[/code]
function getPositiveInteger : int
var n : int
loop
get n
exit when n > 0
put " A positive value please :"
end loop
result n
end getPositiveInteger
%------------------------------------------------------
function GreatestCommonDivisor (n : int) : int
var greatest,t, a , b,m : int
m:= getPositiveInteger
a := m
b := n
loop
t := a mod b
exit when t < 1
a:= b
b:= t
result b
end loop
end GreatestCommonDivisor
%------------------------------------------------------
function LeastCommonMultiple (m,n : int) :int
var lcm : int
lcm := (m*n) / GreatestCommonDivisor(m,n)
result lcm
end LeastCommonMultiple
%-----------------------------------------------------
function relativelyprime(m,n : int) : boolean
if GreatestCommonDivisor = 1 then
relativelyprime = true
else
relativelyprime = false
end if
result relativelyprime (m,n)
end relativelyprime
%-----------------------------------------------------------
var num1, num2,a1, b1 : int
var c1 : boolean
loop
put "Please enter a number"
num1:= getPositiveInteger (n)
put "Please enter another number"
num2 := getPositiveInteger(m)
put "GCD = "
a1:= GreatestCommonDivisor
put a1
put "LCM = "
b1 = LeastCommonMultiple (m,n)
put b1
put "Relatively prime"
c1 := relativelyprime
put c1
end loop
Prince
Posted: Tue Apr 01, 2003 11:38 am Post subject: (No subject)
whoa, u got a shit load of errors there... ive got the same program at home (i think... i hope )... r u in the gr 11 comp sci class?
Dan
Posted: Tue Apr 01, 2003 4:41 pm Post subject: (No subject)
The function GreatestCommonDivisor only takes one number but it is beiing called with 2. also you some times calling it with no numbers wich dose not wrok.
also in turing you use := not = when asing a vlaue you messed that up a few times
then you send a vlaue, n to the a fuction that has no vlaue and you do not input the users repose to put in a number.
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
MysticAngel
Posted: Tue Apr 01, 2003 6:13 pm Post subject: (No subject)
yes i am in grade 11. neways i cahnged some stuff and it works now but theere are some problems in the output. k when u run the code. it works correctly, but after u enter the two numbers, u have to enter another number then u would get the GCD and then again u would have to enter another number even when it is not asking for then you would get the other results...
code:
function getPositiveInteger : int
var n : int
loop
get n
exit when n > 0
end loop
result n
end getPositiveInteger
%------------------------------------------------
function GreatestCommonDivisor (m, n : int) : int
var greatest, t, a, b, c : int
c := getPositiveInteger
a := c
b := n
loop
t := a mod b
exit when t < 1
a := b
b := t
result b
end loop
end GreatestCommonDivisor
%------------------------------------------------
function LeastCommonMultiple (m, n : int) : int
var lcm : int
lcm := (m * n) div GreatestCommonDivisor (m, n)
result lcm
end LeastCommonMultiple
%------------------------------------------------
function relativelyprime (m, n : int) : boolean
var bool : boolean := false
if GreatestCommonDivisor (m, n) = 1 then
bool := true
end if
result bool
end relativelyprime
%-----------------------------------------------------------
var num1, num2, a1, b1 : int
var c1 : boolean
loop
put "Please enter a number"
num1 := getPositiveInteger
put "Please enter another number"
num2 := getPositiveInteger
a1 := GreatestCommonDivisor (num1, num2)
put "GCD = ", a1
b1 := LeastCommonMultiple (num1, num2)
put "LCM = ", b1
c1 := relativelyprime (num1, num2)
put "Relatively prime", c1
end loop