Having Trouble w/ simple graphing program; need some help

This is a discussion on Having Trouble w/ simple graphing program; need some help within the C Programming forums, part of the General Programming Boards category; Hey guys, I started on this simple plotting program, and everything seems to be fine with the program, no errors ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Having Trouble w/ simple graphing program; need some help

    Hey guys, I started on this simple plotting program, and everything seems to be fine with the program, no errors came up, but its not plotting the graph. Here's the code, and any suggestions would be more than appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define X_SEMIRANGE 3
    #define Y_SEMIRANGE 2
    
    int rtoi(double);
    double f(double);
    char graph[Y_SEMIRANGE*20+1][X_SEMIRANGE*20+1];/*store x and y points in a 
    character array*/
    int creategraph();
    int plot();
    int dispplot();
    
    int main()
    {
    	creategraph();
    	plot();
    	dispplot();
    	getchar();	
    	return 0;
    }
    
    int rtoi(double x) /*rounds the double values of f() to ints before storing
    points in the array*/
    {
    	int i;
    	if(x >= 0)
    		i = (int)(x + 0.5);
    	else /* x < 0 */
    		i = (int)(x - 0.5);
    	return i;
    }   
    
    double f(double x) /*holds the function to be graphed. Can be easily 
    overwritten*/
    {       
    	return cos(x*x);
    }
    		
    int creategraph()
    {
    	int i,j;
    	for (i=0; i<=X_SEMIRANGE*20+1; i++)/*for loop defines where axes should be
         placed*/
    	{
    		for (j=0; j<=Y_SEMIRANGE*20; j++)
    		{
    			if(i==(X_SEMIRANGE*20/2))
    				graph[j][i] = '|';
    			else if(j==(Y_SEMIRANGE*20/2))
    				graph[j][i]='-';
    			else
    				graph[j][i]=' ';/*inserts empty spaces in the array, this will
                     be replaced in the the graph*/
    		}
    	}
    }
    
    int plot()
    {
    	int x_coor,y_coor;
    	double range;
    	double func;
    	for(range=-1*((X_SEMIRANGE*20)/2); range <=((X_SEMIRANGE*20)/2); range++)
         /*for loop will plot the points onto graph, it starts from negative 
         X_SEMIRANGE and goes to the end of X_SEMIRANGE*/
    	{
    		func = (f(range));/*units in x direction will increment by 1/10*/
    		y_coor=(Y_SEMIRANGE)*20/2-rtoi(func);/*values are rounded and 
             plotted where the graph is*/
    		x_coor=(X_SEMIRANGE)*20+range;
    		if(y_coor >= 0 && y_coor <=20*Y_SEMIRANGE)
    			graph[y_coor][x_coor]='o'; /*inserts 'o' where I previousle had 
                empty spaces in the previous for loop*/
    	}
    }
    
    int dispplot()
    {
    	int i,j;
    	for(j=0; j <= Y_SEMIRANGE*20; j++)/*checks if any graph gets cut off
         by semirange*/
    	{
    		for(i=0; i <= (X_SEMIRANGE*20); i++)
    		{
    			printf("%c",graph[j][i]);
    		}
    		printf("\n");
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When a function has a data type listed on the left side of it's first line of the function, then it needs to return a data item of that type.

    In this case, three functions aren't returning int values. Your compiler should be warning you about that. Just add the line:

    Code:
    return 0;
    just before the closing brace for these functions, and the program works.

    With the values it displays now, the x axis is correct. The y axis center, I didn't measure, but it appears right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-30-2007, 11:08 PM
  2. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  5. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM

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