Computer Science Canada Can't find the overflow |
Author: | tiedye1 [ Tue Feb 04, 2014 6:42 pm ] | ||
Post subject: | Can't find the overflow | ||
Hi, I've been working on this algorithm, and It works perfectly for small data sets but when I try a larger set the pc (and thus the pp) variable is overflowing, but they're longs so I don't know whats causing it. Here's the relevant code.
Thanks for any help! |
Author: | Insectoid [ Tue Feb 04, 2014 7:12 pm ] |
Post subject: | RE:Can\'t find the overflow |
It would help if you told us what the algorithm is designed to do, and if you would use descriptive variable names. It's hard to read this code if I constantly have to look up what some variable is supposed to be. Also some comments would be nice. |
Author: | Zren [ Tue Feb 04, 2014 10:43 pm ] |
Post subject: | RE:Can\'t find the overflow |
What's considered a "small" and large dataset? |
Author: | tiedye1 [ Thu Feb 06, 2014 11:38 am ] | ||
Post subject: | Re: Can't find the overflow | ||
Sorry for the confusion, the purpose of the algorithm is to find all the possible paths through a grid to the opposite corner by moving only right and down. The input is formatted like this in a file: 5 4 3 2 3 2 4 3 5 Where the first two numbers represent the number of rows and columns in the grid, the second the number of impassible grid locations (X) and the remaining X lines the coordinates of those impassible tiles.
|
Author: | tiedye1 [ Thu Feb 06, 2014 11:40 am ] |
Post subject: | RE:Can\'t find the overflow |
Oh, and its works as is, but I don't know how many of those long long ints are necessary; A large data set is say a 25 by 25 grid with 1000 or more impassible tiles. |
Author: | Zren [ Thu Feb 06, 2014 5:20 pm ] | ||||||||||
Post subject: | Re: RE:Can\'t find the overflow | ||||||||||
So your treating the previousPath/currentPath variables as an array of directions, and since there's only two directions, you only need the single bit to represent the direction. You're overflowing since your trying to access an index larger than your "array". Which means you don't know the maximum path length required. First let's figure out the length of your "arrays". https://en.wikipedia.org/wiki/C_data_types An int has 16 bits. An long has 32 bits. An long long has 64 bits. Next, let's figure out the length we need. You seem to already know it.
A 25x25 grid would need an array length of COLUMNS + ROWS - 1 which is 49. A length of 49 will overflow the regular long data type, but not a long long. Since your algorithms problem doesn't define the limits of the COLUMNS and ROWS input, you should use a data type that can support much more variable lengths, like an array of booleans where you define the size manually. Tips for making your code more legible:
Use constants, so you have more variable names and less magic numbers floating around.
Parse your giant input array into variables.
|