Thread: create a graph of y=x^2

  1. #1
    asimos
    Guest

    create a graph of y=x^2

    well i have this scholl project on c++
    i manage to create the x and y axis but my only problem is the graph it self
    in this specific project not allowed to use any graph libraries or anything equivalent only 4 chars space,*,-,|
    here is what i i did

    void main()
    {
    int i = 1;
    int x = 0;
    int y = 0;
    int k = 0;
    char a;

    const screen_length = 79;
    const screen_height = 25;
    const minx = 10;
    const maxx = -10;
    const miny = -5;
    const maxy = 100;
    int xstep = maxx - minx/79-1;
    //int xstep= screen_height/screen_length;

    while (i<screen_height)
    {
    //x = minx;

    while(k<screen_length)
    {
    if (y == x * x)
    cout<<'*';
    else
    if (k == screen_length/2)
    cout<<'|';
    else
    if (i == 24)
    cout<<'_';
    else
    cout<<' ';
    x++;
    k++;
    y++;
    }
    cout<<endl;
    k = 0;
    i++;
    y--;
    }

    cin>>a;
    }

  2. #2
    Unregistered
    Guest
    For variety, here's an alternate algorhythm that has a shot at producing an acceptable result.

    1) let x axis be vertical axis and y axis be horisontal
    2) x axis located on far left of screen
    3) y axis cuts left to right across the middle of the screen
    4) label x at top and y at far right
    5) label the 2 axis values with even numbers only starting with 2
    6) x axis depicted by | and y by - (or _);
    7) any point x y on the graph is identified by a *
    8) depict screen as 2D char array of dimension 25 by 80 allowing max x to be 8 or -8 and max y to be 64
    9) initialize array to all spaces
    10) use a number of loops to assign all non-calculated chars (everything but the *) to all the respective elements
    11) assign all points of the graph to * using a loop or two
    12) display array (that is the "graph") using yet another loop.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Yes, as the previous poster points out, your y gets big quick, so it displays better on the screen if the y is horizontal. Here is some code to display it in several pages. You could also write the graph to a file.
    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    { 
    int i = 1; 
    int x = 0; 
    int y = 100;
    int k = 0;
    char a; 
    
    const screen_length = 79; 
    const screen_height = 25;
    //const screen_height = 24;
    const minx = 10;
    const maxx = -10; 
    const miny = -5; 
    const maxy = 100; 
    int xstep = maxx - minx/79-1; 
    //int xstep= screen_height/screen_length; 
    
    while (i<=maxy) 
    { 
    //x = minx; 
    
       while(k<screen_length) 
       { 
          if (y == x * x) 
             cout<<'*'; 
          else if (k == 0)
             cout<<'|';
          //else if (i == 24)
          else if (i % screen_height == 0)
             cout<<'_';
          else 
             cout<<' '; 
          x++; 
          k++; 
       }
       if (i % screen_height == 0)
          getch();
       cout<<endl;
       k = 0;
       i++;
       x = 0;
       y--; 
    } 
    
    getch();
    }

  4. #4
    asimos
    Guest
    thx guys
    ill try them out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help w/ graph as adjacency matrix
    By ac251404 in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2006, 10:25 PM
  2. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM