# Thread: C++ Plot Simple Points / Graph, X & Y array points

1. ## C++ Plot Simple Points / Graph, X & Y array points

Hi, I am creating a program and need to plot points to visualise if i have created the program correctly.

For example I have an
int array myX[2]; with elements example 1, 2
int array myY[2]; with elements example 5, 6

so ...
Point1 X and Y is 1,5
Point 2 X and Y is 2,6

i want to plot a simple graph to show roughly what shape the points make for example something that looks like

6| ____P2
5| __P1
4|
3|
2|
1|
0| ___________________
0__1__2__3__4__5

I dont need it to look fancy or anything just a simple thing to visualise the points.

I am not sure how to do this.

2. Which OS/Compiler are you using?

How many points are you wanting to plot, what range of values?

3. Hi, I am using Microsoft Visual Studio C++ 2010 Express, and using the programming language C++.

An example of number of points would be like 10 or 20, and range is like 0 to 25 for example.

Thanks

4. So would some *'s placed on a console would be sufficient resolution?
Win32 Console Applications 1

5. Hi, yes that would be perfect .
Thats what i have been trying but it dosnt come out right. I tried by using for loops and then cout to print a symbol for a point.
thanks

6. Hi, I have tried many times but I can't get it to print correctly.
I dont understand howi can get it to print the y in the right place (if there is no point on the y line before it) and in situations where there are 2 co-ordinates with the same x but different y values.

Thank You

7. Did you actually implement a form of gotoxy(), so you could address any row/column of the screen and put whatever you want there?

Also, saying "it doesn't work" and not posting the code isn't really going to allow us to help you.

8. Hi, sorry I dont know about gotoxy() ?

the code i tried is to sort all the element (xy) by the y so that the first element xy element has the largest y, and then go through a loop to print each x for each y element

Code:
```	cout << " Points Graph " << "\n" << "----------------" << "\n\n";
for(int i = totalelem-1; i>=0; i--)
{
cout << setw (array[i].x);
cout << "*" << endl;
}```
Thanks

9. So you didn't make it to page 3 of the tutorial I posted a link to in post #4.
What have you been doing for the past week?

10. Hi, sorry I was working on code to make sure that the points i need to create a graph for are correct.

and can get a graph to print great using the code

Code:
```for (int i = 0; i<QPX.size(); i++)
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);

Position.X = MYPX[i];
Position.Y = MYPY[i];
SetConsoleCursorPosition(hOut,
Position);
cout << "*";
}```
however i am using cout before and the points are printing where my other cout text is as it is in the place where position x and y is.
how can i make the graph start displaying after my other cout text.

Also if i want to create scales for x and y on the sides what would be the best way
|
|
|____

Thanks.