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

Username:   Password: 
 RegisterRegister   
 Define a parameter to be of any type.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Timothy Willard




PostPosted: Fri Jun 28, 2013 3:18 pm   Post subject: Define a parameter to be of any type.

What is it you are trying to achieve?
I currently have several function that emulate shift/pop for arrays from other languages. However, I want to condense them into 1 function.


What is the problem you are having?
I want the function to take in a parameter that is an array, and result the array shifted. The problem is that I have some arrays of a class (anyclass would probably work here), but some arrays are of strings. Is there any way to set the parameter of the function to be an array of any indexType, rather than having two functions (one for arrays of classes, and one for strings)?

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Here is an example of my procedure that shift a specific array (previousText). If I were to set it up as a function that would do the same with a array put as a parameter, it would be easy if I made the parameter examplearray: array 1 .. * of string, expect for the fact that I also want to do the same for an array of a class.

Turing:
proc shiftText
    for i : 1 .. upper(previousText) - 1
        previousText (i) := previousText (i + 1)
    end for
previousText (upper(previousText)) := nil
end shiftText


Is there any way to do this, or do I need a seperate function for each type of array?
Please specify what version of Turing you are using
4.1
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Fri Jun 28, 2013 6:15 pm   Post subject: RE:Define a parameter to be of any type.

In turing, I believe not, unless there's some way to do it with cheat
Dreadnought




PostPosted: Fri Jun 28, 2013 10:26 pm   Post subject: Re: Define a parameter to be of any type.

There is no "nice" way to do it. If you're sufficiently determined, some combination of union, addressint/pointers, cheat and/or classes would probably do it.

Otherwise, try to think of some other way to get things done (maybe make a string wrapper class).
Tony




PostPosted: Sat Jun 29, 2013 12:50 am   Post subject: Re: Define a parameter to be of any type.

Timothy Willard @ Fri Jun 28, 2013 3:18 pm wrote:

Turing:
proc shiftText
    for i : 1 .. upper(previousText) - 1
        previousText (i) := previousText (i + 1)
    end for
previousText (upper(previousText)) := nil
end shiftText



So this implementation is an O(n) operation. If the array is of size 100, you'd need to perform ~100 operations to shift a single element. That's no good.

If your underlying datastructure was a circular array instead ( https://en.wikipedia.org/wiki/Circular_buffer ) then you could shift 1 element in constant amount of operations.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Sat Jun 29, 2013 6:55 pm   Post subject: RE:Define a parameter to be of any type.

I've seen that formula a lot, so does O stand for number of operations where n is the amount of elements being used?
Insectoid




PostPosted: Sat Jun 29, 2013 7:19 pm   Post subject: RE:Define a parameter to be of any type.

https://en.wikipedia.org/wiki/Big_O_notation

Big-O represents the efficiency of an algorithm. Generally we aren't terribly concerned with the precise number of operations an algorithm requires. Rather, we want to know how much worse the algorithm performs as the input gets bigger. The ideal algorithm is O(1). This doesn't mean that it only requires one operation, but rather that no matter how big the input is, the algorithm requires the same number of operations (or more precisely, it doesn't tend towards a longer runtime. It might fluctuate a bit).

O(n) means that the function grows linearly. An input twice as large requires twice as much time to execute. For example,
code:
for i in array {
    print "Hello World"
}


If array is of size 5, this loop takes 5 operations. If it is of size 100, it takes 100 operations. Thus, it is O(n).

Note that we really don't care about coefficients in big-O. A function might require 2n operations, but it is still O(n), because again, we don't care about how many operations it requires but rather how fast the algorithm grows.

The slowest algorithms are O(n!). They're really, really inefficient. Doubling the input size dramatically slows down the algorithm. 5! = 120. 10! = 3628800. 20! =2.4*10^18. These are ridiculously slow algorithms even with small inputs.

The wiki article lists the most common algorithm runtimes and gives examples of each, if you want to read up on it.
Timothy Willard




PostPosted: Sat Jun 29, 2013 7:24 pm   Post subject: RE:Define a parameter to be of any type.

If I understand the concept of a circular buffer correctly, rather than deleting the first item in the array and shifting everything down one, I would simple overwrite the first item in the array with the new data, and then tell the computer to read 2..upper, then read 1. The next item I add would overwrite 2, and then I would read 3 .. upper, then 1 .. 2.

Is this correct, or did I misunderstand?

Also, what exactly is a string wrapper class? Would this be something along the lines of replacing my array of strings with an array of a class that contains one string, to emulate the array of string but have it technically be an array of a class?
Insectoid




PostPosted: Sat Jun 29, 2013 7:52 pm   Post subject: RE:Define a parameter to be of any type.

It's actually simpler than that, but a little more complicated. A circular buffer consists of an array and an integer. The integer represents the 'start' of the array. The mod operator is what makes it circular. I'll leave it to you to figure out how.

A string wrapper class is just a class with a string in it, that emulates the string. Simply exporting the string should be enough for it to work. This way the same function will work on strings and classes.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: