c++ question, how to make vertical lines

Discussion in 'OT Technology' started by MP, Aug 30, 2004.

  1. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    so I have a problem. I have to take the input of the keyboard.

    ie: 1 4 6 2 4 9 7 12 0

    and display it like this...



    Code:
        *
        *
      * *
      * *
      * * *
    * * * *
    
    I can do it horizontally, but I dunno how to make it vertical like that. Any give me a hint in the right direction here?

    thanks

    ps. 0 stops the cin
     
  2. Grifter

    Grifter Silver Member

    Joined:
    Dec 21, 2000
    Messages:
    3,117
    Likes Received:
    0
    Location:
    the southwest
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    I'll assume you have to use std::cout or printf() to display your output. If you're looking for a simple way to get cout or printf to display output vertically rather than horizontally, there isn't one.

    Here's a clue:
    Code:
    cout << "  * " << endl;
    cout << "  * " << endl;
    cout << " ** " << endl;
    cout << " ** " << endl;
    cout << " ***" << endl;
    cout << "****" << endl;
    
    You just need to figure out how many rows you need to display, and for each row, whether to display a '*' or empty space for each column.
     
  4. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    I don't think it's that easy.

    I have to take the # or value input from the keyboard ie: 12 and turn it into a vertical line of *'s

    I think that's the part I'm having trouble with right now. :(
     
  5. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The point I'm trying to get across is that your output is still going to consist of horizontal lines of text. You just have to use some logic to figure out how many lines you need to display, how many characters are in each line, and whether each character is going to be a '*' or a blank space.
     
  6. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley

    ohhh yeah, sorry I miss-understood your post. I realize that each line is still horizontal I just have to figure out how to code that so it either displays a blank space or a *. hmmm...any hints at least? I'm drawing a blank :\
     
  7. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Well, each column represents a data point entered by the user. What does each row represent? Have you figured out how many rows you need to display based on the user data? Once you figure out how many rows you need to display, iterate row-by-row, and for each row, iterate column-by-column and decide based on the current row and column which character to display.
     
  8. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    haha thanks for the help, that just went way over my head :wtc:
     
  9. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    so here is a sample of my code...

    Code:
    #include <iostream>
    using namespace std;
    
    void print(int num)
    {
        for (int i = 0;  i < num; i++)
        cout << "*";
        cout << endl;
    }
    
    int main()
    {
        int value;
        int index = 0;
        int values[100];
        while (cin >> value)
        {
            if (value <= 0)
            break;
            values [index] = value;
            index++;
            if (index == 100)
            break;
        }
        for (int i = 0; i < index; i++)
        print(values[i]);
    }
    
    I can get it to print out horizontally but I'm not sure how to get it to print out vertical.

    I'm thinking I need a point in the right direction.
     
  10. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Code:
    int max = find_max_value(values); // figure out how many rows to draw
    for (int i = max; i > 0; --i)  // <--- rows
    {
        for (int j = 0; j < numvalues; ++j)  // <--- columns
        {
             if (????)  // when do you display a '*' and when do you display a ' '?
             {
                  cout << "*";
             }
             else
             {
                  cout << " ";
             }
        }
        cout << endl;
    }
    
     
  11. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    hmmm, makes a little more sense now

    what would I put in the if (???)

    if ( max <= i ) ?
     
  12. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    max? Max is just the number of rows you need to display. What does i represent, and why am I counting backwards from max to 0?
     
  13. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    i represents the current max value?

    then you are going backwards from there to continually get the next highest value?

    edit: would I replace my void print with finding the max value and printing it out as well?
     
    Last edited: Sep 6, 2004
  14. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    i is just the current row or y-value of the graph. You're drawing the graph top row first. Thus, you need to count backward. So, yes you're close, you do need to compare something to i.
     
  15. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    okay okay, I think I'm really close and I must say thanks a bunch. How should I declare the "find_max_value" though? I obviously can't use my index , values, etc. is the find_max_value just a const int?
     
  16. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    find_max_value is a function. It takes the array of values as an argument, and returns the max value. You could simply write a for loop that finds the max value.
     
  17. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    ugh :wtc:

    isn't this all i have to do?

    Code:
     for (find_max_value = 0; find_max_value < 100; find_max_value++)
        return find_max_value;
        }
        
        int max_value = find_max_value(values);
    
    it keeps saying "find_max_value can't be used as a function :wtc:
     
    Last edited: Sep 6, 2004
  18. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    functions look something like:

    Code:
    int function(int argument)
    {
         int result;
         /* do some work to calculate a result */
         return result;
    }
    
    Then somewhere else in your code, you call the function:

    Code:
    int main()
    {
       ...
       int something_interesting = function(my_argument);
       ...
    }
    
     
  19. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    haha man this is just getting worse and worse. I thought all I had to do was a for loop? :wtc:

    so i need to make a function for find_max_value and
     
  20. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    what exactly ARE you trying to do with that last code segment, anyway?
     
  21. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    what part are you talking about?
     
  22. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    this

     
  23. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
     
  24. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    would this make more sense/

    Code:
    int find_max_value = 0;
        int n;
        
        while (cin >> n)
        {
            if (n > find_max_value)
            {
                find_max_value = n;
            }
        }
        return n;
    
    
     
  25. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    ok, let's forget the function part.

    so far, you have this:

    Code:
        while (cin >> value)
        {
            if (value <= 0)
            break;
            values [index] = value;
            index++;
            if (index == 100)
            break;
        }
    
    
    If all goes well, and the user enters valid input, your value array will hold index number of integers. Now you want to find the maximum value in that array. You can do it in a loop like this:

    Code:
    int maxvalue = 0;  // start with the smallest expected value
    for (int i = 0; i < index; ++i)  // <-- you only need to iterate through the valid entries
    {
         if (values[i] > maxvalue)
              maxvalue = values[i];
    }
    
    So now, you have the maximum value stored in the int maxvalue. What's next?
     

Share This Page