Thread: Creating graphs with array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Creating graphs with array

    I want my output to look like a bar graph with the number of numbers in an array in each line.

    My code is not working and I don't know why.

    Code:
    void graph(int a[], int lo, int hi)
    {
       int x,y;
       for(y=(hi-1); y>=0; y--)
       {
          if(y%2==0)
          {
             cout<<setw(2)<<y<<"+";
          }
          else
          {
             cout<<setw(2)<<" "<<"|";
          }
          for(x=lo; x<hi; x++)
          {
             if(a[x]>=y)
                cout<<setw(2)<<"X";
             else
                cout<<" ";
          }
       }
    }

    It doesn't look like a graph at all. Please help.

    I want it to look like the graph here p11.html

    This is just my function prototype

    Thanks

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i think you need to post a bit more code, like the setw() function, does this do additional formatting, because it looks like that is all you are missing.
    I just assumed some arbitrary values for your function arguments, removed the setw() and added a newline after the x loop, the Yaxis shows fine like this and the X axis is almost there

    Code:
    #include<iostream>
    #include<stdlib.h>
    
    using namespace std;
    
    const int HIGH = 19;
    const int LOW = 0;
    
    void graph(int a[HIGH], int lo, int hi)
    {
       int x,y;
       
       for(y=(hi-1); y>=0; y--)
       {
          if(y%2==0)
          {
             cout <<y<<"+";
          }
          else
          {
             cout<<" "<<"|";
          }
          for(x=lo; x<hi; x++)
          {
             if(a[x]>= y)
                cout<<"X";
             else
                cout<<" ";
          }
          cout << "\n";
       }
    }
    
    
    int main()
    {
    
        int a[HIGH] = {2, 5, 9, 15, 3, 7, 12, 4, 0, 2, 14, 3, 12, 9, 6, 13, 1, 8, 3};
        int lo = LOW;
        int hi = HIGH;
    
        graph(a, lo, hi);
    
    return 0;
    }
    Sure you can sort it out from there, but if you want anything like the sample you posted you should use some kind of set cursor function to accomodate all the other fields
    Last edited by rogster001; 02-17-2010 at 02:08 AM. Reason: add example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating instances of objects in an array?
    By stumon in forum C# Programming
    Replies: 5
    Last Post: 10-27-2009, 07:37 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  4. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM