Thread: Vertical bar graph not working

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Vertical bar graph not working

    Title. I'm working on a vertical bar graph program, and it doesn't work.
    What it should print:
    Code:
    Number of values to graph: 3
    1 2 3
    
            #
         #  #
      #  #  #
    ---------
      1  2  3
    What it does print:
    Code:
    Enter number of numbers here: 3
    1
    2
    3
    
    
    ------------
    # 1 # 2 # 3
    Can someone help me fix this? My code is:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int num;
        int array[40];
        cout << "Enter number of numbers here: ";
        cin >> num;
        for(int i = 0; i < num; i++) {
                cin >> array[i];
                }
        int largest = array[0];
       //  for(int i = 0; i < num; i++) {
         //       cin >> array[i];
           //     }
    
    
    
    
    for (int i = 0; i < num; i++) {
    		if (array[i] > largest) {
    			largest = array[i];
    		}
    		
    	}
       // for(int cond = 0; cond <= 5; cond++){
             //   if(array[cond] > largest) {
           //      largest = array[cond];
         //       }
                for (int rows = largest; rows <= largest; rows++) {
                for(int columns = 0; columns <= 5; columns++){
            //    else {
                      cout << "  ";
              //        }
                }
                cout << endl;
                }
                
                for(int columns = 0; columns <= 5; columns++) {
                 cout << "--";
                 }
                cout << endl;
                for(int columns = 0; columns < num; columns++){
                    //    if(array[columns] >= rows) {
                 cout << "# ";
                // }
                 cout << array[columns] << " ";
              //   cout << "The largest (should be): " << largest;
                 }
                cout << endl;
                }
    I hope someone can help!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here, with several answers as well.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by jimblumberg View Post
    Also posted here, with several answers as well.


    Jim
    Only I don't have a reply yet....

  4. #4

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, here is the indented code:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int num;
        int array[40];
        cout << "Enter number of numbers here: ";
        cin >> num;
        for(int i = 0; i < num; i++) {
                cin >> array[i];
                }
        int largest = array[0];
       //  for(int i = 0; i < num; i++) {
         //       cin >> array[i];
           //     }
    
    
    
    
        for (int i = 0; i < num; i++) {
            
            if (array[i] > largest) {
                largest = array[i];
            }
            
        }
       // for(int cond = 0; cond <= 5; cond++){
             //   if(array[cond] > largest) {
           //      largest = array[cond];
         //       }
         for (int rows = largest; rows <= largest; rows++) {
             
         for(int columns = 0; columns < num; columns++){
                 
            if(array[columns] >= rows) {
            cout << "# ";
            }
            
            else {
            cout << "  ";
            }
            }
            cout << endl;
            }
                
            for(int columns = 0; columns <= 5; columns++) {
            cout << "--";
            }
            cout << endl;
            for(int columns = 0; columns < num; columns++){
                        
            cout << array[columns] << " ";
              //   cout << "The largest (should be): " << largest;
            }
            cout << endl;
            }
    Anyways, can someone just please help me? I really need help with this.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No dude, this is indented:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int num;
        int array[40];
        cout << "Enter number of numbers here: ";
        cin >> num;
        for(int i = 0; i < num; i++) {
            cin >> array[i];
        }
        int largest = array[0];
        //  for(int i = 0; i < num; i++) {
        //       cin >> array[i];
        //     }
    
        for (int i = 0; i < num; i++) {
            
            if (array[i] > largest) {
                largest = array[i];
            }
            
        }
        // for(int cond = 0; cond <= 5; cond++){
        //   if(array[cond] > largest) {
        //      largest = array[cond];
        //       }
        for (int rows = largest; rows <= largest; rows++) {
            
            for(int columns = 0; columns < num; columns++){
                
                if(array[columns] >= rows) {
                    cout << "# ";
                }
                
                else {
                    cout << "  ";
                }
            }
            cout << endl;
        }
        
        for(int columns = 0; columns <= 5; columns++) {
            cout << "--";
        }
        cout << endl;
        for(int columns = 0; columns < num; columns++){
            
            cout << array[columns] << " ";
            //   cout << "The largest (should be): " << largest;
        }
        cout << endl;
    }
    Notice how the braces line up and there is an even amount of space for every indent. It's almost comical that your code looks so bad every single time. Editors can indent code for you, did you know that? What do you even use to write your homework?

    Quote Originally Posted by Kelton2
    Only I don't have a reply yet....
    There are answers there now. What's wrong with the answers that you have received so far?

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I still need help, so I bumped the topic, and nobody has replied.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay so no one has given you the solution you want, what have you done to solve the problem in the last two hours? This is your assignment and you're really not showing much effort to try to solve your problems. So I suggest you post something other than "It doesn't work" or "bump" or even "bump2". Have you run your program with your debugger single stepping through your program to try to determine what is wrong?

    Jim

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by jimblumberg View Post
    Okay so no one has given you the solution you want, what have you done to solve the problem in the last two hours? This is your assignment and you're really not showing much effort to try to solve your problems. So I suggest you post something other than "It doesn't work" or "bump" or even "bump2". Have you run your program with your debugger single stepping through your program to try to determine what is wrong?

    Jim
    Post updated, still need help.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where have you "updated' your code? I don't see any "updated" code in this topic. You really need to pick a forum you like and stick with it. Posting the same crap code to multiple forums is in extremely bad taste. And like I said before just crying for help will probably never get you to your goal.

    I suggest you take your handy debugger and get to work.

    Oh and it look like we can add another post to Mattcus' last post.

    Jim

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by jimblumberg View Post
    Where have you "updated' your code? I don't see any "updated" code in this topic. You really need to pick a forum you like and stick with it. Posting the same crap code to multiple forums is in extremely bad taste. And like I said before just crying for help will probably never get you to your goal.

    I suggest you take your handy debugger and get to work.

    Oh and it look like we can add another post to Mattcus' last post.

    Jim
    Okay, so what I've found out is that the # is printing the wrong number of times, and it might be trying to print in the wrong place. I'm not sure how to fix it though-is it something with one of the loop conditions?
    And after that I still need to figure out the specific spacing for everything.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In your simple drawing, the largest bar is essentially the height of the graph. And when you tell cout to write a row, (full of # or ' ') it's going to go left to right and down the screen. Use the largest bar to help track your progress printing the graph.

  13. #13
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    In your simple drawing, the largest bar is essentially the height of the graph. And when you tell cout to write a row, (full of # or ' ') it's going to go left to right and down the screen. Use the largest bar to help track your progress printing the graph.
    I don't get what you're saying.
    Anyways, I made progress:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int num;
        int array[40];
        cout << "Enter number of numbers here: ";
        cin >> num;
        for(int i = 0; i < num; i++) {
                cin >> array[i];
                }
        int largest = array[0];
       //  for(int i = 0; i < num; i++) {
         //       cin >> array[i];
           //     }
    
    
    
    
        for (int i = 0; i < num; i++) {
            
    		if (array[i] > largest) {
    			largest = array[i];
    		}
    		
    	}
       // for(int cond = 0; cond <= 5; cond++){
             //   if(array[cond] > largest) {
           //      largest = array[cond];
         //       }
         for (int rows = largest; rows <= largest; rows++) {
             
         for(int columns = 0; columns < num; columns++){
                 
          //  if(columns] >= rows) {
            cout << "# ";
            //}
            
        //    else {
       //     cout << "  ";
          //  }
            }
            cout << endl;
            }
                
            for(int columns = 0; columns <= 5; columns++) {
            cout << "--";
            }
            cout << endl;
            for(int columns = 0; columns < num; columns++){
                        
            cout << array[columns] << " ";
              //   cout << "The largest (should be): " << largest;
            }
            cout << endl;
            }
    It now prints one # for each number, so the only problem left is figuring out how to get it to print (number) #s!

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Kelton2 View Post
    I don't get what you're saying.
    The height of the graph (how tall the graph is on the screen) is the same as the size of the largest bar. You won't need to print more #'s than that. You can use this fact to control the loop that counts the rows of the graph.

    It's better than what you currently have:
    Code:
         for (int rows = largest; rows <= largest; rows++)
    You will need to figure out what to compare here - not just comment it out:
    Code:
        //  if(columns] >= rows) {
        cout << "# ";
        //}
        
        //    else {
        //     cout << "  ";
        //  }
    Say rows is 6 here. This loop will only run once because 6 <= 6 is true only at the start.

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    The height of the graph (how tall the graph is on the screen) is the same as the size of the largest bar. You won't need to print more #'s than that. You can use this fact to control the loop that counts the rows of the graph.

    It's better than what you currently have:
    Code:
         for (int rows = largest; rows <= largest; rows++)
    You will need to figure out what to compare here - not just comment it out:
    Code:
        //  if(columns] >= rows) {
        cout << "# ";
        //}
        
        //    else {
        //     cout << "  ";
        //  }
    Say rows is 6 here. This loop will only run once because 6 <= 6 is true only at the start.
    Except I know that that if condition is wrong, but I don't know what it should be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroll Bar
    By csonx_p in forum Windows Programming
    Replies: 2
    Last Post: 07-01-2008, 12:33 AM
  2. vertical bar graph
    By n00by in forum C Programming
    Replies: 2
    Last Post: 09-01-2004, 04:39 PM
  3. Vertical Scrollbar with mfc
    By Eversman in forum Windows Programming
    Replies: 3
    Last Post: 05-28-2004, 04:02 PM
  4. Vertical scroll????
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-14-2002, 12:14 PM
  5. I need help with my vertical bar graph program.
    By smokedragon in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 05:01 PM