473,699 Members | 2,933 Online

# C++ project help Generating variables.

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(((x b-xa)*(xb-xa))+((yb-ya)*(yb-ya)));

return (length);

}

int main()

int points=0;
int ans=0;
double length(double,d ouble,double,do uble)
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.

Nov 27 '06 #1
28 1592
On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl. pipex.comwrote:
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::pair

int main()
{
std::vector<std ::pair<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_bac k(std::make_pai r(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

Nov 27 '06 #2
er****@student. chalmers.se <er****@student .chalmers.sewro te:
>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::pair
Another possibility is using std::complex<do ublefor the (x,y) pair.

Steve
Nov 27 '06 #3

er****@student. chalmers.se wrote:
On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl. pipex.comwrote:
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::pair

int main()
{
std::vector<std ::pair<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_bac k(std::make_pai r(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..

Nov 27 '06 #4
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

Nov 27 '06 #5

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.

Nov 27 '06 #6
On Nov 27, 10:28 am, "Nutkin" <jamesf...@dsl. pipex.comwrote:
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

Nov 27 '06 #7
On 27 Nov 2006 02:52:59 -0800, "er****@student .chalmers.se"
<er****@student .chalmers.sewro te:
>On Nov 27, 10:28 am, "Nutkin" <jamesf...@dsl. pipex.comwrote:
>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
Nov 27 '06 #8
On Nov 27, 3:10 pm, rossum <rossu...@coldm ail.comwrote:
On 27 Nov 2006 02:52:59 -0800, "eri...@student .chalmers.se"

<eri...@student .chalmers.sewro te:
On Nov 27, 10:28 am, "Nutkin" <jamesf...@dsl. pipex.comwrote:
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

Nov 27 '06 #9

er****@student. chalmers.se wrote in message ...

/* """

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::pair
int main(){
// stores the points int nrPoints;
std::vector<std ::pair<double, double points;
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;
// add the point to the collection
points.push_bac k(std::make_pai r(x,y));
}
// 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

""" */

You guys (include Steve Pope) are involving way too much complexity (yes,
Steve, a pun). You're not trying to hide simplicity from the boss, but, help
a starting student.

If the OP could use 'vector' (later states (s)he can't), a simple vector of
struct would make life easier (in the OPs assignment).

// class Point{ public: // same thing
struct Point{
double x;
double y;
Point() : x(0), y(0){}
};
{ // main() or function

std::vector<Poi ntParray( 10 );

Parray.at(0).x = 23.45;
Parray.at(0).y = 67.89;

std::cout<< "x=" << Parray.at(0).x
<< ", " << Parray.at(0).y <<std::endl;

for(size_t i( 0 ); i < Parray.size(); ++i){ // Notice NO "nrPoints -1"
std::cout<< "x=" << Parray.at( i ).x
<< ", " << Parray.at( i ).y <<std::endl;
} // for(i)

} // end

That's my 0.002 pico-cents **opinion**.
--
Bob R
POVrookie
Nov 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.