Quantcast

What wrong in this code?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

What wrong in this code?

Jorge Eduardo
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)

Help me, guys!
Thanks a lot!

Jorge Eduardo

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Sean Conner
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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Jorge Eduardo
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
math.randomseed(os.time())
array={}
for i=1,10 do
array[i]=math.random(0,99)
end
print(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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Dirk Laurie-2
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Dirk Laurie-2
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'

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Francisco Olarte
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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Jorge Eduardo
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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Martin
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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Jorge Eduardo
Nope.
I am studying alone... Not homework.
LOL
I am a rookie in Lua programming...
I want code useful algorithms in Lua just to learn.
Next is binary search...
Thanks!

Em 15 de mai de 2017 09:16, "Martin" <[hidden email]> escreveu:
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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What wrong in this code?

Francisco Olarte
On Mon, May 15, 2017 at 2:20 PM, Jorge Eduardo de Araújo Oliveira
<[hidden email]> wrote:
> I am studying alone... Not homework.
> LOL

Studying should not be laughed at. Even by yourself.

> I am a rookie in Lua programming...
> I want code useful algorithms in Lua just to learn.
> Next is binary search...

Try to do it in the right./fast/elegant way.

Hint: if you have a 4 element vector with indexes 1 to 4 start with
low=0, high=5, not l=1 h=4, and do only one test against the middle
index in the loop. Search for implementation in the internet and
you'll understand why, ask if you do not funderstand.

Nearly all implementation of binary search found in the wild are not
too well coded, even the ones in widely used libraries, it's a
conceptually simple algorithm, but notoriously tricky to get right.

Francisco Olarte.

Loading...