Beginner Exercises

Pages: 123456... 16
British? I'm in Washington! =P

Anyway, when in doubt, check urbandictionary.com
Thanks for this guys, I am very new to programming and these have helped me a lot. I have only done two of them but will work on the rest of them tonight.

P.S. The DP this is awesome, I'm sure that guy's mom would not like it very much. lol
Here we go again...
Anyway, when in doubt, check urbandictionary.com


No thank you I just ate ....
Suffice it to say that if your parents ever mention a "third wheel," you should cover your ears.
Last edited on
i have a question on the bunnies exercise. what if there is 2 male adult bunnies and 3 female adult bunnies. does that mean that 6 new bunnies is created? or there should only be 3 bunnies created?

anyone here who have finish this exercise?
This is from the exercise text:
So long as there is at least one male age 2 or older, for each female bunny in the list age 2 or older;
a new bunny is created each turn
@Bazzy
so in my example of 2 male and 3 female (assuming there atleast age of 2) there will be 2 bunnies to be created?
Each female has one child
ok, thanks. sorry i dont understand english that good.
Suffice it to say that if your parents ever mention a "third wheel," you should cover your ears.


*cough**cough*ackassjay*cough**cough*
Har-har
Last edited on
Sorry I'm late.

chrisname wrote:
I love me some DP.
DrChill wrote:
My mom loves DP
HAHAHAHAHAHAHAHAHAHAHAHA!
Last edited on
Sorry I'm late.

You're almost a month late.

HAHAHAHAHAHAHAHAHAHAHAHA!

Many hands make light work...
Birds of a feather fly together.
Thanks, very useful for me.

Grading Program:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <IOStream>

int main()
{
	int score;
	do {

	std::cout<<"Please input your score:";
	std::cin>>score;
	switch (score/10)
	{
		case 10:
			std::cout<<"You get the A , Congratualations!\n"<<std::endl;
			break;
		case 9:
			std::cout<<"You get the A \n"<<std::endl;
			break;
		case 8:
			std::cout<<"You get the B \n"<<std::endl;
			break;
		case 7:
			std::cout<<"You get the C \n"<<std::endl;
			break;
		case 6:
			std::cout<<"You get the D \n"<<std::endl;
			break;
		case 5:
			std::cout<<"You get the E \n"<<std::endl;
			break;	
		case 4:
			std::cout<<"You get the F \n"<<std::endl;
			break;
		case 3:
			std::cout<<"You get the G \n"<<std::endl;
			break;
		case 2:
			std::cout<<"You get the H \n"<<std::endl;
			break;
		case 1:
			std::cout<<"You get the I \n"<<std::endl;
			break;
		default:
			std::cout<<"You fail! \n"<<std::endl;
			break;
	};

	}
	while(1);
	return 0;
}


closed account (S6k9GNh0)
A couple of problems:

#include <IOStream>
This may not be problematic but the correct spelling of the header file is iostream all lowercase.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	do {

	std::cout<<"Please input your score:";
	std::cin>>score;
	switch (score/10)
	{
		case 10:
			std::cout<<"You get the A , Congratualations!\n"<<std::endl;
			break;
		case 9:
			std::cout<<"You get the A \n"<<std::endl;
			break;
		case 8:
			std::cout<<"You get the B \n"<<std::endl;
			break;
		case 7:
			std::cout<<"You get the C \n"<<std::endl;
			break;
		case 6:
			std::cout<<"You get the D \n"<<std::endl;
			break;
		case 5:
			std::cout<<"You get the E \n"<<std::endl;
			break;	
		case 4:
			std::cout<<"You get the F \n"<<std::endl;
			break;
		case 3:
			std::cout<<"You get the G \n"<<std::endl;
			break;
		case 2:
			std::cout<<"You get the H \n"<<std::endl;
			break;
		case 1:
			std::cout<<"You get the I \n"<<std::endl;
			break;
		default:
			std::cout<<"You fail! \n"<<std::endl;
			break;
	};

	}
	while(1);


Have you run your code? This code will never stop. So that means when your user gives you input, it will give output and immediately ask for input again. In any program, you want it to stop at some point in time.

Also, there is no need for the do-while loop here. To reduce your code and confusion, you would just use a while loop. The use of a do-while loop is when you need the code to be executed at least once before the boolean placed in the while function is checked. As (1) always return true, there should be no problem with this. ALSO, if you were to do that, instead of using while(1), I would use for(;;) which is a quick optimization boost as while(1) actually uses instruction to check and see if 1 is true where as the latter does not.

I also posted a version of the example earlier in this same topic. It DOES use a loop and you can look at it as an example on how to do it more correctly.
Last edited on
I would use for(;;) which is a quick optimization boost as while(1) actually uses instruction to check and see if 1 is true where as the latter does not.


Umm... what?

while(1) is fine
Because the compiler couldn't possible figure out that while (1) can be compiled to a simple unconditional jump. I mean, while (1) is so rarely used, that the developers couldn't possibly think of that one.
@computerquip : Thanks, I ran my code and I stop it with Ctrl+C. and I added "do ... while" for convenience of debugging. I read your code and it help me a lot. I remain my code here and let guys discuss the wrong example.
Pages: 123456... 16