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

Username:   Password: 
 RegisterRegister   
 Procedures and passing arrays. How?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ahsonmonis




PostPosted: Thu Apr 19, 2007 7:35 am   Post subject: Procedures and passing arrays. How?

Hi, i am just wondering if i can get some help on the concept of procedures, functions, how i set them up, how i pass arrays through them, and how to do swaps? Thanks alot.
Sponsor
Sponsor
Sponsor
sponsor
rollerdude




PostPosted: Thu Apr 19, 2007 1:41 pm   Post subject: Re: Procedures and passing arrays. How?

um... did you try going to the walkthrough forum?

otherwise

code:

var y : array 1 .. 2 of int
for i : 1 .. 2
    y (i) := Rand.Int (1 , 2)
end for
proc testarray (var x : array 1 .. 2 of int)
    for i : 1 .. 2
        x (i) := y (i)
        put x (i)
    end for
end testarray
testarray(y)



spmething like that?
Clayton




PostPosted: Thu Apr 19, 2007 3:41 pm   Post subject: Re: Procedures and passing arrays. How?

Please don't post two topics on the same subject, there's a delete button for a reason. Although, welcome to CompSci.ca!

Here's the reply from Draken Zeil:

Draken Zeil wrote:
A function is really simple.

You know you can define variables as ints...

int x;

You can assign it a value.

x=5;

A function is a block of code that returns a value. It can take arguments.

So, let's make function Func, and have it return a value of type INT.

int Func()
{return 5;}

x=Func();

X will now equal 5.

With an argument:

int Func2(int a)
{return a;}

x=Func2(5);
X will now equal 5.

int Func3(int a, int b)
{return (a+b);}

x=Func3(2,3)
X will again equal 5.


Your other post has been deleted
ahsonmonis




PostPosted: Fri Apr 20, 2007 12:50 pm   Post subject: Re: Procedures and passing arrays. How?

I didn't realize you could delete your posts. I will be more careful in the future. Anyways, i am still a bit confused on procedures and how you pass arrays through them. I understand the swap concept.

I think my Lotto 6 49 post was deleted so I will add it here. I need help with an assignment for turing. We need to stimulate the idea of the lottery 6 49 and use certain procedures to do so. First, we have the userpick (which gets the user's numbers). Then, we have the drawnum (which shuffles the numbers from 1-49 so it is more random when picked). Then, we have winningnum (which picks the 6 winning numbers after they have been shuffled). Then, we have comparenum (which compares the userpick and winningnum). Finally, we have prize (which calculates winnings by 10^x where x is number of same numbers. In addition to that, we need to make sure that all picks are different for both userpick and computerpick. And we need to sort both list of numbers in ascending order. It doesn't matter what order the numbers are. I have attatched the assignment. I will not copy the code, just use it as a guide to understand what we have to do. Thanks a lot. Wink



TIK_assignment.bmp
 Description:
 Filesize:  25.25 KB
 Viewed:  96 Time(s)

TIK_assignment.bmp


ahsonmonis




PostPosted: Fri Apr 20, 2007 1:11 pm   Post subject: Re: Procedures and passing arrays. How?

I realized that i cannot ask you to write the whole program for me. I will be using this thread as a discussion on each procedure that I will complete and post. I will try to finish my program by monday. I will do it step by step and any extra add-ons or advice to make the program better would be helpful. If i run into difficulty, i will consult here. So, hopefully i can understand this assignment and do it successfully.

Here is the first step of my program: Sorting the numbers the user enters in ascending order. I have two questions: How do I make sure they can't enter the same number more than once? How do I limit the choice to be integers between 1 and 49 inclusive?

Turing:

procedure sortList
    var count:int:=6

    var list: array 1..count of int
    put "Please enter your six numbers. "
    for i:1..count
        get list(i)
    end for
    var sortList: array 1.. count of int
    for i:1..count
        var smallest:= 999
        var where:int
            for j:1..count
                if list(j)<smallest then
                    smallest:=list (j)
                    where:=j
                end if
            end for
            sortList(i):=smallest
            list (where):=999
        end for
        put " "
        for i:1..count
            put sortList (i)
        end for
end sortList
sortList


Edited by Clayton: Added code tags



lotto649 program.t
 Description:

Download
 Filename:  lotto649 program.t
 Filesize:  1.11 KB
 Downloaded:  94 Time(s)

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  [ 5 Posts ]
Jump to:   


Style:  
Search: