algorithms and data structures?

BlitzMax Forums/BlitzMax Beginners Area/algorithms and data structures?

Hotshot2005(Posted 2010) [#1]
Is there algorithms and data structures Tutorial for BlitzMax?


Gabriel(Posted 2010) [#2]
Surely the whole point of a tutorial about data structures and algorithms would be that it would be code-neutral, and therefore applicable for any language?


Dreamora(Posted 2010) [#3]
agreed, the majority, especially the good, articles on ADT I've so far seen were written in pseudo code (pascal-basic-ish normally ie "near normal english")


Hotshot2005(Posted 2010) [#4]
I have try to do this by convert from C++ to blitzmax

Strict

Type movies_t
	Field title
     Field year
End Type

pmovie(movie)

While Not KeyDown(1)

  String mystr

  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  Print "Enter title: "
  yours.title=Input
  Print "Enter year: "
  your.year=Input
  

  Print "My favorite movie is:"
  Pmovie (mine)
  Print "And yours is: "
  pmovie (yours)

  Return 0;
Wend

Function pmovie (movie)

  Print movie.title
  Print "(",movie.year,")"

End Function


I dont know why the code doesnt work :(


Jesse(Posted 2010) [#5]
you are trying to convert a program from c to BM with out knowin how to properly program in BM. I am going to tell you what you are doing wrong but I am not going to convert it for you. first thing you need to learn to do is lear how to use BM commands. To do this you need to look at the commands and how they are used. One way to do it is to type the commands in the ide put the cursor over the command and press f1 if the command exists it will display a simplified way of using the command At the bottom of the ide. If you press f1 again it will take you to a page where it might explain it better.
Strict ' in your case I think you need to use Superstrict while you learn to use variables

Type movies_t
     ' if you don't declare a variable type then it will automatically will
     ' be converted to an int. In this case "title" is wrong sense you want it
     ' to be a string because you are storing names of movies.
     Field title
     Field year 'this is correct sense it automatically is converted to int
End Type

pmovie(movie) ' this line is wrong  BM does not use prototypes so it needs to be removed

While Not KeyDown(1) ' I doubt that 1 means <Escape> you need to use <KEY_ESCAPE>
  ' below is not how you declare variables in BM. you need to first declare
  ' Local or Global then the name followed by collins then the type
  ' such as "String","Float","Int",etc.
  String mystr  ' you never use "mystr" so why are you trying to create it?
  'for every type before you use it, it has to be declared and instantiated
  '"mine" has never been declared so derefore it will not work also before
  'you use it, you have to create an instance of it. you can crate an
  'instace of it like this:
  'local mine:movies_t = new Movies_t
 
  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  Print "Enter title: "
  'that is not how you are supposed to use input research it as I explained
  'above. also 'yours' have to be declared and instantiated.
  yours.title=Input
  Print "Enter year: "
  your.year=Input
  

  Print "My favorite movie is:"
  Pmovie (mine)
  Print "And yours is: "
  pmovie (yours)

  Return 0; ' if you are not returning anything you don't need this line
Wend
' if you are passing something other than an integer to a function you
' need to specify or it will assume you are passing an integer
' in this case you are passing a type so you need to specify 
Function pmovie (movie:movies_t)

  Print movie.title
  ' BM print command does not accept multiple parameters so you can't use
  ' commas. You need to substitue the comma with a '+' sign.
  Print "(",movie.year,")"

End Function


you need to go through the documentation in the ide so you can learn how to use BM better. I know it is not well documented but eitherway it will help you a lot based on the code you posted.


Hotshot2005(Posted 2010) [#6]
this is what i have done so far

SuperStrict

Type movies_t
     Field title$
     Field year:Int
End Type


Local mine:movies_t  = New Movies_t
Local yours:movies_t = New movies_t

 
  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  yours.title$=Input("Enter Title: ")
  yours.year=Input("Enter Year: ")
  
  Print "My favorite movie is:"+  Pmovie (mine)
  Print "And yours is: "+  pmovie (yours)

WaitKey

' if you are passing something other than an integer to a function you
' need to specify or it will assume you are passing an integer
' in this case you are passing a type so you need to specify 
Function pmovie (movie:movies_t)

  Print movie.title +" ("+ movie.year +")"

End Function



I get the error saying "unable to convert from String(String) to string...
it is to do with year error?


TaskMaster(Posted 2010) [#7]
1. You don't need semi colons after command in BlitzMax.
2. You did not tell the pmovie function that it is suppose to return a string.

Function pmovie:string(movie:movies_t)


Hotshot2005(Posted 2010) [#8]
i see...

why do i get error saying unable to convert from string to int

yours.year=Input("Enter Year: ") ' << why i get the error for this one?


Hotshot2005(Posted 2010) [#9]
This what i have done so far and it`s work :)

SuperStrict

Type movies_t
     Field title:String
     Field year:String
End Type


Local mine :movies_t  = New Movies_t

  mine.title = "2001 A Space Odyssey"
  mine.year = 1968

  Local yours:movies_t  = New movies_t

  yours.title$=Input("Enter Title: ")
  yours.year=Input("Enter Year: ")
  
  Print "My favorite movie is:"
  Print  pmovie (mine)

  Print "And yours is: " 
  Print  pmovie (yours)

WaitKey


Function pmovie:String(movie:movies_t) 

  Print movie.title +" ("+ movie.year +")"

End Function



Hotshot2005(Posted 2010) [#10]
I have also done abit more to it...

SuperStrict

Const N_MOVIES:Int=3

Type movies_t 
  Field title:String
  Field year:String
End Type

Local  n:movies_t  = New Movies_t
Local  Flims:String[4]

  For N:Int=0 To N_Movies:Int
      Title=Input("Enter Title: ")
      Year=Input ("Enter Year: ")
      Flims[n]    
      n=n+1
  Next

  Print "You have entered these movies: "

  For N=0 To N_MOVIES
     pmovie (Films[n])
     N=N+1
  Next

WaitKey

Function pmovie:String(movie:movies_t)

  Print  movie.title
  Print " (" + movie.year + ")"

End Function



For N:Int=0 To N_Movies:Int ' << Why error?

It saying Identifier type does not match declared type


Sledge(Posted 2010) [#11]
For N:Int=0 To N_Movies:Int ' << Why error?

Because you already defined N as of type movies_t. You want a new variable that is local to the for...next loop, eg:

For Local N:Int=0 To N_Movies:Int


(EDIT: Ideally, of course, you should choose a distinct name for your iterator.)


Czar Flavius(Posted 2010) [#12]
You are programming in C++ style in Blitz which is a recipe for disaster. C++ style is so horrible, Blitz style is elegant and flowing. Flims[n] does not even do anything. Here is what BlitzMax should be like - succulent and juicey.

SuperStrict


'this is the Blitz type naming style TMyType
Type TMovie
	'the underscore _ means they are private variables and should not be accessed directly
	Field _title:String 'don't mix $ style and word (String) style. it looks odd
	Field _year:Int
	
	Method get_title:String()
		'this is a "getter" method. they can be more trouble to use but for example
		'you could put a debug stop in here and if you have a strange error whenever
		'you are detaling with a movie's title you can easily find out which part
		'of the code requested the movie's title
		Return _title
	End Method
	
	Method get_year:Int()
		'be careful to make sure your functions are given a return type if they return data
		Return _year
	End Method
	
	Method set_title(title:String)
		'this is a "setter" method. by only allowing these to set your fields you
		'can gain greater control over what data your types can store. here
		'we make sure a blank title is not entered. asserts only check in
		'debug mode. alternatively, replace assert with a message which won't stop the program
		Assert title.Length > 0
		_title = title
	End Method
	
	Method set_year(year:Int)
		Assert year >= 0
		_year = year
	End Method
	
	Method get_details:String()
		'similar to your pmovie function but turned into a method. note the
		'MUCH more descriptive name. what is "pmovie" meant to mean? avoid
		'C++ naming styles like the uiPlg. oops sorry, the Plauge
		'~n is new line
		Return "-------------~n" + "Movie Details~n" + "Title: " + get_title() + "~nYear: " + get_year() + "~n-------------~n~n"
	End Method
	
	Function Create:TMovie(title:String, year:Int)
		'this is anagolous to a C++ constructor, which you will meet soon enough
		'in the C++ guides. it sets up a new object with some starting values
		'in a constructor it's ok to set an underscored field directly
		'but here we still enforce the validity checks
		
		
		'first we need to make the new movie type
		Local movie:TMovie = New TMovie
		'setup initial values
		movie.set_title(title)
		movie.set_year(year)
		
		'now we need to return the new movie we've made back to the program.
		'be careful to remember this functions return type. sometimes even i forget it
		Return movie
	End Function
End Type



'in Blitz you use lists more often than arrays as you can add as many things as you want
Local film_list:TList = New TList

film_list.AddLast(TMovie.Create("2001 A Space Odyssey", 1968))

'using a local variable first
Local temp_film:TMovie = TMovie.Create("Toy Story 3", 2010)

film_list.AddLast(temp_film)


'how to go through everything in a list

For Local film:TMovie = EachIn film_list
	Print film.get_details()
Next

'no this is bad
temp_film._title = "gfdjoigdfjoi"

'this is good
temp_film.set_title("Toy Story 57")


'create an array of films like this. note carefully the first [] and the second [] with number, and new
Local film_array:TMovie[] = New TMovie[4]

'this won't work, so i've commented it out
'film_array[0].set_title("Invisible Man")

'this is because the array is EMPTY


film_array[0] = temp_film
'this is ok because there is now a real film object here
'the other array positions are still blank though
film_array[0].set_title("Invisible Man")

'this is how to setup an array of real objects

For Local i:Int = 0 To film_array.Length - 1
	film_array[i] = TMovie.Create("Clone " + (i+1), Rand(1980, 2010))
Next

'don't bother with having a ton of constants to remember how big arrays are
'that C crap is not needed here. you can ask arrays how big they are with .Length

'either of these two methods are acceptable for going through a whole array


For Local i:Int = 0 To film_array.Length - 1
	Print film_array[i].get_details()
Next

For Local film:TMovie = EachIn film_array
	Print film.get_details()
Next

'Invisible Man didn't appear from that. can you see why? it was replaced with a clone




Hotshot2005(Posted 2010) [#13]
that is very good and good to learn from :)


Jesse(Posted 2010) [#14]
this:
Input("Enter Year: ")

should be:
int(Input("Enter Year: "))



Hotshot2005(Posted 2010) [#15]

int(Input("Enter Year: "))



that is new to me :)


Jesse(Posted 2010) [#16]
The reason you need to do that is because input returns a string and before you assign it to an integer variable you have to convert it to an 'Int'.