C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ project help Generating variables.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Nutkin
Guest





PostPosted: Mon Nov 27, 2006 10:10 am    Post subject: C++ project help Generating variables. Reply with quote



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.
Back to top
Steve Pope
Guest





PostPosted: Mon Nov 27, 2006 10:10 am    Post subject: Re: C++ project help Generating variables. Reply with quote



eriwik (AT) student (DOT) chalmers.se <eriwik (AT) student (DOT) chalmers.se> wrote:

Quote:
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
Back to top
kwikius
Guest





PostPosted: Mon Nov 27, 2006 10:10 am    Post subject: Re: C++ project help Generating variables. Reply with quote



Nutkin wrote:
Quote:
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 (;Wink{
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
Back to top
Nutkin
Guest





PostPosted: Mon Nov 27, 2006 10:10 am    Post subject: Re: C++ project help Generating variables. Reply with quote

eriwik (AT) student (DOT) chalmers.se wrote:
Quote:
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::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[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..
Back to top
eriwik@student.chalmers.s
Guest





PostPosted: Mon Nov 27, 2006 10:10 am    Post subject: Re: C++ project help Generating variables. Reply with quote

On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl.pipex.com> wrote:
Quote:
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[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
Back to top
Nutkin
Guest





PostPosted: Mon Nov 27, 2006 10:10 am    Post subject: Re: C++ project help Generating variables. Reply with quote

kwikius wrote:
Quote:
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 (;Wink{
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.
Back to top
Erik Wikström
Guest





PostPosted: Tue Nov 28, 2006 10:10 am    Post subject: Re: C++ project help Generating variables. Reply with quote

On 2006-11-27 20:41, Marcus Kwok wrote:
Quote:
eriwik (AT) student (DOT) chalmers.se <eriwik (AT) student (DOT) chalmers.se> wrote:
#include <map> // to get std::pair

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.

--
Erik Wikström
Back to top
kwikius
Guest





PostPosted: Wed Nov 29, 2006 10:11 am    Post subject: Re: C++ project help Generating variables. Reply with quote

BobR wrote:

Quote:
'goto' gives some C++ programmers 'the bends'! :-}

I see. Sincere Apologies for that. OK, now I got rid of the gotos. How
about this version? I'm making cool use of exceptions to keep stuff
moving in this version. Better now huh?

regards
Andy Little

#include <iostream>
#include <cmath>
#include <string>


// a 2D vector type
struct vect{
double x,y;
vect()Mad(0),y(0){}
vect (double const & x_in, double const & y_in)
Mad(x_in),y(y_in){}
};


inline
vect operator -(vect const & lhs, vect const & rhs)
{
return vect(lhs.x-rhs.x,lhs.y - rhs.y);
}


std::istream & operator >>(std::istream & in, vect & pt)
{
in >> pt.x >> pt.y;
return in;
}


std::ostream & operator <<(std::ostream & out, vect const & pt)
{
out <<'(' << pt.x << ','<< pt.y << ')';
return out;
}


inline
double magnitude(vect const & v)
{
return std::sqrt(v.x * v.x + v.y * v.y);
}


typedef vect point;

double mag_n( point const & p_in, int current_point, int const
num_points)
{
int next_point = current_point + 1;
std::cout << "Enter point number " << next_point << " : ";
point p;
std::cin >> p;
double result = magnitude( p - p_in);
if( next_point < num_points) {
result += mag_n(p,next_point, num_points);
}
return result;
}

// exceptions..
struct good_input{
int n_points;
good_input(int n_points_in):n_points(n_points_in){}
};
struct quit{ int f(){return 0;}};

int main()
{
try{
for(;Wink{
try{
while(1){
int num_points = 0;
std::cout << "Enter num points to input : ";
std::cin >> num_points; std::cout << '\n';
if ((num_points >=2) && (num_points <= 10)){
throw good_input(num_points);
}
else{
std::cout << "Error: must be 2 <= num points
<=10\n";
}
}
}
catch (good_input & e){

int num_points = e.n_points;
std::cout << "Enter first point:(syntax x y) : ";
point p;
std::cin >> p;
double result = mag_n(p,1, num_points) ;
std::cout << "path length = " << result <<'\n';
std::cout << "Do another set of points? (y/n) : ";
std::string str;
std::cin >> str;
if ( str.substr(0,1) != "y"){
throw quit();
}
}
}
}
catch (quit & e){return e.f();}
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.