lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks all!
Code is now running well!

Em 15 de mai de 2017 05:37, "Francisco Olarte" <folarte@peoplecall.com> escreveu:
Jorge Eduardo:

On Mon, May 15, 2017 at 10:10 AM, Jorge Eduardo de Araújo Oliveira
<jorgeeduardoaoliveira@gmail.com> 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.