| | |
Distance between 2 points
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2005
Posts: 6
Reputation:
Solved Threads: 0
I'm trying this new project, like this.
Write a program which will use functions to compute the distance between 2 points on a plane given their coordinates. If one point is located at (x1,y1) and the other is located at (x2,y2) then the formula for computing the distance is
sqrt(sqr(x2-x1) + sqr(y2-y1))
Create your own data file for this exercise the coordinates should be arranged in the text file as follows;
5 -2 3 1
4 4 12 2
0 1 1 0
all parameters and variables will be of type float or double (display to 3 decimal places) the program should use 3 functions
1. getdata -- will read and echo the input
2. calculate -- will compute the distance between the points
3. print -- will print the results of the calculations to the screen
I ahve sorted it out into functions, but I am not sure how to isolate the first 4 points so I can make
x1 = 5
y1 = -2
x2 = 3
y2 = 1
and so on for the remainder lines.
What I have so far is:
The result is:
http://i2.photobucket.com/albums/y45...console002.jpg
I was given the equation:
sqrt(sqr(x2-x1) + sqr(y2-y1))
but am not sure where to use it because I still have not figures out how to isolate each value from the txt file.
Write a program which will use functions to compute the distance between 2 points on a plane given their coordinates. If one point is located at (x1,y1) and the other is located at (x2,y2) then the formula for computing the distance is
sqrt(sqr(x2-x1) + sqr(y2-y1))
Create your own data file for this exercise the coordinates should be arranged in the text file as follows;
5 -2 3 1
4 4 12 2
0 1 1 0
all parameters and variables will be of type float or double (display to 3 decimal places) the program should use 3 functions
1. getdata -- will read and echo the input
2. calculate -- will compute the distance between the points
3. print -- will print the results of the calculations to the screen
I ahve sorted it out into functions, but I am not sure how to isolate the first 4 points so I can make
x1 = 5
y1 = -2
x2 = 3
y2 = 1
and so on for the remainder lines.
What I have so far is:
C++ Syntax (Toggle Plain Text)
// Excercise 2 no.3.cpp : Defines the entry point for the console application. // #include "stdafx.h" using namespace std; void getData(double&); void doCalc(); void printScreen(); void printClosing(); ifstream infile; void main() { // start main double n; // input file values float x1 = 0; // x coordinate 1 float x2 = 0; // x coordinate 2 float y1 = 0; // y coordinate 1 float y2 = 0; // y coordinate 2 // ***** open stream file ***** infile.open ("ergo.txt"); // get data from file if(infile) cout << "Stream ok.\n\n"; else cout << "Stream error.\n\n"; // ***** take input from file ***** getData(n); // ***** do calculations from input file ***** doCalc(); // ***** print results to screen ***** printScreen(); // ***** print closing message ***** printClosing(); } // end main void getData(double& n) { // start getData while(infile) { // start while loop infile >> n; // print loop from file. cout << "\nnumber =\t" << n; } // end while loop cout << endl; } // end getData void doCalc() { // start doCalc } // end doCalc void printScreen() { // start printScreen } // end printScreen void printClosing() { // start printClosing int wait; cout << "\nEnter any key to terminate program.\n"; cin >> wait; } // end printClosing
The result is:
http://i2.photobucket.com/albums/y45...console002.jpg
I was given the equation:
sqrt(sqr(x2-x1) + sqr(y2-y1))
but am not sure where to use it because I still have not figures out how to isolate each value from the txt file.
>I am not sure how to isolate the first 4 points so I can
How about changing getdata just a wee bit. And then in main actually passing the parameters you want to get (once you've made a slight change) to this function.
How about changing getdata just a wee bit.
C++ Syntax (Toggle Plain Text)
void getData(double& n) { if ( infile >> n ) { cout << "number = " << n << endl; } }
C++ Syntax (Toggle Plain Text)
double x1, x2, y1, y2; // ... while ( infile ) { getData(x1); getData(x2); getData(y1); getData(y2); doCalc(); }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>But I need to assign every fourth digit x1, and so on.
Uh, this is being done in the code I posted. (I should have done better error checking, but I didn't want to confuse you further.)
You weren't expecting us to fill in the blanks for you on the remaining functions, were you? I showed how the doCalc ought to move into this loop, thinking you could pick up on it, fill in some of that code, and take the initiative to put the printScreen function there as well.
Uh, this is being done in the code I posted. (I should have done better error checking, but I didn't want to confuse you further.)
You weren't expecting us to fill in the blanks for you on the remaining functions, were you? I showed how the doCalc ought to move into this loop, thinking you could pick up on it, fill in some of that code, and take the initiative to put the printScreen function there as well.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- How to redistribute points on line..?? (C++)
- C#.Net:How to find the distance between two points clicked using mouse Button (C#)
- Calculating the minimal distance between points (C++)
Other Threads in the C++ Forum
- Previous Thread: PUTTING C++ TO USE!!! (GUIs & MS Windows)
- Next Thread: Tic Tac Toe Homework
Views: 7844 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






