problem with plot a linear graph on the command line

This is a discussion on problem with plot a linear graph on the command line within the C Programming forums, part of the General Programming Boards category; Code: /*This program produce real numbers,x and y. The minimum number for x is 1,... and the maximum number is ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    problem with plot a linear graph on the command line

    Code:
    /*This program produce real numbers,x and y.
    The minimum number for x is 1,... and the maximum
    number is 14{...decimal places}because C is 15 spaces " "
    The minimum number for y is 1,... and the maximum
    number is 24 {...decimal places}because L is 25 spaces " " 
    C=WIDTH
    L=HEIGHT
    * * * * * * * * * * * * * * * * 		
    *			      *	
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *	                      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *			      *
    *                             *
    *                             *
    * * * * * * * * * * * * * * * *
    ^
    |
    (0,0)
    
    */
    #include<stdio.h>
    #define WIDTH 15
    #define HEIGHT 25
    int i;
    double my_function(double x);
    int main(void)
    {
    double x_spot,y_spot;
     for(i=WIDTH-1;i>0;i--)
      {
       x_spot=(double)i/WIDTH;
        y_spot=my_function(x_spot);
         printf("%lf\t%lf\n",x_spot,y_spot);
      }
    return 0;
    }
    double my_function(double x)
    {
    	double y;
    	y=x*x*x+x*x-x+0.2;
       if (y>0 && y<HEIGHT)/*only possitive values*/
       return y;
    }
    --------------------------------------------------------------------------------



    HOW I CAN PRINT THE GRAPH IN THE BOX?
    first i run the secondary programm and i place x y values in a *.txt file
    secondly i scanf the values and finally place for each coordinate one spot at the command line
    i must convert x and y into integers first (int)x=x_spot*HEIGHT; (int)y=y_spot*WIDTH;
    I dont understand how i could print the spot for each cordinate starting from the left down corner of
    the rectangle (0,0)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Draw it in memory before you draw it on screen

    Code:
    char paper[25][80];
    Start by filling this with spaces

    Then do
    paper[y][x] = '*';
    at the points you want to plot

    When you're done, just print the paper
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    explain

    Draw it in memory before you draw it on screen

    Code:
    char paper[25][80];

    Start by filling this with spaces

    Then do
    paper[y][x] = '*';
    at the points you want to plot

    My dimensions are 15*25..(what is 80 for?)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    It's for you to read between the lines and apply it to your specific situation
    If you can't change
    char paper[25][80];
    into
    char paper[15][25];
    I can't 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Sorry

    sorry !
    I REALLY APRECIATE YOUR HELP SALEM
    THANKS A LOT ):

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Salem what is wrong?

    Code:
    #include<stdio.h>
    #define WIDTH 25
    #define HEIGHT 15
    int main(void)
    {
    	char star='*',space=' ';
    	int y,x,c;
    	double y_spot,x_spot;
    	char my_graph[WIDTH][HEIGHT];
    	for(y=0;y<HEIGHT;y++)
    	 {
    	  for(x=0;x<WIDTH;x++)
    		{
    		 my_graph[y][x]=star;
    		}
    	 }
    	 do{
    		c=scanf("%lf %lf\n",&y_spot,&x_spot);
    		y_spot*=HEIGHT;
    		x_spot*=WIDTH;
    		my_graph[(int)x_spot][(int)y_spot]=space;
    	  }while(c!=EOF);
    	  
    	  for(y=0;y<HEIGHT;y++)
    	 {
    	  for(x=0;x<WIDTH;x++)
    		{
    		printf("%c",my_graph[y][x]);
    		}
    	 printf("\n");
    	 }
    return 0;
    }
    0.933333 0.950815
    0.866667 0.735407
    0.800000 0.552000
    0.733333 0.398815
    0.666667 0.274074
    0.600000 0.176000
    0.533333 0.102815
    0.466667 0.052741
    0.400000 0.024000
    0.333333 0.014815
    0.266667 0.023407
    0.200000 0.048000
    0.133333 0.086815
    0.066667 0.138074
    The y x spots

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Problem using DFS with a DAG graph
    By incognito54 in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 06:16 AM
  3. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  4. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21