Beginner needing help with program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 41

Thread: Beginner needing help with program

Hybrid View

  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!
    Share on Google+

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

    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
    http://www.microsoft.com/visualstudio/eng/downloads
    Share on Google+

  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?
    Share on Google+

  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
    Share on Google+

  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.
    Share on Google+

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

    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.
    Share on Google+

  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!
    Share on Google+

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Beginner needing help with program

    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
    http://www.microsoft.com/visualstudio/eng/downloads
    Share on Google+

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

    Re: Beginner needing help with program

    Stummschaltung! Dachte ich mir!
    Share on Google+

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

    Re: Beginner needing help with program

    Quote Originally Posted by Rigsby View Post
    Stummschaltung! Dachte ich mir!
    Just don't see the point in talking to you any more. That's not what this forum is about. I'm not really sure why you would choose not to abide by its policies, but thankfully you don't post much. With a little luck, you'll go dormant again.
    Share on Google+

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

    Re: Beginner needing help with program

    Quote Originally Posted by GCDEF View Post
    Just don't see the point in talking to you any more. That's not what this forum is about. I'm not really sure why you would choose not to abide by its policies, but thankfully you don't post much. With a little luck, you'll go dormant again.
    GCDEF you are so hurtful and personal! I guess you were just made that way! I do not hope that you go away…. If you thought the “point” in talking to me was to somehow “win” than you are right… just leave it; however, should you actually be interested in an exchange of intellectual perspectives on this or any other subject, then I always welcome the opportunity of discussion!
    Share on Google+

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,069

    Re: Beginner needing help with program

    Quote Originally Posted by Rigsby View Post
    GCDEF you are so hurtful and personal! I guess you were just made that way! I do not hope that you go away…. If you thought the “point” in talking to me was to somehow “win” than you are right… just leave it; however, should you actually be interested in an exchange of intellectual perspectives on this or any other subject, then I always welcome the opportunity of discussion!
    Then I suggest you go the the discussion forum where it would be on topic. However, I'm not really interested in exchanging "intellectual perspectives" with you here or anywhere else. If you don't like the forum rules, I suggest you take them up with the moderator.
    Share on Google+

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

    Re: Beginner needing help with program

    Quote Originally Posted by GCDEF View Post
    Then I suggest you go the the discussion forum where it would be on topic. However, I'm not really interested in exchanging "intellectual perspectives" with you here or anywhere else. If you don't like the forum rules, I suggest you take them up with the moderator.
    Well, there you go again with your suggestions and advice…. I would like to remind you that no one (without a hyphen) asked you to comment here! I seem to remember that I was in the process of helping with (not doing) some kid’s homework, when Mr. Homework-detective (that’s a compound so we use a hyphen) butted in and got all aggressive… and used offensive words (pi***ng)….

    So if you are not “really interested” in exchanging with me, get the hell out of my discussion with this poster…. You butted in, not I… I will then help here as I see fit…. If you don’t like it, then you will have to converse with me you arrogant, aggressive, angry, not very articulate, person – or stay out of it! You do not make the rules in my discussion….

    And I just said all of that without a single swear word!

    Like I said, go make some friends….
    Share on Google+

  14. #14
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Beginner needing help with program

    Did you read what I posted Rigsby? It's not that no help is offered for homework issues. We're just trying to avoid posting a full solution since that doesn't really help a beginner. If this was a math board you wouldn't post a full solution would you?
    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
    http://www.microsoft.com/visualstudio/eng/downloads
    Share on Google+

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

    Re: Beginner needing help with program

    Of course I read what you posted! Anything else would be ignorant.

    I did not post a solution! I posted a trap that any IT teacher would immediately recognize as “from the web” so thus gave the person either a solution to a problem (if not homework) or reason to think about how to solve it themselves if homework when the teacher challenges them on it.

    People, the natural solution to the problem was NOT a vector; why is that so difficult for you to understand?
    Share on Google+

Page 1 of 2 12 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center