CSC 15 Lab 3 Due Wednesday 2/21/01 This lab lets you practice with if-else (and while) statements. First a quick review. Remember that both if-else (and while) statments can consists of blocks of other statements enclosed in {}'s: if (x < 10) { cout << "small"; x = x+1; } else { cout << "big"; x = x-1; } The above lines should be thought of as *a single* compound statement. Depending on the value of x, executing this statement will result in the execution of one or the other sequence of statments enclosed in {}'s These sequences of statements enclosed in {}'s are like miniture programs on their own. They can also contain other compound if-else statements, resulting in a nesting of statements. Similarly, a while loop executes a sequence of statments repeatedly until the boolean condition becomes false. int x = 0; while (x < 10) { cout << x; x = x+1; } The above program segment will print 0123456789 on the screen. When writing compound statements such as if-else and while loops, it is very important to line up the brackets and use proper indentation: code that look like the following are *UNACCEPTABLE*: if (x<5) { if(y int main() { // your code goes here return 0; } For the first program, write a program to prompt the user to enter an integer. You should recall how to do that from the last lab: first declare an integer, then use cout and cin. Then, write a if-else statement to determine if the number entered is odd or even. How do you do that? Simply take the remainder of the number after dividing it by two. If it's 1 then the number is odd, and if it's 0 then the number is even. For example, 8%2==0 because 8 is even, 9%2==1 because 9 is odd. zero can be considered an even number. You should then print out one of the following messages: "The number you entered is even", or "The number you entered is odd" ------ For the main part of the lab you are to write a program that asks the user a series of questions in order to guess what kind of animal the user is talking about. The user can answer the questions with either 'y' or 'n'. Here's a sample output of what the program might look like: Computer: Is it a mammal? y Computer: Does it walk on four legs? y Computer: Does it prey on other animals? y Computer: According to my database, your animal is likely a tiger. ---- Of course, more questions would be needed to really pin the answer down to a tiger. Your program needs to ask enough questions to distinguish between the following animals: tiger, horse, rabbit, shark, bass, turtle, snake, chicken, hawk Note the above line of questions was enough to exclude all other possibilites except tiger. You should draw a species tree to determine what and how many questions you need to ask. * If the answers do not match any of the animals in your database, you are to output a message indicating so: Computer: I'm sorry, I've no information on such an animal *** Important rule: each question can only ask a single trait, i.e, you can not ask "is it a tiger" directly - that would be cheating! --- Your program will consist of a number of if-else statements nested within if-else statments. Here's something to get you started with: char mammal; // answer to question is mammal? char fly; // answer to question animal can fly? // declarations for other answer variables ... cin >> mammal; // preceeded by appropriate prompt: if (mammal == 'y') { // ask next question based on answer (is mammal) cin >> fly; // preceeded by "does it fly" prompt if (fly == 'y') { // next question based on mammal that can fly } else { // next question based on mammal that can't fly } } // closes sub-statement for mammal question else { // next question based on non-mammal animal } ------ Make sure that your braces are lined up and commented (unless they enclose a single line of code). I will not look at programs with braces that are not lined up. After you're finished with this part, put a while loop around your entire code so that the program will execute repeatedly: char again; again = 'y'; // 1 is true while (again == 'y') { // code for determining animal, excluding variable declarations cout << "go again? "; cin >> again; } // closes while loop ----- ------------------------------------- What to turn in: 1. Write up on paper, including "decision tree" 2. Source code. All variables used must be commented. Each enclosed {} segment should include comments as to its purpose (as in above example) 3. two sets of output You can copy and past output from the "terminal" into a file in emacs, save it, and print it. */