Beginner Exercises

Pages: 123456... 16
Dec 17, 2009 at 5:11am
British? I'm in Washington! =P

Anyway, when in doubt, check urbandictionary.com
Dec 18, 2009 at 4:03am
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
Dec 18, 2009 at 12:35pm
Here we go again...
Dec 18, 2009 at 5:13pm
Anyway, when in doubt, check urbandictionary.com


No thank you I just ate ....
Dec 28, 2009 at 9:48pm
Suffice it to say that if your parents ever mention a "third wheel," you should cover your ears.
Last edited on Dec 29, 2009 at 10:41pm
Jan 3, 2010 at 9:49am
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?
Jan 3, 2010 at 12:44pm
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
Jan 3, 2010 at 1:58pm
@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?
Jan 3, 2010 at 2:06pm
Each female has one child
Jan 3, 2010 at 4:23pm
ok, thanks. sorry i dont understand english that good.
Jan 4, 2010 at 11:56pm
Suffice it to say that if your parents ever mention a "third wheel," you should cover your ears.


*cough**cough*ackassjay*cough**cough*
Jan 5, 2010 at 8:23am
Har-har
Last edited on Jan 5, 2010 at 8:25am
Jan 6, 2010 at 1:44am
Sorry I'm late.

chrisname wrote:
I love me some DP.
DrChill wrote:
My mom loves DP
HAHAHAHAHAHAHAHAHAHAHAHA!
Last edited on Jan 6, 2010 at 1:44am
Jan 6, 2010 at 3:20pm
Sorry I'm late.

You're almost a month late.

HAHAHAHAHAHAHAHAHAHAHAHA!

Many hands make light work...
Jan 9, 2010 at 8:33pm
Birds of a feather fly together.
Jan 11, 2010 at 8:59am
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;
}


Jan 11, 2010 at 6:10pm
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 Jan 11, 2010 at 6:12pm
Jan 11, 2010 at 7:02pm
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
Jan 11, 2010 at 8:33pm
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.
Jan 12, 2010 at 2:35am
@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