[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: What wrong in this code?
- From: Francisco Olarte <folarte@...>
- Date: Mon, 15 May 2017 10:36:31 +0200
On Mon, May 15, 2017 at 10:10 AM, Jorge Eduardo de Araújo Oliveira
> The right code is it:
> function linearSearch(table,element)
> local position=0
> while position<#table do
> if table[position]==element then
> return position
You are searching in integer indexes 0 to #table-1. In lua sequences
go normally from 1 to #table.
> for i=1,10 do
And here you are filling them the lua way, from 1
> io.write("Enter a number to search in array:\n")
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
>> > 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.'