What wrong in this code?

10 messages
Open this post in threaded view
|

What wrong in this code?

 I was writing a code to do linear search:What wrong?function linearSearch(table, element)local position=0while position<#table doif table[position]==element thenreturn positionelseposition=position+1endendendmath.randomseed(is.time())array={}for i=1,10 doarray[i]=math.random(0,99)endprint(table.concat(vetor,","))io.write("Enter a number to search in  array:\n")num=io.read("*n")buscaLinear(array,num)Help me, guys!Thanks a lot!Jorge Eduardo
Open this post in threaded view
|

Re: What wrong in this code?

 It was thus said that the Great Jorge Eduardo de Araújo Oliveira once stated: > I was writing a code to do linear search: > What wrong? > > function linearSearch(table, element) > local position=0 > while position<#table do > if table[position]==element then > return position > else > position=position+1 > end > end > end > math.randomseed(is.time()) > array={} > for i=1,10 do > array[i]=math.random(0,99) > end > print(table.concat(vetor,",")) > io.write("Enter a number to search in  array:\n") > num=io.read("*n") > buscaLinear(array,num)   Problem 1: lua-53: y.lua:11: attempt to index a nil value (global 'is') stack traceback:         y.lua:11: in main chunk         [C]: in ?   Problem 2: lua-53: y.lua:16: bad argument #1 to 'concat' (table expected, got nil) stack traceback:         [C]: in function 'table.concat'         y.lua:16: in main chunk         [C]: in ?   Problem 3: lua-53: y.lua:22: attempt to call a nil value (global 'buscaLinear') stack traceback:         y.lua:22: in main chunk         [C]: in ?   Quite a few typos ("is" instead of "os", "vector" instead of "array", "buscalLinear" instead of "linearSearch").     -spc
Open this post in threaded view
|

Re: What wrong in this code?

 Wow!I'm sorry...The right code is it:function linearSearch(table,element)local position=0while position<#table doif table[position]==element thenreturn positionelseposition=position+1endendendmath.randomseed(os.time())array={}for i=1,10 doarray[i]=math.random(0,99)endprint(table.concat(array,","))io.write("Enter a number to search in array:\n")num=io.read("*n")linearSearch(array,num)Em 15 de mai de 2017 04:59, "Sean Conner" <[hidden email]> escreveu:It was thus said that the Great Jorge Eduardo de Araújo Oliveira once stated: > I was writing a code to do linear search: > What wrong? > > function linearSearch(table, element) > local position=0 > while position<#table do > if table[position]==element then > return position > else > position=position+1 > end > end > end > math.randomseed(is.time()) > array={} > for i=1,10 do > array[i]=math.random(0,99) > end > print(table.concat(vetor,",")) > io.write("Enter a number to search in  array:\n") > num=io.read("*n") > buscaLinear(array,num)   Problem 1: lua-53: y.lua:11: attempt to index a nil value (global 'is') stack traceback:         y.lua:11: in main chunk         [C]: in ?   Problem 2: lua-53: y.lua:16: bad argument #1 to 'concat' (table expected, got nil) stack traceback:         [C]: in function 'table.concat'         y.lua:16: in main chunk         [C]: in ?   Problem 3: lua-53: y.lua:22: attempt to call a nil value (global 'buscaLinear') stack traceback:         y.lua:22: in main chunk         [C]: in ?   Quite a few typos ("is" instead of "os", "vector" instead of "array", "buscalLinear" instead of "linearSearch").   -spc
Open this post in threaded view
|

Re: What wrong in this code?

 2017-05-15 10:10 GMT+02:00 Jorge Eduardo de Araújo Oliveira <[hidden email]>: > Wow! > I'm sorry... > The right code is it: > > function linearSearch(table,element) > local position=0 > while position<#table do > if table[position]==element then > return position > else > position=position+1 > end > end > end It will interrogate position[0] to position[#table-1]. But why do it this way? Idiomatic Lua is: function linearSearch(table,element)   for k,v in pairs(table) do     if v==element then return k   end end
Open this post in threaded view
|

Re: What wrong in this code?

 2017-05-15 10:20 GMT+02:00 Dirk Laurie <[hidden email]>: > function linearSearch(table,element) >   for k,v in pairs(table) do >     if v==element then return k >   end > end Oops, needs another 'end'
Open this post in threaded view
|

Re: What wrong in this code?

 In reply to this post by Jorge Eduardo Jorge Eduardo: On Mon, May 15, 2017 at 10:10 AM, Jorge Eduardo de Araújo Oliveira <[hidden email]> wrote: > The right code is it: ... > > function linearSearch(table,element) > local position=0 > while position<#table do > if table[position]==element then > return position > else > position=position+1 > end You are searching in integer indexes 0 to #table-1. In lua sequences go normally from 1 to #table. > end > end > math.randomseed(os.time()) > array={} > for i=1,10 do > array[i]=math.random(0,99) > end And here you are filling them the lua way, from 1 > print(table.concat(array,",")) > io.write("Enter a number to search in array:\n") > num=io.read("*n") > linearSearch(array,num) So if you search for the last num you'll never find it. Anyway, your loop is quite complex, and you've made a major mistake: >> It was thus said that the Great Jorge Eduardo de Araújo Oliveira once >> stated: >> > I was writing a code to do linear search: >> > What wrong? If you are a teacher giving an exam, you MAY be able to ask that kind of questions. If you want help fixing your code you should normally state what is yur observed problem, otherwise you'll increase the probability of people deciding 'If he cannot take time to properly write his questions, I am not taking it to try to answer.' Regards.    Francisco Olarte.
Open this post in threaded view
|

Re: What wrong in this code?

 Thanks all!Code is now running well!Em 15 de mai de 2017 05:37, "Francisco Olarte" <[hidden email]> escreveu:Jorge Eduardo: On Mon, May 15, 2017 at 10:10 AM, Jorge Eduardo de Araújo Oliveira <[hidden email]> wrote: > The right code is it: ... > > function linearSearch(table,element) > local position=0 > while position<#table do > if table[position]==element then > return position > else > position=position+1 > end You are searching in integer indexes 0 to #table-1. In lua sequences go normally from 1 to #table. > end > end > math.randomseed(os.time()) > array={} > for i=1,10 do > array[i]=math.random(0,99) > end And here you are filling them the lua way, from 1 > print(table.concat(array,",")) > io.write("Enter a number to search in array:\n") > num=io.read("*n") > linearSearch(array,num) So if you search for the last num you'll never find it. Anyway, your loop is quite complex, and you've made a major mistake: >> It was thus said that the Great Jorge Eduardo de Araújo Oliveira once >> stated: >> > I was writing a code to do linear search: >> > What wrong? If you are a teacher giving an exam, you MAY be able to ask that kind of questions. If you want help fixing your code you should normally state what is yur observed problem, otherwise you'll increase the probability of people deciding 'If he cannot take time to properly write his questions, I am not taking it to try to answer.' Regards.    Francisco Olarte.
Open this post in threaded view
|

Re: What wrong in this code?

 On 05/15/2017 04:23 AM, Jorge Eduardo de Araújo Oliveira wrote: > Thanks all! > Code is now running well! I have a feeling that it was someones own homework.