Beginner Exercises

Pages: 1... 910111213... 16
closed account (S6k9GNh0)
Perhaps I should change to Gender
Lol :P

I don't see the point in having all those accessor functions. Waste of CPU time IMO.

I just made those variables public... :)
Last edited on
closed account (S6k9GNh0)
I was wanting output to be flexible enough to where whoever is implementing main could just add as many output streams as he wanted (game still requiring an initial out). This could be accomplished through a vector and iteration I guess but it's kinda dull. I guess I'll go with that for now..

And the reason to make them private is:
1) To disallow editing of the variable after being initialized. This ended up being kinda dumb anyways since Game is a friend and has access to Bunny's privates. I'm thinking about playing with permissions to fix this but I'm going to leave it how it is for right now... I'm almost to the printing stage. Fixing up my output method.

2) Abstraction ftw.

@Names list: ONLY 16KB? Dude, my list is nearly half a megabyte >.> I just googled for a database of names. Although it's not as funny now they aren't bunny names.

EDIT1: http://codepad.org/8f6s0QsH
{
This post gives a floating point error although it compiles. GDB shows that it's inside of the Bunny() constructor where I assign name. I haven't a clue what causes it, but I guess I'll keep going...

FIXED: Seems to be a problem with the poorly coded operator. Using a macroed function built on top of another function instead. Not as convenient or clean but for now it will do.
}

EDIT2:
{
Seems my biggest problem now is rand(). No matter what I do, the random generator will only output female or male. Not sure why.

FIXED: Change the way I set highest and lowest range to a specific equation that it is guarenteed to work. rand() % 2 is somehow not the same as rand() % (1 - 0 + 1) + 0. If anyone would like to explain this, please do, it baffles me.

FIXED AGAIN: rand() % 2 is wrong. rand() % (1-0+1)+0 reads like so: (rand()%2)+0... wait, wtf, that still doesn't work.
}

EDIT3:
{
I have yet another problem. I randomly choose first and last names from two lists. For some reason, it faults each time on the last name which seems to be random. I've only once before opened a debugger. Thank god I know how to use DDD, the sexy beast of a frontend.

FIXED: The text file I downloaded was formatted using \r instead of \n. WTF is that.....
}

EDIT4: http://codepad.org/X10FppV6
{
Names aren't random enough, trillions of combinations and I get names that are the same which I believe is the fault of the time seed. I'm currently not going to worry to much about it and will be fixed when I edit the program to execute every 2 seconds.

I also sometimes get a segmentation fault which is going to happen when I have 1000+ bunnies all over the place. I've yet to implement my BunnyGenocide() function.
}


Any criticism so far is helpful. Things that I already know:

* OutputManager isn't useless, it's just lacking advanced feature that I've yet to implement.
* I know the RAVB are not even taking other bunnies over.
* I know that I need to print all the info.
* I know that I need to set a sorting function to print. This will indeed be done.

After this, I'm going back to my main project. I'm having too much fun with this.
Last edited on
Hm.. I have a gameplay question: Which would version would be more correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Game::next_turn(void)
{
	// increase the age of every bunny by 1
	increase_age();

	// if a bunny is older than 10, it dies.
	// radioactive vampire bunnies die if > 50
	kill_old();

	// create new children if possible
	new_children();

	// now for each vampire create a new vampire
	turn_vampire();
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Game::next_turn(void)
{
	// increase the age of every bunny by 1
	increase_age();

	// if a bunny is older than 10, it dies.
	// radioactive vampire bunnies die if > 50
	kill_old();

	// now for each vampire create a new vampire
	turn_vampire();

	// create new children if possible
	new_children();
}


btw. i am really happy if my bunny population would exceed 1000. In most cases the radioactive bunnies takes them over before it happens.
I would do kill_old before increase_age. Bunnies will always die on their 10th birthday if you kill them immediately after they age. That doesn't seem fair.
since i kill only bunnies older than 10, i will kill them on their 11th birthday, which seems pretty fair to me.
Oh, ok. I was wondering if you compared age with > 10 or >= to 10. In the former case, you're ok.
closed account (S6k9GNh0)
I just realized how realistic but complex my method of completing the task is. I wonder if that's a good thing...
Hi there, im currently on struggling to do the pancake glutton exercise. I am using this http://www.cplusplus.com/doc/tutorial/ to teach me c++ and so far so good, its really good for folk like myself who have never programmed before.

I stumbled across the beginner exercises in this thread and thought it would be a good way for me to put to the test, that which i have learned here.

Where i am struggling though is the part where i am supposed to list the pancake eaters in order, i have read up to (and a little beyond) arrays at the moment but cant really come up with a solution for listing array data in any kind of order.

Ive had a good google but cant find anything that is well explained (which i need or i wont understand, cant just throw a heap of code in front of me and say, this works) or anything that even compiles so that i might try and break it down. Can anyone point me in the right direction.

I dont want the full solution obviously but help on sorting arrays would be nice, maybe there is a site or a post on here i have overlooked which explains this in laymans terms.

Thanks in advance!
Somarl :
If you want to figure out how to sort an array, then think about how would you sort a deck of cards in your hand. Maybe you would always search for the smallest card, put it on the top of the deck, and repeating the same process (excluding the top (already sorted) cards) until the whole deck is sorted.

Or if you don't want to reinvent the wheel
http://en.wikipedia.org/wiki/Sorting_algorithm

Or, you can just use
http://www.cplusplus.com/reference/algorithm/sort/

Last edited on
Thanks R0mai. I was hoping i would come across something like this but in the books i have read i havent as of yet. Ill read up as much as i can on sorting before tackling the problem again.

Thanks!
@ anyone who's tried to help me

Sorry for taking forever to respond(I don't have internet). Thanks for all of your help, still havn't really figured out how to do it yet, mainly because I don't have internet. I'm just going to hold off on learning c++ till I do get internet, or I find a book somewhere(one for semi-beginners).

closed account (jwC5fSEw)
I'm having trouble with the guessing game where the computer has to get it in seven guesses or less. I know how to get it in seven or less guesses; I just can't implement it. Let's say I have the number 89. The program always starts with 50, so I say too low. It then does 50 + (100 - 50)/2, resulting in 75. Still too low, so 75 + (100 - 75)/2, resulting in 87. Still too low. I apply the same formula and get 93. This is too high, so I do the reverse: 93 - (100 - 93)/2, resulting in 90. Too high still. I do it again. I get 85, which is obviously not a possible answer because I already said 87 was too low.

Now, I understand what I'm doing wrong; the program doesn't restrict its guesses to the already ruled-out numbers. I just can't figure out how to implement that. All of my attempts fail. Any suggestions?


GOT IT WORKING! During a ten hour car ride earlier, I was thinking about it and it struck me. Now it works perfectly for any number between 1 and 100!
Last edited on
For that While Gullibility one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    int a = 0;
    string b;
    for(int i =1;i <= 10;i+=1)
    {
         cout<<" Enter any number besides "<<i<<endl;
         cin>>a;
         if (a == i)
            exit(1);
         if (a ==5)
         {
               cout<<"You're not supposed to enter five.";
               exit(1);
         }
         cout<<"Your number was:"<<a<<"."<<endl;                          
    }
    cout<<"Wow patience, young padawan.";
    getline (cin,b);
    cin.get();
    return 0;
}


I'm a beginner, Anything I can fix?
No one? :(
closed account (S6k9GNh0)
1) Text formatting is big deal. It DOES matter if you add that extra period because other wise you look like a dummy who can't spend a minute to make his program look nice. I suggest you remove the space before Enter on line 10. I also suggest you remove the period from line 19 and add a space after "was:" on line 19.

2) Your header of <stdlib.h> is deprecated and even wrong. The correct header to include in C++ is <cstdlib> which is the exact same thing mind you.

3) using namespace std; is fine if you use a large amount of the std. But for clarification and quick optimization, either use the std:: namespace for each std:: member or specify only the classes and members that you use.

Besides that, the functionality of it is fine.
Last edited on
closed account (jwC5fSEw)
3) using namespace std; is fine if you use a large amount of the std. But for clarification and quick optimization, either use the std:: namespace for each std:: member or specify only the classes and members that you use.


I don't really think it's right to recommend not using using namespace std this in beginner exercises; even Stroustrup uses it in all of his code examples in TC++PL.
Last edited on
closed account (S6k9GNh0)
Might as well start now than ever. I don't really care what his examples did.
closed account (jwC5fSEw)
Fair enough. As a beginner (only a few weeks ago :P), I found constant std:: prefixes a bit confusing, and considering exercises like these exclusively demonstrate features in the std namespace, it makes it easier for a beginner. But that's more of an opinion, so you're right.

Seeing as you got rid of your original post, I will too.

That's not really a good attitude, I'd think, considering the point of his examples were to demonstrate good C++ style.
Last edited on
I believe the reason he did that is because it's easier to say "Just put this line here" than "Whenever you type cout cin etc put an std:: in front of it because it is part of the standard namespace and...*discussion about namespaces*", not because it's better coding style. He does state somewhere that scoping the using declarations is better than putting outside of everywhere.
Pages: 1... 910111213... 16