Thread: Creating graphs with 2 dimensional arrays

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    46

    Creating graphs with 2 dimensional arrays

    Hey guys. I have a question about how 2 dimensional arrays work. I am making a program that will graph functions (I'm starting easy like using 2x). To accomplish this, I am using a 2 dimensional array. Well, here is my question....

    Code:
     This is what the graph will look like with 3 plotted points (they will be 'o''s  for my graph but to identify them I put the as x,y, and z).
    x                             |
                                  |
                                  |
                                  |
                                  |
                                  |
    y---------------------------------------------------
                                  |
                                  |
                                  |
                                  |
                                  |
    z                             |
    What index positions in the array are the points at? Example, what index positions are x, y, and z at? I guess I am trying to figure out how to loop through all the points on the graph and decide which index positions get a space, -, |, or o.

    Thank you all for your help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The positions would depend on the values in your array and how you interpret them.

    Give it your best shot, post your code... I'm sure you'll get lots of help.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    This is my attempt at creating the initial graph without the plotted function on it. Ignore the X-SEMIRANGE stuff, that was a #define. It doesn't work, but maybe you can point out my mistakes. Thanks so much guys.

    Code:
    char makeGraph()
    {
         int x;
         int y;
         char graph[(Y_SEMIRANGE * 20) + 1][(X_SEMIRANGE * 20) + 1];
         for(y = 0; y < (Y_SEMIRANGE * 20) + 1; y++)
         {
               for(x = 0; x < (X_SEMIRANGE * 20) + 1; x++)
               {
                     if(x == X_SEMIRANGE / 2)
                          graph[y][x] = '-';
                     if(y == Y_SEMIRANGE / 2)
                          graph[y][x] = '|';
                     else
                         graph[y][x] = ' ';
                     printf("%c", graph[y][x]);
               }
         }
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Create the Y axis (using a simple loop)
    Create the X axis (using a simple loop)

    There is no reason to embed one loop inside the other.
    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
    Sep 2006
    Posts
    8,868
    Post up what it is you're trying to graph. Something more than the first post? No challenge in that graph. I have no clue what you're stumped on.

    You can take a screen shot of your screen (ctrl+PrnScr), paste it into your image viewer, and save it as a file. Then attach the file (using the manage attachments button near the bottom of the edit screen of the forum).

    Think of your screen as a bunch of x and y points. You want to print from top to bottom rows, and from left to row columns.

    The screen is your canvas.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Quote Originally Posted by Adak View Post
    Post up what it is you're trying to graph. Something more than the first post? No challenge in that graph. I have no clue what you're stumped on.

    You can take a screen shot of your screen (ctrl+PrnScr), paste it into your image viewer, and save it as a file. Then attach the file (using the manage attachments button near the bottom of the edit screen of the forum).

    Think of your screen as a bunch of x and y points. You want to print from top to bottom rows, and from left to row columns.

    The screen is your canvas.
    I just want to make something that looks like this for now. If I could make this, I'm sure I can plot some function on it.

    Code:
                                               |
                                               |
                                               |
                                               |
            -----------------------------------|-----------------------------
                                               |
                                               |
                                               |
                                               | 
                                               |

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have some reservations, but anyway:
    Code:
    #include <stdio.h>
    #define MIDX 40
    #define XMAX 41
    #define MIDY 11
    #define YMAX 18
    
    int main() {
      int x,y; 
      printf("\n\n\n");
      for(y=0;y<YMAX;y++) {
         if(y==MIDY) {
          printf("--------------------------------------------------------------------------------");
        }
        for(x=0;x<XMAX;x++) {
          if(x!=MIDX)
            putchar(' ');
          else
            putchar('|');
        }
        putchar('\n');
      }
    
      printf("\n\n\t\t\t     press enter when ready");
    
      (void) getchar(); 
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Hey adak, thanks for your post. I now am able to create the y-axis on the graph, but for some reason the x-axis is not being loaded into the array (or maybe I am just not using printf to output the x-axis correctly). Here is my code so far.

    Also - Part of the assignment is to accomplish this using a 2-dimensional array. Thanks all!

    Heres my code so far:
    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define X_SEMIRANGE 3
    #define Y_SEMIRANGE 3
    
    double f(double); //prototype of the mathematical function to be graphed
    char plot(double); //prototype for the function that will plot the graph of f
    char makeGraph(); //prototype for the function that intializes the graph to spaces and the x/y coordinates
    
    double f(double x)
    {
         return (2*x);
    }
    
    char makeGraph()
    {
         int x;
         int y;
         int Y_AXIS = (Y_SEMIRANGE * 20) + 1;
         int X_AXIS = (X_SEMIRANGE * 20) + 1;
         char graph[Y_AXIS][X_AXIS];
         
         /*The following will create the x and y axis on the graph*/
         for(y = 0; y < Y_AXIS; y++)
         {
               for(x = 0; x < X_AXIS; x++)
               {
                    if(y == Y_AXIS / 2)
                         graph[y][x] = '-';
                    if(x == X_AXIS / 2)
                         graph[y][x] = '|';
                    else
                        graph[y][x] = ' ';
               }
         }
         //end creating x and y axis
         
         //The following will output the graph of the x and y axis
         for(y = 0; y < Y_AXIS; y++)
         {
               for(x = 0; x < X_AXIS; x++)
               {
                     printf("%c", graph[y][x]);
               }
               printf("\n");
         }
         //end outputting the x and y axis
         
    }
    
    int intCast(double x)
    {
         int i = x;
         
         if (x >= 0)
             i = (int)(x + 0.5);
         else /* x < 0 */
             i = (int)(x - 0.5);
             
         return i;
    }
    
    /*char plot(double x)
    {
    
    
    }*/
    
    int main()
    {
       makeGraph();
       system("PAUSE");
       return 0;
    }
    What the output looks like:
    Code:
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
                                  |
    It's missing the x-axis for some reason.

    Finally, I am very puzzled when it comes to plotting a function on this graph. For instance, if the function is y = 2*x we get....
    (-2,-4)
    (-1,-2)
    (0,0) (the origin)
    (1,2)
    (2,4) etc....

    In index positions on my array(if my array were to hold up to [10][10]) that would be....
    [9][3]
    [7][4]
    [5][5] (the origin)
    [3][6]
    [1][7] etc....

    How do I translate the mathematical coordinates to the array index positions?
    Last edited by askinne2; 10-20-2010 at 12:26 PM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Am I going about this wrong?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This will initialize the graph array to all spaces, except the vertical and horizontal lines.

    Code:
    #include <stdio.h>
    
    #define MIDX 38
    #define XMAX 78
    #define MIDY 11
    #define YMAX 22
    
    int main() {
      int i, j, r, c; 
      char graf[XMAX][YMAX];
      printf("\n\n ");
      for(i=0;i<YMAX;i++) {
        for(j=0;j<XMAX;j++) {
          graf[i][j]= ' ';
          if(i==MIDY)
            graf[i][j]= '-';
          if(j==MIDX)
            graf[i][j]= '|';
          putchar(graf[i][j]);
        }
        printf("\n ");
      }
      printf("\n\t\t\t     press enter when ready");
    
      (void) getchar(); 
      return 0;
    }
    On my screen, it's centered.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Thanks for your help Adak! I now have the the x and y axis correctly plotted.

    Now for the last little push, how do I go about plotting a simple function on the graph?

    Here is my attempt at a function to plot f(x) = 2*x;

    Code:
    char plot()
    {
         double y, x;
         int X_INDEX, Y_INDEX, X_INT, Y_INT;
         for(x = -(X_SEMIRANGE); x < X_AXIS; x++)
         {
             y = f(x);
             Y_INT = intCast(y);
             X_INT = intCast(x);
             
             //this is where I am stuck
            
    }
    Ok, so how to I translate these numbers to the correct index positions?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know.

    You have to first decide which way the graph will increase: the direction of that increase. Will it be up from the bottom, or to the right side (from the left), or down from the top, or to the left, from the right hand side.

    Look at some graphs and decide what you want. There are ALL kinds of graphs, and you need to know EXACTLY what you want, or your program will flop (or take forever to complete the code, because you'll want to keep changing it).

    That's the next thing - you read up on graphs types, and decide what you want, how big your range of data will need to be (that helps decide the number of gradients that you will show), etc.

    For a console display, you want to use only 75 horizontal X 22 vertical data points, max.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Here you have a vertical x axis and can change 3 display-parameters:
    Ideone.com | R6kJV

  14. #14
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Thanks for responses again. I'm ALMOST there! Let me post my code, bold the part that is causing me my (hopefully) final problem, and knock it out!

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define X_SEMIRANGE 3
    #define Y_SEMIRANGE 3
    
    double f(double); //prototype of the mathematical function to be graphed
    char plot(); //prototype for the function that will plot the graph of f
    char makeGraph(); //prototype for the function that intializes the graph to spaces and the x/y coordinates
    int intCast(double); //prototype for the function that type casts a double to int correctly
    
    double f(double x)
    {
         return (2*x);
    }
    
    char makeGraph()
    {
         int x, y, i, j;
         double xx, yy;
         int Y_AXIS = (Y_SEMIRANGE * 20) + 1;
         int X_AXIS = (X_SEMIRANGE * 20) + 1;
         int MID_Y = Y_AXIS / 2;
         int MID_X = X_AXIS / 2;
         int Y_INT, X_INT, Y_POS, X_POS;
         char graph[Y_AXIS][X_AXIS];
         
         for(y = 0; y < Y_AXIS; y++)  //This nest loop should create the x and y axis
         {
               for(x = 0; x < X_AXIS; x++)
               {
                    graph[y][x] = ' ';
                    if(y == MID_Y)
                         graph[y][x] = '-';
                    if(x == MID_X)
                         graph[y][x] = '|';
                    //putchar(graph[y][x]);
               }
               printf("\n");
         }
         //end creating x and y axis
         
         for(xx = -(X_SEMIRANGE); xx < X_AXIS; xx++)  //This loop should insert o's for all points on the function
         {
              yy = f(xx);
              Y_INT = intCast(yy);
              X_INT = intCast(xx);
              Y_POS = MID_Y + Y_INT;
              X_POS = MID_X + X_INT;
              graph[Y_POS][X_POS] = 'o';
         }
         
         for(i = 0; i < Y_AXIS; i++)  //This should loop through index positions and output the graph
         {
               for(j = 0; j < X_AXIS; j++)
               {
                     putchar(graph[i][j]);
               }
         }
    }
    
    int intCast(double x)
    {
         int i = x;
         
         if (x >= 0)
             i = (int)(x + 0.5);
         else /* x < 0 */
             i = (int)(x - 0.5);
             
         return i;
    }
    
    int main()
    {
       makeGraph();
       system("PAUSE");
       return 0;
    }
    The bolded code causes the program to crash. I think I have the right idea on how I am loading the o's on the graph (which are the plotted points on the function line). Am I doing this correctly, and am I outputting the graph correctly? Obviously I am wrong somewhere, because the program is crashing.

    Thanks for the guidance!

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look at my code - you left out the putchar('\n'), after the inner loop. (the graph will be at the end of the row).

    You should be getting a warning or an error message from your debugger/compiler. I don't know what the heck you're doing with SEMIRANGE stuff.

    Nobody knows your code better than you do, and you can step through it in your debugger, as well or better, than anyone else. You're the right person to debug that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with creating arrays outiside functions
    By kaylors in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2009, 11:04 AM
  2. Help with 2 dimensional arrays
    By riotact in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:19 AM
  3. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. populating two dimensional arrays
    By garycastillo in forum C Programming
    Replies: 2
    Last Post: 04-05-2002, 08:22 PM