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

# 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.

eriwik@student.chalmers.se
Guest
Posts: n/a

 11-27-2006
On Nov 27, 8:07 am, "Nutkin" <(E-Mail Removed)> 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

Steve Pope
Guest
Posts: n/a

 11-27-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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

Nutkin
Guest
Posts: n/a

 11-27-2006

(E-Mail Removed) wrote:
> On Nov 27, 8:07 am, "Nutkin" <(E-Mail Removed)> 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..

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

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.

eriwik@student.chalmers.se
Guest
Posts: n/a

 11-27-2006
On Nov 27, 10:28 am, "Nutkin" <(E-Mail Removed)> 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

rossum
Guest
Posts: n/a

 11-27-2006
On 27 Nov 2006 02:52:59 -0800, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>On Nov 27, 10:28 am, "Nutkin" <(E-Mail Removed)> 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

eriwik@student.chalmers.se
Guest
Posts: n/a

 11-27-2006
On Nov 27, 3:10 pm, rossum <(E-Mail Removed)> wrote:
> On 27 Nov 2006 02:52:59 -0800, "(E-Mail Removed)"
>
> <(E-Mail Removed)> wrote:
> >On Nov 27, 10:28 am, "Nutkin" <(E-Mail Removed)> 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

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 =----