473,804 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

----== 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 =----
Nov 27 '06 #12
er****@student. chalmers.se <er****@student .chalmers.sewro te:
#include <map// to get std::pair
std::pair is actually in <utility>.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 27 '06 #13

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

Nov 27 '06 #14
BobR <Re***********@ worldnet.att.ne twrote:
>
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<doubleb uilt 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
Nov 27 '06 #15

"Nutkin" <ja*******@dsl. pipex.comwrote in message
news:11******** *************@j 44g2000cwa.goog legroups.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(((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
:)
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<Poi nt2DPoints;

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_bac k( 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<Poi nt2D>::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.
Nov 28 '06 #16

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<doubleC D;
// `complex' undeclared (first use this function)
std::complex<do ubleCD;
//`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
Nov 28 '06 #17
BobR <Re***********@ worldnet.att.ne twrote:
>Steve Pope wrote in message ...
>>So... since the language already has complex<double* *built in,**
>Nope!

complex<doubleC D;
// `complex' undeclared (first use this function)
std::complex<do ubleCD;
//`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
Nov 28 '06 #18

Steve Pope wrote in message ...
>BobR wrote:
>>Steve Pope wrote in message ...
>>>So... since the language already has complex<double* *built in,**
>>Nope!
complex<doubleC D;
// `complex' undeclared (first use this function)
std::complex<do ubleCD;
//`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
Nov 28 '06 #19
On 2006-11-27 20:41, Marcus Kwok wrote:
er****@student. chalmers.se <er****@student .chalmers.sewro te:
>#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
Nov 28 '06 #20

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

Similar topics

3
1323
by: Le, Thanh-Nhan | last post by:
Hi, I am a beginer in .NET. Is there an assistant or a free tool as in VB6 for generating a complete project frame in VS .NET. Thanks Nhan
7
1254
by: vladislav.moltchanov | last post by:
I countinue to discover problems for simple tasks. Now I would like to write code, Sub or Function, where some values (more than 1) are calculated and are to be assigned to the variables which names I would like to submit as arguments to this SUB /Function How to do this. -- Vlad. Moltchanov
15
3092
by: MR | last post by:
i need to develop a SOAP client, Since I have never personally done one I would like to make sure that I am going about it correctly. The client is a Windows (probably 2k3) application that communicates over HTTPS SOAP. The remote server is Unix based and implements Axis, which I know nothing about What are the steps I need to create this client? I will be developing in C# and I have the XML schema of the SOAP messages. How do I get...
4
1249
by: solrick51 | last post by:
Dear community, I dont really know if this is the right way to do, otherwise my excuses for that. I'm a Dutch student, and graduating this year on my graphic design study. For my graduating study, I want to do some Python work and i'm lokking for a partner wich can help/ cooperate me in programming python.
12
1378
by: E.D.G. | last post by:
Important Research Project (Related to computer programming) Posted by E.D.G. on August 30, 2007 edgrsprj@ix.netcom.com This report is being posted to a number of Internet Newsgroups to see if there are any experienced computer programmers who would like to provide some assistance with an effort to develop a Perl language computer program. Interested parties can try contacting me by e-mail or by posting a response note to the...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.