944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 14643
  • C++ RSS
Mar 15th, 2005
0

Distance between 2 points

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayseR is offline Offline
6 posts
since Mar 2005
Mar 15th, 2005
0

Re: Distance between 2 points

>I am not sure how to isolate the first 4 points so I can

How about changing getdata just a wee bit.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 15th, 2005
0

Re: Distance between 2 points

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayseR is offline Offline
6 posts
since Mar 2005
Mar 15th, 2005
0

Re: Distance between 2 points

>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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 15th, 2005
0

Re: Distance between 2 points

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayseR is offline Offline
6 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: PUTTING C++ TO USE!!! (GUIs & MS Windows)
Next Thread in C++ Forum Timeline: Tic Tac Toe Homework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC