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

Username:   Password: 
 RegisterRegister   
 how to make this function??
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
appling




PostPosted: Tue Feb 24, 2004 3:13 pm   Post subject: how to make this function??

how could i make this program into a function
code:
var x, y : int
var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 15)
var a : array 1 .. 7 of int
get x
for i : 1 .. 7
    y := 0
    loop
          if x mod prime (i) = 0 then
                x := x div prime (i)
                y := y + 1
          else
                exit
          end if
    end loop
    a (i) := y
end for


for k : 1 .. 7
    put a (k)
end for


i tried a lot, who can help me??? Question Question
Sponsor
Sponsor
Sponsor
sponsor
shorthair




PostPosted: Tue Feb 24, 2004 3:20 pm   Post subject: (No subject)

Function Hello
*
*
*
*
end Hello



Hello
appling




PostPosted: Tue Feb 24, 2004 3:29 pm   Post subject: (No subject)

i dont know how to call the function, so can u write the code and let me have a look?
recneps




PostPosted: Tue Feb 24, 2004 3:49 pm   Post subject: (No subject)

Functions are simple.

code:
procedure ProcedureNameHere

%Code here...
var x, y : int
var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 15)
var a : array 1 .. 7 of int
get x
for i : 1 .. 7
    y := 0
    loop
          if x mod prime (i) = 0 then
                x := x div prime (i)
                y := y + 1
          else
                exit
          end if
    end loop
    a (i) := y
end for


for k : 1 .. 7
    put a (k)
end for
% end code.

end ProcedureNameHere

% then to call, just type the proc name.
ProcedureNameHere

Simple Simple.
appling




PostPosted: Tue Feb 24, 2004 4:02 pm   Post subject: (No subject)

sorry, the teacher told me just use the function not the procedure.so...

thank u all the same
naoki




PostPosted: Tue Feb 24, 2004 4:55 pm   Post subject: (No subject)

The above poster is wrong, that's actually a procedure, not a function

A function goes
code:

function whateverthename (place parameters here) : whatever returning value (can be boolean, integer, string, real)

put code here
should end in
result whatever returning value (i.e. result true, result num ... )

end whateverthename




calling it is different than a procedure. You can't just run it normally. For example, if it returned a boolean answer, you would go

code:

if whatever the name (parameters) = true then
appling




PostPosted: Tue Feb 24, 2004 5:13 pm   Post subject: (No subject)

i tried this
code:
function KKK (var x : int) : array 1 .. 7 of int
    var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 15)
    var a : array 1 .. 7 of int
    var y : int
    for i : 1 .. 7
        y := 0
        loop
            if x mod prime (i) = 0 then
                x := x div prime (i)
                y := y + 1
            else
                exit
            end if
        end loop
        a (i) := y
    end for
end KKK


var X : int
get X
put KKK (X)

but turing said "illegal put item" ???
i dont know where i am wrong
naoki




PostPosted: Tue Feb 24, 2004 5:45 pm   Post subject: (No subject)

i dunno, I don't really feel like running it but I'm guessing that Turing won't let you have a whole ARRAY as your returning value. I mean, think about it: Turing has NO idea how you want to output or result an array. Do you want to add/catenate the values? Do you want them output on the same line or seperate lines? Ambiguous cases aren't Turing's specialty

also, if you look in your function you never declared "result" and then whatever you wanted. You just put exit. That means that it's supposed to return something, but you're not telling it what.

Change the resulting value, and make sure you have one in your program. If you want to return an array value, that could work, but not the entire array
shorthair




PostPosted: Tue Feb 24, 2004 5:49 pm   Post subject: (No subject)

you need to result somthing ,
appling




PostPosted: Tue Feb 24, 2004 5:50 pm   Post subject: (No subject)

what i need to do if i want the whole array(array a) as my result?
and how to call it?
Thuged_Out_G




PostPosted: Tue Feb 24, 2004 10:38 pm   Post subject: (No subject)

your program doesnt output anything, it just does some variable changes if some conditions are true, so it would make more sense to make the program a procedure with parameters instead of a function.
recneps




PostPosted: Wed Feb 25, 2004 6:12 pm   Post subject: (No subject)

oops! haha, sorry there, i kinda started on functions and then got interrupted, and kinda changed to procedure :\
appling




PostPosted: Wed Feb 25, 2004 9:28 pm   Post subject: (No subject)

after a long time trying, i work it out at last Very Happy Very Happy Very Happy
code:

function get_fators (x : int) : array 1 .. 7 of int
    var prime : array 1 .. 7 of int := init (2, 3, 5, 7, 11, 13, 17)
    var y, z : int
    z := x
    var a : array 1 .. 7 of int
    for i : 1 .. 7
        y := 0
        loop
            if z mod prime (i) = 0 then
                z := z div prime (i)
                y := y + 1
            else
                exit
            end if
        end loop
        a (i) := y
    end for
    result a
end get_fators


var X : int
get X

for i : 1 .. 7
    put get_fators (X) (i)
end for

is this right/?

thanks here to all the people Exclamation Exclamation
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  [ 13 Posts ]
Jump to:   


Style:  
Search: