Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Uninitialized warnings trouble

by hv (Prior)
on Dec 13, 2022 at 19:06 UTC ( [id://11148835]=note: print w/replies, xml ) Need Help??


in reply to Uninitialized warnings trouble

I am being flooded with uninitialized value warnings for lines 52 ...

Let's start with this one: your previous loop (which I visualize as "looking to the left") is working ok. In that case, you are testing visibility of $row[$current][$i], and looking at the tree $counter to the left of it as $counter ranges from 1 to $i. So you check from $row[$current][$i - 1] down to $row[$current][0].

In this loop (which I visualize as "looking to the right") you want to check each tree $counter to the right, so you should end up ranging from row[$current][$i + 1] to $row[$current][99] $row[$current][98]. But you are letting $counter run from 1 to $i again, so you actually check from row[$current][$i + 1] to $row[$current][$i + $i].

If you fix that error, it should then be obvious what you need to do to fix the remaining two loops, and I think it is possible you'll even get the correct answer.

When you do, I would love for you to come back with your finished code - I'd like to give you tips on how to improve the code, but I don't want to spoil the fun and learning experience of solving the problem first. :)

Update 2022-12-13 19:06: correct index from 99 to 98

Replies are listed 'Best First'.
Re^2: Uninitialized warnings trouble
by Anonymous Monk on Dec 13, 2022 at 21:42 UTC

    Thanks! I fixed my while loops and am not getting any warnings. I haven't got the answer yet, though.

    $counter = $i + 1; while ($forest[$current][$counter] < $forest[$current][$i] and $fo +und == 0) { $counter += 1; if ($counter == 99) { $visible += 1; $found = 1; $counter = 1; } }

      Oh, I had a typo/thinko in my previous response - you were correctly counting from 0 to 98, but I wrongly suggested you should go up to $row[$counter][99] when I should have said 98. (So I think you should still be getting a few warnings.)

      My output is: 1673

      That's not what I get, but it isn't very far out. Maybe you could show the rest of the changes you made?

      It would probably be more practical to start with a smaller grid, so that you can more easily see what the correct answer is - which would mean generalizing your code to discover the size when you read the input file, but that's a useful thing to do in any case. Then you could test your code against some simple cases, like:

      222 222 (8 visible) 222 222 122 (9 visible) 222 222 221 (9 visible) 222 212 222 (9 visible) 222 222 222 (9 visible) 212 222 212 (8 visible) 222 22222 21112 (12 visible, test horizontal bounds) 22222 222 212 212 (12 visible, test vertical bounds) 212 222 etc.

        "99" did work. I think it did because I increment my counter before I start my if loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148835]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found