Miscellaneous > Programming & Networking
how long is it?
worker201:
I have to write a program that does the following (for work):
* Reads in a file of x,y points
* computes the distance between each point
* outputs a total distance
* can deal with multiple segments (segments are separated by '>')
Here's an example of a 3 segment file:
--- Code: --->
3 4
9 8
1 -4
7 9
>
2 5
-4 -6
12 118
2 4
>
-6 -63
-6 -21
-6 5
-4 87
--- End code ---
From this example, I would want the length of each line segment, and then the total length.
Ideally, it wouldn't matter whether there was a tab or a space or comma or whatever between each column, as long as each column is well-defined. And it would have to handle files (and segments) of arbitrary size.
So:
1. What language fits this application? I was thinking Perl, even though I don't know much Perl. Although the columnar nature of the data suggests awk.
2. Is the best structure going to be some giant 40-line control loop ({while a != EOF} or whatever)? Or is there a more subtle way to do it?
PLEASE, don't respond with any code, because I really do want to write this program myself, whatever language it ends up in. I just need help with the program design.
worker201:
Okay, then, go ahead and respond with code. I don't know enough to step over the > and start over with the computations.
piratePenguin:
When you reach a '>', if it's not at the beginning of the file, add the answer-so-far-for-this-segment to the complete-answer-so-far and then set answer-so-far-for-this-segment to 0 and continue (I smell a recursive function).....
worker201:
Where do I even start?
Set total=0. Get the first line. If that line is a >, then move to the next line. Set subtotal[0]=0. Take the first number and put it into var[0], and the second number, and put it into var[1]. Move to the next line, and put the first number into var[2] and the second number into var[3]. Calculate the distance using var[0], [1], [2], [3]. Add the distance to subtotal[0]. Set var[0]=var[2] and var[1]=var[3]. Move to the next line and put the first number into var[2] and the second number into var[3]. Unless the next line starts with a >, then add subtotal[0] to total...
There's so many different places where a different looping process starts, or overlaps with another one. I'm beginning to wonder if this is beyond me.
piratePenguin:
--- Quote from: worker201 ---Where do I even start?
Set total=0. Get the first line. If that line is a >, then move to the next line. Set subtotal[0]=0. Take the first number and put it into var[0], and the second number, and put it into var[1]. Move to the next line, and put the first number into var[2] and the second number into var[3]. Calculate the distance using var[0], [1], [2], [3]. Add the distance to subtotal[0]. Set var[0]=var[2] and var[1]=var[3]. Move to the next line and put the first number into var[2] and the second number into var[3]. Unless the next line starts with a >, then add subtotal[0] to total...
There's so many different places where a different looping process starts, or overlaps with another one. I'm beginning to wonder if this is beyond me.
--- End quote ---
Well, you did already describe one way that would (probably) work...
This is what I'd do (in C, I don't know perl or python):
I'd have a 'point' structure, with 'x' and 'y' components. A 'distance' function that returns the distance between 2 given points. A 'previous_point' point, which will store NULL when we're at the beginning of a segment. A 'current_point' point. Then,,
I'd have a 'string' variable - a 7-char array (7 should be big enough..), which would store the current "fragment" of the input file we're parsing, retrieved with the fscanf function. Then you can compare string[0] to '>' so you can set 'previous_point' to NULL, else you can use the atoi function on 'string' and store the result in 'this_point.x' and then do something like 'fscanf (input_file, "%d", &this_point.y)' and then this_point is populated. And then check if previous_point is NULL, if it is then do nothing, otherwise do a 'total += difference (previous_point, this_point)' job. Then 'previous_point = current_point' And... If all of is happening in a while (string[0] != EOF) type-loop (string being defined outside the loop (I completely messed that up)) it should work. Hopefully.
Alot of the specifics there I'm unsure of though, it's only after I'm in the write-compile-fix mood I can be sure of anything, with C.
If that's completely wrong, if it just doesn't work, it's the kinda thinking I'd start off with, anyhow.
(what a mess of a post, but I hope you got the idea..)
Navigation
[0] Message Index
[#] Next page
Go to full version