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

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    55

    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.

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which OS/Compiler are you using?

    How many points are you wanting to plot, what range of values?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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
    Last edited by Khadafi; 10-31-2011 at 05:26 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So would some *'s placed on a console would be sufficient resolution?
    Win32 Console Applications 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, sorry I was working on code to make sure that the points i need to create a graph for are correct.

    I have tried your tutorial

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2011, 11:30 AM
  2. Find corners in array of points
    By John Erlandsson in forum C Programming
    Replies: 12
    Last Post: 05-22-2011, 04:11 PM
  3. How to plot a graph using C
    By Katie_lin in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 02:38 PM
  4. plot a graph on the screen
    By dionys in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 02:46 PM
  5. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM