views:

165

answers:

2

i need to write algo for this problem. i have never written an algo before . please correct me.

there is a list which contains four collumns each with numbers with upto 5 digits and about 10 rows in total. we have to remove the rows containng any number with less than 3 digits. here is how i have tried

  1. read list into multi-dimensional array
  2. for each number in the array if numdigits < 3 delete all numbers of that row

i know this is not the correct algorithm . can you help me correct it .

A: 

With up to 5 digits in total in each column? If so here is what I would do.

For each row in list

    For each column in row

        if column number < 100 then
        row delete
Thomas Miller
It is really not allowed to delete the row you are iterating over in every language. It could result in an iterator pointing to bogus data and being unable to resolve the next item in the collection)
Toad
True, but you don't consider 'every language' when writing pseudocode - in fact you don't normally consider *any* language.
Jon
yes i think this algo would work perfectly if use excel's programming language. please give his rating back :-)
silverkid
jon: even then, if the first column is less than 3 digits, you are still cycling through columns on a row which has just been deleted.This algo is flawed
Toad
+2  A: 

When creating your original list, rather check the individual values then, and not add it to that list if any of the numbers has less than 3 digits, that way reducing the original list size.

EDIT:

foreach row in original_document
{
    bool allMoreThan3Digits = true
    foreach cell in row
     allMoreThan3Digits = allMoreThan3Digits && (ABS(cell.Value) >= 100)

    if (allMoreThan3Digits)
     add row to new list
}

Something like that.

astander
my origninal is an an excel document. to operate upon it i need to read the excel list into an array . are you saying that while reading it into the array itself i should check for less that 3 digits and then create the array . in that case how would i delete the numbers which are in the same row.
silverkid
Changed answer with pseudo code
astander
silverkid
ok understood, thanks
silverkid
It says allMoreThan3Digits and cell value >= 100. If the number is btween -99 and 99 the result will be false, in which case we do not want to add the row.
astander