C++ project help Generating variables.

N

Nutkin

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

eriwik

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_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.first, points[i+1].first, points.second,
points[i+1].second)
}

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

Steve Pope

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<double> for the (x,y) pair.

Steve
 
N

Nutkin

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_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.first, points[i+1].first, points.second,
points[i+1].second)
}

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




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

kwikius

Nutkin said:
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
 
N

Nutkin

kwikius said:
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.
 
E

eriwik

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).
 
R

rossum

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
 
E

eriwik

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

Kevin Handy

Nutkin said:
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 pissed 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)
 
B

BobR

(e-mail address removed) 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_back(std::make_pair(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.first, points[i+1].first, points.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<Point> Parray( 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**.
 
B

BobR

Nutkin wrote in message ...
Ive now been told i must use arrays ahhh i hate my uni.

It's just the ol' "pencil-n-paper before calculator" thing.

Since you know the maximum number of elements, you could just set up the
array to max, and put the limits in input/output.

// #includes here
int main(){
std::size_t const sz( 10 );
double X[ sz ] = {0};
double Y[ sz ] = {0};
// - get the max number from user and check for range. -
for( std::size_t i( 0 ); i < NumFromUser; ++i ){
cin >> X[ i ];
cin >> Y[ i ];
if( not cin ){
std::cerr <<"ERROR!"<<std::endl;
return EXIT_FAILURE;
} // if(!cin)
} // for(i)
// - process the arrays -
return 0;
} // main()

If you have been taught 'new[]/delete[]/pointers', then that may be what your
instructor is after. Else, the above should do.
 
K

kwikius

Kevin said:
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 pissed off.


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)

But is it the distance from the first to last point, or the sums of
the lengths of discrete disconnected lines, or the length of the line
joining each point to the next?

The requirements arent clear AFAICS.

regards
Andy Little
 
S

Steve Pope

BobR said:
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){}
};

So... since the language already has complex<double> built in, so
how do you figure it is any less complicated to declare your
new struct "Point"??

Use what's already there, I say.

Steve
 
J

Jim Langston

Nutkin said:
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)

Okay, good. Store this in a variable.
2. User enters points

Okay, good. Store these values somewhere. A std::vector would be good, but
if that is beyond you, a dynamic array.
3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points

Which two? You entered 2 to 10 points. All of them to each other? 2-3,
then 3-4, then 4-5 ?
4. It displays the length between the first and last point.

Oh, I see. I think you want to total the paths. It's not truely the
distance between the first point and the last point, but the total of the
distance with the points inbetween.
My problem is how do i accept the data. im not sure how to vary the
number of inputs;

A for loop or while loop works for this.
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.

A vector or a dynamic array works for this.
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
:)

Well, your program would work with a little modification, but what about now
you want to know the average of the distances, etc? You are not storing the
points.

Two ways, like I said. A dynamic array, or a std::vector. I believe in
your case a std::vector would be prefered.
But, what do we push onto the vector? A point has two values, an X and a Y.
You could use std::pair, but a structure is probably prefered.

(untested code)

struct Point2D
{
double X;
double Y;
};
std::vector<Point2D> Points;

Okay, instead of a while loop I would use a for loop.

for ( int i = 0; i < points; ++i )
// It is customary to start counting in 0 in C++ because arrays are 0 bound
{
std::cout >> "Enter point number:" << i << ":";
Point2D Point;
std::cin >> Point.X >> Point.Y;
// Note, if they entered a bad value, such as "blah" then X and Y
have
// bad data. Excersice for the reader to deal with this //
Points.push_back( Point );
}

At this point, if all went well, you have a std::vector of Point2D that
cointains points number of points. You can access them with subscript
operator such as:

Points[0].X
Points[0].Y
etc...

You can also access them using iterators.

std::vector<Point2D>::iterator it = Points.begin();
(*it).X
(*it).Y

I generally use iterators, but for simplsticy sake, we'll use a for loop
again.

double TotalDistance = 0;
for ( int i = 0; i < points - 1; ++i )
// I would actually use
for ( size_t i = 0; i < Points.size() - 1; ++i )
// Whichever you understand better, but you should get to know both forms.
{
TotalDistance += length( Ponnts.X, Points.Y, Points[i+i].X,
Points[i+1].Y );
}

and there's your answer.

Read through all the replies given you and try to understand all of them.
If you're going to be using C++ you are going to need to learn the
containers, such as std::vector.
 
B

BobR

Steve Pope wrote in message ...
So... since the language already has complex<double> **built in,**

Nope!

complex<double> CD;
// `complex' undeclared (first use this function)
so
how do you figure it is any less complicated to declare your
new struct "Point"??

No header needed! said:
Use what's already there, I say.
Steve

That's the Point! It is/maybe not there yet for the student just starting
arrays. Seems to me that 'x, y' is easier than 'real, imag' for a student at
that stage (assuming (s)he isn't a math major <G>).
If the instructor won't accept 'vector', 'complex' would probably be
off-limits also.

If this were a guy modifying a library for Charles Schwab, I'd say "throw the
curve ball to him". For this instance, we need to put the ball on the tee and
teach the person to swing the bat.
 
S

Steve Pope

BobR said:
Steve Pope wrote in message ...
Nope!

complex<double> CD;
// `complex' undeclared (first use this function)
std::complex<double> CD;
//`complex' undeclared in namespace `std'
That's the Point! It is/maybe not there yet for the student
just starting arrays.

It should definitely be there.

Steve
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

std::pair is actually in <utility>.

I've always wondered where it was, but never really bothered since I've
only seldomn used it outside a map. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top