Histogram C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rrusing
    New Member
    • Nov 2007
    • 2

    Histogram C++

    Hi,
    I'm a mathematics major, so the concepts behind this make sense, but I have the hardest problem putting it into computer language.

    So I am supposed to write a function that produces this histogram:

    Code:
        |                                         
    18+                                         
        |       
    16+       
        |        
    14+      
        |         
    12+     
        |    
    10+     
        |    
      8+        
        |       
      6+     
        |       
      4+     
        |   
      2+     
        |   
        +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
            0  1   2  3  4  5  6  7   8  9 10 1112 1314 15 161718 19 2021 22 2324 25
    Obviously since I created this by typing, it's a little off, but that's the idea behind the program.

    I have the following function (in progress, does not output the numbers on either the x or the y-axis):

    [CODE=cpp]void graph(int a[], int lo, int hi, int ymax)
    {
    int x, y;
    for (y=19; y>=-1; y--)
    {
    for (x=-2; x<=25; x++)
    if (y==0 && x>=0 && x%3==0)
    cout << "+";
    else if (y==0 && x>=0 && (x%2==0 || x%1==0))
    cout << "-";
    else if (x==0 && y>=0 && y%2==0)
    cout << "|";
    else if (x==0 && y>=0 && y%1==0)
    cout << "+";
    else
    cout << " ";
    }
    }[/CODE]


    Why do I get some horribly spread out mess?!

    I feel like I should be using x and y to represent the xy-axis, so when x==0 I'm referencing the origin and then making y be variable and vice versa, but the screen comes up jumbled.
    Last edited by Ganon11; Nov 16 '07, 10:50 PM. Reason: Please use the [CODE] tags provided.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    o when x==0 I'm referencing the origin
    How does that correspond in any way to output actually being at the "origin"? The console has no concept of graphs and positions and what not. You get line output, and that's about it.

    EDIT: Even if it did, your code is meaningless. How does setting x= 0 have any effect on the console output position?

    Comment

    • rrusing
      New Member
      • Nov 2007
      • 2

      #3
      Originally posted by oler1s
      How does that correspond in any way to output actually being at the "origin"? The console has no concept of graphs and positions and what not. You get line output, and that's about it.

      EDIT: Even if it did, your code is meaningless. How does setting x= 0 have any effect on the console output position?


      Obviously I'm having problems, but thanks for responding in the rudest way possible. I appreciate your help.

      Comment

      • AHMEDYO
        New Member
        • Nov 2007
        • 112

        #4
        HI...


        no man you can do that using VC++ and if VC++ have some restriction about Screen Graph when using consol application, C++ can do that by graph and colors too, i will see what i can do and i will post you again


        GOOD LUCK

        Comment

        • AHMEDYO
          New Member
          • Nov 2007
          • 112

          #5
          Hey Man..

          First i wanna say , when you have variables in your code as int x,y when you change values on x or y it doesnt effect any thing except value placed in memory but does not effect screen or anything like that

          and I Think that i got it for you!!!

          First include Windows.h Header File in your .Cpp File and use my WriteToScreen Function instread of cout.., i test it by your code and i got your result but it reserved by Y position :D lol , i think you can handle that,for sorry im not good in graph.

          [CODE=cpp]#include <Windows.h>

          void graph();
          DWORD WriteToScreen(L PSTR Text,WORD XPos,WORD YPos);

          int main(int argc, char* argv[])
          {
          graph();
          return 0;
          }

          DWORD WriteToScreen(L PSTR Text,DWORD n_char,WORD XPos,WORD YPos)
          {
          HANDLE Outputhandle=Ge tStdHandle(STD_ OUTPUT_HANDLE);
          COORD ConsolCursor;
          DWORD cbwchars=0;
          Outputhandle=Ou tputhandle;
          ConsolCursor.X= XPos;
          ConsolCursor.Y= YPos;
          SetConsoleCurso rPosition(Outpu thandle,ConsolC ursor);
          WriteConsole(Ou tputhandle,(CON ST VOID*)Text,n_ch ar,&cbwchars,NU LL);
          return cbwchars;
          }

          void graph()
          {
          int x, y;
          for (y=19; y>=-1; y--)
          {
          for (x=-2; x<=25; x++)
          if (y==0 && x>=0 && x%3==0)
          WriteToScreen(" +",1,x,y);
          else if (y==0 && x>=0 && (x%2==0 || x%1==0))
          WriteToScreen("-",1,x,y);
          else if (x==0 && y>=0 && y%2==0)
          WriteToScreen(" |",1,x,y);
          else if (x==0 && y>=0 && y%1==0)
          WriteToScreen(" +",1,x,y);
          else
          WriteToScreen(" ",1,x,y);
          }
          }[/CODE]

          i hope this code help you

          GOOD LUCK...
          Last edited by AHMEDYO; Nov 17 '07, 09:06 AM. Reason: Very bad english :D

          Comment

          Working...