CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    Join Date
    Mar 2012
    Posts
    14

    Beginner needing help with program

    Hi there,

    I'm trying to write a console app in visual studio 2010 that will read a set of numbers and construct a histogram that shows how many numbers are between each two numbers...if that makes sense. To clear it up, i'm looking for an output like:

    0 - 10 ***
    11 - 20 ****
    21 - 30 **

    So in that example, 3 numbers (*) have been found in the read file between 0 and 10. 4 numbers (*) have been found in the read file between 11 and 20 and so on.

    The read file looks like this:

    1 2 24 13 11 9 20 28 16

    So it's just a pretty basic txt file.

    Any help on how to code this?
    Thanks!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Beginner needing help with program

    What have you done so far?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Mar 2012
    Posts
    14

    Re: Beginner needing help with program

    Nothing so far unfortunately :/
    I'm not sure how I'd load the file but am I right in thinking that an if statement would be used for displaying that stars?

  4. #4
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Re: Beginner needing help with program

    Will your file be a continuous file with no returns or double spaces? Can you guarantee that? So just one continuous stream of “number space number space number space number” and so on?

    If you can’t read a simple file into c++ the whole thing is a bit ambitious.

    But what I would do is simply push each number into a vector as in: “vector <int> num;” and then iterate through num using 2 int variables:

    Code:
    void printNumHist(vector <int> num){
     
    unsignedint strt=1, tens=10;
     
     for (unsignedint i=0; i<num.size(); i++){
     
     cout << "\n";
     cout << strt" - "tens" = ";
     
     while(num[i]<=tens){cout << “*”;}
     
     tens=tens+10;
     strt=tens-9;
     }
    }
    
    This is non-tested pseudo code just typed into the box here…. Also, in order to create the vector I would need to have the answers to my first questions regarding the file format.

    The vector also needs to be sorted….
    Rigsby

  5. #5
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Re: Beginner needing help with program

    once I have your file format, I will explain how to read it into the vector and sort the vector.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Beginner needing help with program

    Quote Originally Posted by Rigsby View Post
    Code:
     while(num[i]<=tens){cout << “*”;}
    I wouldn't bet on that this loop ever terminates...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Re: Beginner needing help with program

    Quote Originally Posted by Eri523 View Post
    I wouldn't bet on that this loop ever terminates...
    LOL Oh dear….. You’re right, but he would have learned a very important lesson!

    It was pseudo-code typed without testing!

    Sorry dahrull! I have added the variable maxT as a quick and dirty solution, though I would probably look at another option to break;

    Code:
    void printNumHist(vector <int> num){
     
    unsignedint strt=1, tens=10, maxT=num[num.size()-1];
     
    for (unsignedint i=0; i<num.size(); i++){
     
    cout << "\n";
    cout << strt" - "tens" = ";
     
    while(num[i]<=tens){cout << “*”;}
     
    tens=tens+10;
    strt=tens-9;
     
    if(i>maxT){break;}
     
    }
    }
    Well spotted Eri523! And thanks for letting me correct this myself!

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Beginner needing help with program

    Why are you doing this guy's homework for him?

  9. #9
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Re: Beginner needing help with program

    Quote Originally Posted by GCDEF View Post
    Why are you doing this guy's homework for him?
    Oh GCDEF…. Seriously! Is this homework? Do you know that? This guy said he was a newbie! We all start somewhere! And I didn’t tell him how to read the file, which I suspect he was actually looking for ;-))

    Also, a vector for this problem will work, but is a totally top-heavy solution for what he wants that any good teacher will jump on immediately, thus teaching him a further lesson! But if he is a genuine “newbie” the vector solution will serve him well, and teach him his first step into STL – so a win-win, don’t you think!

    Why are you policing these threads?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Beginner needing help with program

    Quote Originally Posted by Rigsby View Post
    Oh GCDEF…. Seriously! Is this homework? Do you know that? This guy said he was a newbie! We all start somewhere! And I didn’t tell him how to read the file, which I suspect he was actually looking for ;-))

    Also, a vector for this problem will work, but is a totally top-heavy solution for what he wants that any good teacher will jump on immediately, thus teaching him a further lesson! But if he is a genuine “newbie” the vector solution will serve him well, and teach him his first step into STL – so a win-win, don’t you think!

    Why are you policing these threads?
    With 45 posts in 6 years, you're probably not familiar with how this forum works. We don't do homework for people, and that's very obviously homework.

    And no, I don't think doing somebody's work for them is win/win.

    I would advise you to come up with a consistent and meaningful style of indentation and alignment though.

  11. #11
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Re: Beginner needing help with program

    Quote Originally Posted by GCDEF View Post
    With 45 posts in 6 years, you're probably not familiar with how this forum works. We don't do homework for people, and that's very obviously homework.

    And no, I don't think doing somebody's work for them is win/win.

    I would advise you to come up with a consistent and meaningful style of indentation and alignment though.
    Why are you policing these threads?

  12. #12
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Re: Beginner needing help with program

    Quote Originally Posted by GCDEF View Post
    With 45 posts in 6 years, you're probably not familiar with how this forum works. We don't do homework for people, and that's very obviously homework.

    And no, I don't think doing somebody's work for them is win/win.

    I would advise you to come up with a consistent and meaningful style of indentation and alignment though.
    Why are you getting angry? And why are you „giving advice” on things other than code?

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Beginner needing help with program

    Quote Originally Posted by Rigsby View Post
    Why are you policing these threads?
    I'm not going to engage in a pissing match with you. I'm just pointing out how the forum works. If I hadn't, somebody else would have.

  14. #14
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Beginner needing help with program

    Quote Originally Posted by Rigsby View Post
    Why are you policing these threads?
    Don't do other peoples homework for them. That helps no-one.

  15. #15
    Join Date
    Sep 2006
    Location
    Germany
    Posts
    62

    Question Re: Beginner needing help with program

    Quote Originally Posted by GCDEF View Post
    I'm not going to engage in a pissing match with you. I'm just pointing out how the forum works. If I hadn't, somebody else would have.
    I’m sorry, but I seriously do not understand what in God’s name you have for a bee in your bonnet about! If I do anything untoward then admin here will tell me and not you! And, words like p***ing are not very nice!

    I am sorry if I embarrassed you over the int char thing! But that is not any reason to stalk my threads and hit me on “indentation” – I am on a Galaxy SII phone for Pete’s sake! Try indenting code on this thing.

Page 1 of 3 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured