Like Tree2Likes
  • 2 Post By iMalc

The heap

This is a discussion on The heap within the C++ Programming forums, part of the General Programming Boards category; I'm new to programming, and would like to involve the heap into this program. Any help would be appreciated Code: ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    The heap

    I'm new to programming, and would like to involve the heap into this program. Any help would be appreciated

    Code:
    #include
    <iostream>
    
    #include
    <cstdlib>
    
    #include
    <ctime>
    
    
    using
    namespace std;
    
    
    class
     RandomNumberGenerator
    
    {
    
        
    int secretNumber;
    
        
    int tries;
    
        
    int guess;
    
    
    public
    :
    
        RandomNumberGenerator()
    
        {
    
            tries=0;
    
            secretNumber=randomNumber(100); 
    
        }
    
    
        
    int randomNumber(int max)
    
        {
    
            
    return (rand() % max + 1); // random number between 1 and 100
    
        }
    
    
        
    void guessprocedure()
    
        {
    
            cout << 
    "\tWelcome to Guess My Number\n\n";
    
            
    do
    
            {
    
                cout << 
    "Enter a guess: ";
    
                cin >> guess;
    
                ++tries;
    
                
    if (guess > secretNumber)
    
                {
    
                    cout << 
    "Too high!\n\n";
    
                }
    
                
    elseif (guess < secretNumber)
    
                {
    
                    cout << 
    "Too low!\n\n";
    
                }
    
                
    else
    
                {
    
                    cout << 
    "\nThat's it! You got it in " << tries << "guesses!\n";
    
                }
    
            } 
    while (guess != secretNumber);
    
        }
    
    };
    
    
    int
     main()
    
    {
    
        RandomNumberGenerator rng;        
    
        rng.guessprocedure();
    
        
    return 0;
    
    }
    

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by chris222 View Post
    I'm new to programming, and would like to involve the heap into this program.
    Do you mean a data structure or dynamic memory allocation? What would be its purpose?
    I never put signature, but I decided to make an exception.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    First, allow me to demonstrate how to "correctly" format code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    class RandomNumberGenerator
    {
        int secretNumber;
        int tries;
        int guess;
    
    public:
    
        RandomNumberGenerator()
        {
            tries = 0;
            secretNumber = randomNumber(100);
        }
    
        int randomNumber(int max)
        {
            return (rand() % max + 1); // random number between 1 and 100
        }
    
        void guessprocedure()
        {
            cout << "\tWelcome to Guess My Number\n\n";
    
            do
            {
                cout << "Enter a guess: ";
                cin >> guess;
                ++tries;
    
                if (guess > secretNumber)
                {
                    cout << "Too high!\n\n";
                }
                else if (guess < secretNumber)
                {
                    cout << "Too low!\n\n";
                }
                else
                {
                    cout << "\nThat's it! You got it in " << tries << "guesses!\n";
                }
            }
            while (guess != secretNumber);
        }
    };
    
    
    int main()
    {
        RandomNumberGenerator rng;
        rng.guessprocedure();
    
        return 0;
    }
    Next, I would state that it is not appropriate to use dynamic memory allocation in this program. I do not advocate learning to use something inappropriately, so I suggest either forgetting that idea, or coming up with a new requirement which will give you a valid reason to use dynamic memory allocation. If you need suggestions, just ask.
    Elysia and rags_to_riches like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Formatting issues also pointed out here.

    I agree with iMalc; there is no need to support dynamic memory allocation in this program, unless there are some other additional features that might need to be added which might use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Min-Heap help
    By Uni616 in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2010, 02:00 AM
  2. Heap?
    By audinue in forum Tech Board
    Replies: 8
    Last Post: 01-12-2009, 10:18 PM
  3. Anyone Know HEAP ?
    By Kam in forum C Programming
    Replies: 5
    Last Post: 09-14-2002, 07:47 AM
  4. heap
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 08-21-2002, 12:18 PM
  5. heap
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2001, 01:15 PM

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