English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm making a program for class that needs to ask

1) How many points on a graph there are.

int n;
cout<<"enter number of points on graph";
cin>>n;

2)Ask for the x and y values for each point based on the number of points.

Output would be:

Enter X_1:
Enter Y_1:
Enter X_2:
Enter Y_2:
Enter X_n:
Enter Y_n:

How would I go about getting this set up. I'm guessing something like:

for(int k=1; k<=n; k++)
{
cout<<"Enter the value of x_"< cin>> ; <--- What goes here?
cout<<"Enter the value of y_"< cin>> " <---What goes here?
}

Thats probably wrong, but I just don't know how to start it out to do it right and to have each number maintain the input value.

2007-02-07 11:40:31 · 2 answers · asked by ? 2 in Computers & Internet Programming & Design

2 answers

First of all, ask yourself what datatype is each point? It's a floating point value, so we'll use a double to store the points. Second, ask yourself how are you storing the points? It's a giant list of cartesian points, so there's a few ways to deal with that. A simple way right now is using a vector to store a sequence of doubles.

Include:
#include

Declare:
vector cartPoints;

Now you need to take in with cin a double and store it in the vector.
So you need a temporary storage variable:
double temp;

just do cin >> temp to get the double.
Then do cartPoints.push_back(temp) to store it in the vector.

2007-02-07 11:56:25 · answer #1 · answered by csanon 6 · 1 1

You didn't have it TOTALLY wrong, you actually did a very good start and had the right concept, you just need to learn to use arrays in cases like this, here is a moded version of your code

int array1[x];
int array2[y];
//if you have not gotten to understand dynamic arrays
//yet then simple use the HIGHEST possible value for your array,

for(int k=1; k<=n; k++)
{
cout<<"Enter the value of x_"< cin>> array[x]; <--- What goes here?
cout<<"Enter the value of y_"< cin>> array2[y]; " <---What goes here?
}

I'm not really sure how your doing your project, but since you posted just a for loop, i would assume you needed 2 arrays and not just one two dimensional array.

2007-02-07 11:50:06 · answer #2 · answered by D 4 · 1 2

fedest.com, questions and answers