Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ project help Generating variables.

Reply
Thread Tools

C++ project help Generating variables.

 
 
Nutkin
Guest
Posts: n/a
 
      11-27-2006
Basicly i have to make a program that calculates the distance between x
and y points in 2d space.
the code basicly goes like this


1. User says how many points they have (max of 10)

2. User enters points

3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points

4. It displays the length between the first and last point.


My problem is how do i accept the data. im not sure how to vary the
number of inputs or how to declare the variables. like say the user
wants 6 points how do i let the program know only to ask the user for 6
points. and then how do i do the same calculation for each of those
points.


i tried using a while loop and heres my code so far.

#include <iostream>;
#include <cmath.h>

using namespace std;

double length(double xa,double xb,double ya,double yb)
{
double length=0;

length=sqrt(((xb-xa)*(xb-xa))+((yb-ya)*(yb-ya)));

return (length);

}

int main()

int points=0;
int ans=0;
double length(double,double,double,double)
double xa=0;
double xb=0;
double ya=0;
double yb=0;



cout <<"How many points would you like to input (Max 10)?\n\n";
cin >>points;


while (points > 1)
{
cout <<"Please enter an x value\n";
cin >>xa;
cout <<"Please enter a y value\n";
cin >>ya;
cout <<"Please enter an x value\n";
cin >>xb;
cout <<"Please enter a y value\n";
cin >>yb;

ans=ans+length(xa,xb,ya,yb)

points=points-1;
}

return (0);



Im using VC++


i know the codes a little crappy but hey thats what help is for right


Thanks in advance to any genius who can sort this mess out.

 
Reply With Quote
 
 
 
 
eriwik@student.chalmers.se
Guest
Posts: n/a
 
      11-27-2006
On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl.pipex.com> wrote:
> Basicly i have to make a program that calculates the distance between x
> and y points in 2d space.
> the code basicly goes like this
>
> 1. User says how many points they have (max of 10)
>
> 2. User enters points
>
> 3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
> between 2 points
>
> 4. It displays the length between the first and last point.
>
> My problem is how do i accept the data. im not sure how to vary the
> number of inputs or how to declare the variables. like say the user
> wants 6 points how do i let the program know only to ask the user for 6
> points. and then how do i do the same calculation for each of those
> points.


I thin you want to use a container-class to store the points in,
something like this:

#include <iostream>
#include <vector>
#include <map> // to get std:air

int main()
{
std::vector<std:air<double, double> > points; // stores the points
int nrPoints;

std::cout << "Number of points: ";
std::cin >> nrPoints;

for (int i = 0; i < nrPoints; ++i) // For-loops are nice, but can use
while too
{
double x, y;
std::cout << "Enter x: ";
std::cin >> x;
std::cout << "Enter y: ";
std::cin >> y;

points.push_back(std::make_pair(x,y)); // add the point to the
collection
}

// Calculate distance

return 0;
}

Then you can access the points just like this:

points[2].first // The x-value of the third point (starts from 0)
points[2].second // The y-value of the second point

To get the distance I'd do something like this:

for (int i = 0; i < nrPoints - 1; ++i) // Notice nrPoints -1
{
ans += length(points[i].first, points[i+1].first, points[i].second,
points[i+1].second)
}

You could also use two vectors, one for x- and one for y-values.

--
Erik Wikstöm

 
Reply With Quote
 
 
 
 
Steve Pope
Guest
Posts: n/a
 
      11-27-2006
<> wrote:

>I thin you want to use a container-class to store the points in,
>something like this:
>
>#include <iostream>
>#include <vector>
>#include <map> // to get std:air


Another possibility is using std::complex<double> for the (x,y) pair.

Steve
 
Reply With Quote
 
Nutkin
Guest
Posts: n/a
 
      11-27-2006

wrote:
> On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl.pipex.com> wrote:
> > Basicly i have to make a program that calculates the distance between x
> > and y points in 2d space.
> > the code basicly goes like this
> >
> > 1. User says how many points they have (max of 10)
> >
> > 2. User enters points
> >
> > 3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
> > between 2 points
> >
> > 4. It displays the length between the first and last point.
> >
> > My problem is how do i accept the data. im not sure how to vary the
> > number of inputs or how to declare the variables. like say the user
> > wants 6 points how do i let the program know only to ask the user for 6
> > points. and then how do i do the same calculation for each of those
> > points.

>
> I thin you want to use a container-class to store the points in,
> something like this:
>
> #include <iostream>
> #include <vector>
> #include <map> // to get std:air
>
> int main()
> {
> std::vector<std:air<double, double> > points; // stores the points
> int nrPoints;
>
> std::cout << "Number of points: ";
> std::cin >> nrPoints;
>
> for (int i = 0; i < nrPoints; ++i) // For-loops are nice, but can use
> while too
> {
> double x, y;
> std::cout << "Enter x: ";
> std::cin >> x;
> std::cout << "Enter y: ";
> std::cin >> y;
>
> points.push_back(std::make_pair(x,y)); // add the point to the
> collection
> }
>
> // Calculate distance
>
> return 0;
> }
>
> Then you can access the points just like this:
>
> points[2].first // The x-value of the third point (starts from 0)
> points[2].second // The y-value of the second point
>
> To get the distance I'd do something like this:
>
> for (int i = 0; i < nrPoints - 1; ++i) // Notice nrPoints -1
> {
> ans += length(points[i].first, points[i+1].first, points[i].second,
> points[i+1].second)
> }
>
> You could also use two vectors, one for x- and one for y-values.
>
> --
> Erik Wikstöm




I think thats a bit over my head but i can see where you are coming
from. So i shall study it a bit more and see if i can get away with
using it. But we havnt been taught vectors yet so im not sure if it
will be valid. Thanks so much for giving me some options im gonna have
a play about with ti now.



If anyone has a more basic idea would be awesome..

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      11-27-2006
Nutkin wrote:
> Basicly i have to make a program that calculates the distance between x
> and y points in 2d space.
> the code basicly goes like this
>
>
> 1. User says how many points they have (max of 10)
>
> 2. User enters points
>
> 3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
> between 2 points
>
> 4. It displays the length between the first and last point.
>
>
> My problem is how do i accept the data. im not sure how to vary the
> number of inputs or how to declare the variables. like say the user
> wants 6 points how do i let the program know only to ask the user for 6
> points. and then how do i do the same calculation for each of those
> points.
>
>
> i tried using a while loop and heres my code so far.


Your code looks to be on the right track, but I would rethink using an
int for the ans variable. Should it not be capable of holding values
like 0.5 too?

For checking the users input, you could use an infinite loop and break
out of it when you have checked the input is valid:

int points = 0;
// for loop continues until number of points is valid
for (;{
std::cout <<"How many points would you like to input (Max 10)? :
";
std::cin >> points
if ((points < 2) || (points > 10)){
std::cout << "number of points has to be between 2 and 10\n";
}
else {
break;
}
}

you could also use a while loop or a do loop instead of course.

regards
Andy Little

 
Reply With Quote
 
Nutkin
Guest
Posts: n/a
 
      11-27-2006

kwikius wrote:
> Nutkin wrote:
> > Basicly i have to make a program that calculates the distance between x
> > and y points in 2d space.
> > the code basicly goes like this
> >
> >
> > 1. User says how many points they have (max of 10)
> >
> > 2. User enters points
> >
> > 3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
> > between 2 points
> >
> > 4. It displays the length between the first and last point.
> >
> >
> > My problem is how do i accept the data. im not sure how to vary the
> > number of inputs or how to declare the variables. like say the user
> > wants 6 points how do i let the program know only to ask the user for 6
> > points. and then how do i do the same calculation for each of those
> > points.
> >
> >
> > i tried using a while loop and heres my code so far.

>
> Your code looks to be on the right track, but I would rethink using an
> int for the ans variable. Should it not be capable of holding values
> like 0.5 too?
>
> For checking the users input, you could use an infinite loop and break
> out of it when you have checked the input is valid:
>
> int points = 0;
> // for loop continues until number of points is valid
> for (;{
> std::cout <<"How many points would you like to input (Max 10)? :
> ";
> std::cin >> points
> if ((points < 2) || (points > 10)){
> std::cout << "number of points has to be between 2 and 10\n";
> }
> else {
> break;
> }
> }
>
> you could also use a while loop or a do loop instead of course.
>
> regards
> Andy Little



Ive now been told i must use arrays ahhh i hate my uni.

 
Reply With Quote
 
eriwik@student.chalmers.se
Guest
Posts: n/a
 
      11-27-2006
On Nov 27, 10:28 am, "Nutkin" <jamesf...@dsl.pipex.com> wrote:
> Ive now been told i must use arrays ahhh i hate my uni.


Then first read in the number of points wanted then dynamically
allocate the array using new, then the rest should be quite like what's
already discussed. Don't forget to deallocate the array when you are
done with it (using delete).

--
Erik Wikström

 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      11-27-2006
On 27 Nov 2006 02:52:59 -0800, ""
<> wrote:

>On Nov 27, 10:28 am, "Nutkin" <jamesf...@dsl.pipex.com> wrote:
>> Ive now been told i must use arrays ahhh i hate my uni.

>
>Then first read in the number of points wanted then dynamically
>allocate the array using new, then the rest should be quite like what's
>already discussed. Don't forget to deallocate the array when you are
>done with it (using delete).


No need to bo so complex. We know that there will be a maximum of 10
points so allocate a fixed size array big enough to hold 10 points.
With small amounts of data there is no need to get into the
complexities of dynamic arrays. The OP may not have studied new and
delete yet, this assignment seems to be quite simple and likely to be
early in the course.

rossum


 
Reply With Quote
 
eriwik@student.chalmers.se
Guest
Posts: n/a
 
      11-27-2006
On Nov 27, 3:10 pm, rossum <rossu...@coldmail.com> wrote:
> On 27 Nov 2006 02:52:59 -0800, "eri...@student.chalmers.se"
>
> <eri...@student.chalmers.se> wrote:
> >On Nov 27, 10:28 am, "Nutkin" <jamesf...@dsl.pipex.com> wrote:
> >> Ive now been told i must use arrays ahhh i hate my uni.

>
> >Then first read in the number of points wanted then dynamically
> >allocate the array using new, then the rest should be quite like what's
> >already discussed. Don't forget to deallocate the array when you are
> >done with it (using delete).No need to bo so complex. We know that there will be a maximum of 10

> points so allocate a fixed size array big enough to hold 10 points.
> With small amounts of data there is no need to get into the
> complexities of dynamic arrays. The OP may not have studied new and
> delete yet, this assignment seems to be quite simple and likely to be
> early in the course.


Right you are, I forgot about the max 10 part.

--
Erik Wikström

 
Reply With Quote
 
Kevin Handy
Guest
Posts: n/a
 
      11-27-2006
Nutkin wrote:
> Basicly i have to make a program that calculates the distance between x
> and y points in 2d space.
> the code basicly goes like this
>
>
> 1. User says how many points they have (max of 10)
>
> 2. User enters points
>
> 3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
> between 2 points
>
> 4. It displays the length between the first and last point.


Why bother entering all the data points, when you are
only going to display the first and last points entered,
ignoring the rest? If you made me enter 1000 points,
then ignored all but the first and last entry, I'd
probably get ****ed off.

> My problem is how do i accept the data. im not sure how to vary the
> number of inputs or how to declare the variables. like say the user
> wants 6 points how do i let the program know only to ask the user for 6
> points. and then how do i do the same calculation for each of those
> points.


Assuming you want the difference between point 1 and
last entered point, just enter/store point 1, then loop
through additional points (input, calculate, display, repeat).
Only need storage for two points at a time (point 1
and current point)

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need ur help for my Masters project(TASM project) Mohanajeeva.D C Programming 2 07-02-2010 09:25 PM
tool for generating UML diagrams and project analysis finest137@yahoo.com C++ 0 07-27-2005 08:43 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM
Error while trying to run project: Unable to start debugging on the web server. The project is not configured Ken Stealth ASP .Net 2 01-31-2004 05:46 PM
Error while trying to run project: Unable to start debugging on the web server. The project is not configured to be debugged. windows 2003 server Claude seraphin ASP .Net 11 01-26-2004 09:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57