Thread: printing axis for histogram

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    printing axis for histogram

    Hey guys im trying to print this
    Code:
              0     5    10    15   20   25   30   35  
             +----+----+----+----+----+----+----+-
             |
             |
             |
             |
       5     |
             |
             |
    
    10    
    
    15  
    
     20  
    
     25   
    
    and so on with 5 hiphens | in between each number i cant get the formatting correctly?
    
    30   
    
    35
    could someone take a look

    thankyou
    Code:
    /* print the numbers*/   
      for(pos=0; pos<8; pos++)
      {
        
        n = pos * 5;
        printf("  %-3d", n);
       
        
        
        
      }
      
      
      /* after a new line space print
      each +----+ once at a time in the loop*/
      /*
      for(b=0; b<VER_ARRAY; b++)
      {
        
          
            
        
      }*/
       printf("\n");
       printf("  +----+----+----+----+----+----+----+-");
      printf("\n");
      /*for the amount entered keep printing numbers
       on vertical axis limited to user input*/
      
      
          /*keep printing hiphens for each interval*/
          for(a =0; a<5; a++)
          {
            printf("  |");
           
            printf("\n");    
          
          }
      
           for(d=1; d<4; d++)
          {
              n = 0;
              n = d * 5;
              
              printf("%-5d\n", n);
              
                 /*
              for(e =0; e<5; e++)
              {
                  printf("  |");
                  
                  
                  printf("\n");
              }*/
              
          }
          
     
          /* for the size of num being entered if 
          0 integers have been entered make it valid*/
          
          for(c=0; c<sizeof(num[c]); c++);
          {
             if(num[c] %5 ==0)
             {
                 printf("\n");
             }
     
              
          }
          printf("\n");
    Last edited by Salem; 04-06-2007 at 11:10 AM. Reason: tagged the ascii art

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A hyphen is a dash. A pipe is the vertical bar. Try editing your post and wrapping code tags around your drawing to say what you're really trying to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need TWO, and only two loops for a histogram. Here's some logic
    Code:
    for (i = 0; i < number of rows of data to be in the histogram; i++)  {
       
       for (j = low range; j <= high range; j++) {
          if ((j > 0) && (j % 5 == 0)) {
              print a pipe
              continue; 
          }  
    
           if (array[i] > j)  
              print an asterisk
           else
              print a blank space
    
       }         
    }
    J is our value in the histogram, as it's drawn, from left to right, low range to high range.

    Note that if a value would print a * right on the fifth column's pipe, that value will not be printed in the above logic. The pipe will take precedence over the asterisk.

    Adak

  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
    Rather than trying to figure out the exact printing order, how about

    char screen[25][80];
    Initialise the whole array to spaces.

    Then you can write to any "position" on the screen by writing directly to the corresponding position in the array.

    Once you've finished, then its a simple matter of working your way through the array printing each character.
    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
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Sorry i meant pipe | will that work for every 5 pipes theres a number from 5 10 15 20? however the numbers have to be before the pipes

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can do like Salem suggested, or you fill it in as you go.
    Code:
    for each row
        for each column
            find what goes here and print it
    In short, you do a tiny bit of math, find out what goes in this spot, and print it as you move across the columns. Then you go down a row and do the same.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bazzano View Post
    Sorry i meant pipe | will that work for every 5 pipes theres a number from 5 10 15 20? however the numbers have to be before the pipes
    No it won't work for the header portion. That was intended for the body of the histogram. It should print a pipe in the body of the histogram, when they're needed. Otherwise it prints either a blank space, or an asterisk. When you see how it works, you should have no trouble getting the header to print right.

    It would be best if you moved away from some of those mad loops you have in your code. They are enough to make Rube Goldberg, jealous.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. AXIS and C++
    By bman1176 in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2006, 11:00 AM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM