Apr-06-2019, 01:26 AM
Below, is an assignment I was given, first of all I'm no where near this level in math yet but the math operation line for this should be something like: distance = math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) I think, anyway we just covered opening files, reading files, outputting to files and that part is new to me.
My question is do I want to use file.readlines() for something like this and if so, how do I go about running loop over my lists? Below is the assignment:
In Cartesian coordinate system, the distance between two points
## Page will not display special characters from this file but the formula is: the distance between P1 and P2 = square root
of: ((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) ##
In this lab, you are writing a small program which reads in several sets of point coordinates from an input file, calculates their distances, and writes those calculated distances to an output file.
Reading from and Writing to Files
There are exactly six integers (could be negative) appear in each line of the input file. The first three numbers represent the first point P1( x 1 , y 1 , z 1 ) , and the next three numbers represent the second point P2 ( x 2 , y 2 , z 2 ) . You cannot assume how many lines are in the input file. Your program should read all the data from the input file.
After reading the six numbers in a line, your program calculates the distance between the two points, which should be floating-point number. Then, your program outputs the calculated distance to the output file. Each distance is in a separate line in the output file.
Writing the Distance Function
You need to write a function called calcDistance, which takes in 6 arguments (all integers) that represent the x x , y y and z z coordinates of the two points. The method calculates the distance between the two points, and returns the value (floating-point).
My question is do I want to use file.readlines() for something like this and if so, how do I go about running loop over my lists? Below is the assignment:
In Cartesian coordinate system, the distance between two points
## Page will not display special characters from this file but the formula is: the distance between P1 and P2 = square root
of: ((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) ##
In this lab, you are writing a small program which reads in several sets of point coordinates from an input file, calculates their distances, and writes those calculated distances to an output file.
Reading from and Writing to Files
There are exactly six integers (could be negative) appear in each line of the input file. The first three numbers represent the first point P1( x 1 , y 1 , z 1 ) , and the next three numbers represent the second point P2 ( x 2 , y 2 , z 2 ) . You cannot assume how many lines are in the input file. Your program should read all the data from the input file.
After reading the six numbers in a line, your program calculates the distance between the two points, which should be floating-point number. Then, your program outputs the calculated distance to the output file. Each distance is in a separate line in the output file.
Writing the Distance Function
You need to write a function called calcDistance, which takes in 6 arguments (all integers) that represent the x x , y y and z z coordinates of the two points. The method calculates the distance between the two points, and returns the value (floating-point).