Like Tree10Likes
  • 1 Post By manasij7479
  • 2 Post By WaltP
  • 2 Post By manasij7479
  • 2 Post By WaltP
  • 3 Post By WaltP

How would I display the results in a bar graph way?

This is a discussion on How would I display the results in a bar graph way? within the C++ Programming forums, part of the General Programming Boards category; How would I display the results in a bar graph way? Currently it does, depending on the count: Code: * ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Smile How would I display the results in a bar graph way?

    How would I display the results in a bar graph way?

    Currently it does, depending on the count:

    Code:
    *
    *
    *
    *
            *
            *
            *
                      *
                      *
                      *
    Code:
        for(int i = 0; i < anspos1_count; i++)
        {
            cout << "*" << endl;
    
    
        }
    
    
        for(int j = 0; j < anspos2_count; j++)
        {
            cout << "\t *" << endl;
        }
    
    
        for(int k = 0; k < anspos3_count; k++)
        {
            cout << "\t \t *";
        }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    You need to 'fuse' these loops into a single nested loop.
    Here is a general solution you can play with and then adapt to your problem.
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    int main()
    {
        std::vector<int> data={1,4,3,4,5,6,0,3};
        int max = * std::max_element(data.begin(),data.end());
        for(int i=max-1;i>=0;--i)
        {
            for(auto x:data)
                std::cout<<(x>i?"*":" ")<<"\t";
            std::cout<<std::endl;
        }
        return 0;
    }
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by manasij7479 View Post
    You need to 'fuse' these loops into a single nested loop.
    Here is a general solution you can play with and then adapt to your problem.
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    int main()
    {
        std::vector<int> data={1,4,3,4,5,6,0,3};
        int max = * std::max_element(data.begin(),data.end());
        for(int i=max-1;i>=0;--i)
        {
            for(auto x:data)
                std::cout<<(x>i?"*":" ")<<"\t";
            std::cout<<std::endl;
        }
        return 0;
    }

    Hey, the following code you gave me is not executing, is giving errors.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by Swadesh View Post
    Hey, the following code you gave me is not executing, is giving errors.
    Compiler ? ..and error messages ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by manasij7479 View Post
    Compiler ? ..and error messages ?
    Code:
    Error	1	error C2552: 'data' : non-aggregates cannot be initialized with initializer list	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	6
    Error	2	error C2143: syntax error : missing ',' before ':'	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10
    
    
    Error	3	error C3531: 'x': a symbol whose type contains 'auto' must have an initializer	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10
    
    
    	4	IntelliSense: initialization with '{...}' is not allowed for object of type "std::vector<int, std::allocator<int>>"	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	6
    
    
    	5	IntelliSense: cannot deduce 'auto' type (initializer required)	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10
    
    
    	6	IntelliSense: expected a ';'	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10
    
    
    	7	IntelliSense: expected an expression	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Oops... either you need to tell your compiler to use C++11 features.. or they are not supported yet.

    I'll let you figure out how to convert them to old C++.
    (Terms to google for: Brace Enclosed Initializer List, Ranged For Loops)
    Swadesh likes this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by manasij7479 View Post
    Oops... either you need to tell your compiler to use C++11 features.. or they are not supported yet.

    I'll let you figure out how to convert them to old C++.
    (Terms to google for: Brace Enclosed Initializer List, Ranged For Loops)
    I am using Visual Studio 2010, can you tell me how I can use the C++11 features?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Maybe this will pass through your compiler
    Code:
    #include <iostream>
    
    int findMax(int*,const int);
    
    int main()
    {
        int data[]={1,4,3,4,5,6,0,3};
        int max = findMax(data,8);
        for(int i=max-1;i>=0;--i)
        {
            for(int j = 0 ; j < 8 ; j++)
                std::cout<<(data[j]>i?"*":" ")<<"\t";
            std::cout<<std::endl;
        }
        return 0;
    }
    
    int findMax(int* data,const int n)
    {
    	int max=data[0];
    	for(int i=1 ; i<n ; i++)
    		if(max<data[i])
    			max=data[i];
    	return max;
    }

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Swadesh View Post
    I am using Visual Studio 2010
    VS 2012 is available in MSDNAA if you are a student

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    So what you are trying to do is get manasij7479 to do your coding for you, eh? Wouldn't it be better to look at and study his code for understanding, then you can write the code that matches exactly into your program rather than having him keep changing his code until it fits your program?

    Don't be so lazy. Learn to do from the examples.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by WaltP View Post
    So what you are trying to do is get manasij7479 to do your coding for you, eh? Wouldn't it be better to look at and study his code for understanding, then you can write the code that matches exactly into your program rather than having him keep changing his code until it fits your program?

    Don't be so lazy. Learn to do from the examples.

    Thats why I always post a C++11-ish version (which teachers probably won't accept for a few more years !).

    Case 1: OP decides to learn C++11. Profit
    Case 2: OP tries to understand the code and rewrites it himself.Profit.
    Case 3: Someone comes along and hands out a vanilla solution. Loss.
    Salem and iMalc like this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by manasij7479 View Post

    Thats why I always post a C++11-ish version (which teachers probably won't accept for a few more years !).
    Just a thought...
    Rather than the confusing syntax of pure C++11 (vectors, std:: et al) that must surely confuse someone that barely can do a FOR statement, how about psuedocode? They can see the steps but can't use the post as is. They have to think and translate into actual code.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by WaltP View Post
    Just a thought...
    Rather than the confusing syntax of pure C++11 (vectors, std:: et al) that must surely confuse someone that barely can do a FOR statement, how about psuedocode? They can see the steps but can't use the post as is. They have to think and translate into actual code.
    I always find pseudo-code (like the ones given in the Cormen book, which I'm reading now) very confusing in terms of translating ideas(maybe that is because I'm much more familiar with C++ than with, say python or pascal).

    (Yesterday, I figured out Quick Sort's Partition function (and a variant of it provided for 'fun') only after seeing some C code on the web after an hour of looking at the pseudo code.)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Good pseudo code starts with a laundry list of tasks to do and an explanation of data. Then you just have to drill down until it looks like an implementation. I don't think programming experience has much to do with it. ALL of my formal education forced me to hand in program specifications before I wrote a program, which meant lots of pseudo code writing. I guess my education was unique that way.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by whiteflags View Post
    Good pseudo code starts with a laundry list of tasks to do and an explanation of data. Then you just have to drill down until it looks like an implementation.
    But I'm talking about pseudo code to give an idea, not design from scratch toward implementation.


    Quote Originally Posted by manasij7479 View Post
    I always find pseudo-code (like the ones given in the Cormen book, which I'm reading now) very confusing in terms of translating ideas(maybe that is because I'm much more familiar with C++ than with, say python or pascal).
    So don't use that style of pseudo code.

    std100093's code:
    Code:
    int findMax(int* data,const int n)
    {
        int max=data[0];
        for(int i=1 ; i<n ; i++)
            if(max<data[i])
                max=data[i];
        return max;
    }
    in pseudo code:
    Code:
    function findMax(data [as array], n [as amount of data])
        set max to first element of data
        loop using I through the elements of data
            test if max less than data[I]
                if so replace max with data[I]
        return max
    No code that can be directly copied
    The OP must still think about the small details of the code (what loop to use; loop parameters)
    They still learn with guidance, not get confused with gobbeldy-gook like
    Code:
        std::vector<int> data={1,4,3,4,5,6,0,3};  // they barely know what an array is, how is this syntax going to help them?
        for(auto x:data)  // what is an auto? All I've learned is for(i=1; i<n; i++)
        std::cout<<(x>i?"*":" ")<<"\t";  // what's this std thing? What's the ?"*":"" mean? Doesn't cout work?
        // I've never seen anything like this in my like! I'm suppose to learn something from this?
    Target your audience. Don't show them neurosurgery when all they know is a band-aid.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 05-02-2012, 05:17 PM
  2. Replies: 1
    Last Post: 02-28-2012, 06:19 PM
  3. Graph - Help!! :(
    By Zeldic in forum C++ Programming
    Replies: 6
    Last Post: 06-02-2010, 09:15 AM
  4. need graph.h
    By h_howee in forum Windows Programming
    Replies: 3
    Last Post: 12-17-2005, 05:36 PM
  5. Replies: 0
    Last Post: 03-15-2002, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21