473,699 Members | 2,933 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 #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.

Similar topics

3
1318
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
1249
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
3082
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
1243
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
1361
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
9187
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
9053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6540
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
5879
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
4390
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...
1
3071
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
2360
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.