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

# C++ project help Generating variables.

BobR
Guest
Posts: n/a

 11-27-2006

http://www.velocityreviews.com/forums/(E-Mail 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:air
int main(){
// stores the points int nrPoints;
std::vector<std:air<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[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<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**.
--
Bob R
POVrookie

BobR
Guest
Posts: n/a

 11-27-2006

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.

--
Bob R
POVrookie

Marcus Kwok
Guest
Posts: n/a

 11-27-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:
> #include <map> // to get std:air

std:air is actually in <utility>.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

kwikius
Guest
Posts: n/a

 11-27-2006

Kevin Handy 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.

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

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

Steve Pope
Guest
Posts: n/a

 11-28-2006
BobR <(E-Mail Removed)> wrote:

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

Jim Langston
Guest
Posts: n/a

 11-28-2006

"Nutkin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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:air, 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[i].X, Points[i].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.

BobR
Guest
Posts: n/a

 11-28-2006

Steve Pope wrote in message ...
>BobR wrote:
>
>>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,**

Nope!

complex<double> CD;
// `complex' undeclared (first use this function)
std::complex<double> CD;
//`complex' undeclared in namespace `std'

> so
>how do you figure it is any less complicated to declare your
>new struct "Point"??

No header needed! <G>

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

--
Bob R
POVrookie

Steve Pope
Guest
Posts: n/a

 11-28-2006
BobR <(E-Mail Removed)> wrote:

>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)
> std::complex<double> CD;
> //`complex' undeclared in namespace `std'

#include <complex>

>>Use what's already there, I say.

> That's the Point! It is/maybe not there yet for the student
> just starting arrays.

It should definitely be there.

Steve

BobR
Guest
Posts: n/a

 11-28-2006

Steve Pope wrote in message ...
>BobR wrote:
>
>>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)
>> std::complex<double> CD;
>> //`complex' undeclared in namespace `std'

>
>#include <complex>

Ah ha, so you admit it wasn't 'built-in'!?! <G>

[ sorry - the devil made me do it. ]
--
Bob R
POVrookie

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a

 11-28-2006
On 2006-11-27 20:41, Marcus Kwok wrote:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>> #include <map> // to get std:air

>
> std:air 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.

--
Erik Wikström

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Mohanajeeva.D C Programming 2 07-02-2010 09:25 PM finest137@yahoo.com C++ 0 07-27-2005 08:43 AM =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM Ken Stealth ASP .Net 2 01-31-2004 05:46 PM Claude seraphin ASP .Net 11 01-26-2004 09:17 PM

Advertisments