Distance between 2 points

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2005
Posts: 6
Reputation: JayseR is an unknown quantity at this point 
Solved Threads: 0
JayseR JayseR is offline Offline
Newbie Poster

Distance between 2 points

 
0
  #1
Mar 15th, 2005
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:
  1. // Excercise 2 no.3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. using namespace std;
  6.  
  7. void getData(double&);
  8. void doCalc();
  9. void printScreen();
  10. void printClosing();
  11.  
  12. ifstream infile;
  13.  
  14. void main()
  15. { // start main
  16.  
  17. double n; // input file values
  18. float x1 = 0; // x coordinate 1
  19. float x2 = 0; // x coordinate 2
  20. float y1 = 0; // y coordinate 1
  21. float y2 = 0; // y coordinate 2
  22.  
  23. // ***** open stream file *****
  24. infile.open ("ergo.txt"); // get data from file
  25.  
  26. if(infile)
  27. cout << "Stream ok.\n\n";
  28. else
  29. cout << "Stream error.\n\n";
  30.  
  31. // ***** take input from file *****
  32. getData(n);
  33.  
  34. // ***** do calculations from input file *****
  35. doCalc();
  36.  
  37. // ***** print results to screen *****
  38. printScreen();
  39.  
  40. // ***** print closing message *****
  41. printClosing();
  42.  
  43. } // end main
  44.  
  45. void getData(double& n)
  46. { // start getData
  47.  
  48. while(infile)
  49. { // start while loop
  50. infile >> n; // print loop from file.
  51. cout << "\nnumber =\t" << n;
  52. } // end while loop
  53.  
  54. cout << endl;
  55.  
  56. } // end getData
  57.  
  58. void doCalc()
  59. { // start doCalc
  60.  
  61. } // end doCalc
  62.  
  63. void printScreen()
  64. { // start printScreen
  65.  
  66. } // end printScreen
  67.  
  68. void printClosing()
  69. { // start printClosing
  70.  
  71. int wait;
  72.  
  73. cout << "\nEnter any key to terminate program.\n";
  74. cin >> wait;
  75.  
  76. } // 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Distance between 2 points

 
0
  #2
Mar 15th, 2005
>I am not sure how to isolate the first 4 points so I can

How about changing getdata just a wee bit.
  1. void getData(double& n)
  2. {
  3. if ( infile >> n )
  4. {
  5. cout << "number = " << n << endl;
  6. }
  7. }
And then in main actually passing the parameters you want to get (once you've made a slight change) to this function.
  1. double x1, x2, y1, y2;
  2. // ...
  3. while ( infile )
  4. {
  5. getData(x1);
  6. getData(x2);
  7. getData(y1);
  8. getData(y2);
  9.  
  10. doCalc();
  11. }
"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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: JayseR is an unknown quantity at this point 
Solved Threads: 0
JayseR JayseR is offline Offline
Newbie Poster

Re: Distance between 2 points

 
0
  #3
Mar 15th, 2005
I have done what you had suggested and all worked out fine, but it's still just outputting the samething to console, which is good. But I need to assign every fourth digit x1, and so on. Sorry for the fuss, I do appreciate your help :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Distance between 2 points

 
0
  #4
Mar 15th, 2005
>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.
"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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: JayseR is an unknown quantity at this point 
Solved Threads: 0
JayseR JayseR is offline Offline
Newbie Poster

Re: Distance between 2 points

 
0
  #5
Mar 15th, 2005
Sorry, my bad, I'm pretty clueless sometimes. But yeah, I'm pretty sure I got it now. Thanks for all the help. Much appreciated.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 7844 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC