Homework 5: Decision Trees

Wednesday, July 25, 2012 by 5:00pm     


[Previous Homework] [Next Homework]


Objectives

More practice with if statements.

Assignment


For this assignment, you will write a program that implements this decision tree for cold and flu symptoms from FamilyDoctor.org. (Here is a local copy in case FamilyDoctor.org is inaccessible.)

The decision tree helps the user determine if he has a cold or a flu or something else.


PT[201]% gcc -Wall cold.c 

PT[202]% ./a.out
Do you have a fever? (y/n) n
Do you have a runny and/or itchy nose, sneezing, and itchy eyes? (y/n) y
You may have ALLERGIES

PT[203]% ./a.out
Do you have a fever? (y/n) y
Do you have a sore throat and headache --- without nasal drainage? (y/n) n
Did your symptoms start suddenly, and do you have a combination of
symptoms including muscle aches, chills a sore throat, runny nose or
cough? (y/n) y
You may have the FLU.

PT[204]% ./a.out
Do you have a fever? (y/n) n
Do you have a runny and/or itchy nose, sneezing, and itchy eyes? (y/n) n
Do you have sneezing, a sore throat, headache, congestion and a runny
nose? (y/n) y
You probably have a COLD.

PT[205]% ./a.out
Do you have a fever? (y/n) y
Do you have a sore throat and headache --- without nasal drainage? (y/n) n
Did your symptoms start suddenly, and do you have a combination of
symptoms including muscle aches, chills a sore throat, runny nose or
cough? (y/n) n
Do you have a persistent cough that brings up yellowish or greenish
mucus, wheezing and shortness of breath? (y/n) n
Do you have a headache or muscle aches, nausea or vomiting, and watery
diarrhea? (y/n) n
You should go see a doctor!
PT[206]% 

Notes

  • New note: If you are having trouble with the nested if statements, this pseudocode might help you. Notice that it is really, really, really, really, really, really, really important to indent as you are typing. Indentation greatly facilitates debugging.

  1. Please name your C program cold.c

  2. If the user answers 'no' to Question 5 or Question 8, your program should tell the user to go see a doctor.

  3. You do NOT have to printout the "Self-Care" column in the decision tree. (Too much typing.)

  4. To read in a single character typed in by the user (e.g., the letter y), use the following:

    
          scanf("%c%c", &reply, &cr) ;
       

    where reply and cr were declared as variables of type char earlier:

    
         char reply, cr ;
       

    You have to read in two characters because you want to capture the carriage return typed in by the user. This gets stored in the character variable cr, but you don't really need to use cr anywhere. See if8.c for an example.

  5. After you read in the character typed in by the user, you can check if the user typed a lower case y or upper case Y using:

    
          if (reply == 'y' || reply == 'Y') 
       

    You may assume that if the user did not type in a y or a Y that he meant 'no'.

  6. You will need several nested levels of if statements. You MUST indent your program. Otherwise, you will be horribly lost.

  7. If you ask someone to debug an unindented program, it is perfectly reasonable for that person to ask you to come back after you have indented your program.

  8. One way to avoid messing up your braces and indentation is to type in the braces as soon as you type in your if condition. That is, first type in:

    
          if (reply == 'y' || reply == 'Y') {
          }  else {
          }
       

    Then go back and fill in the body of the if-part and the body of the else-part (properly indented).

  9. There is a lot of repetition in the program. Learn how to use cut and paste in nano. Here's a Beginner's Guide to Nano from How-To Geek. Basically:
    • use ctrl-shift-^ to "mark" a place in your program
    • use arrow keys to select the lines to copy
    • use ctrl-K to cut the selected text
    • immediately type ctrl-U to put the text back
    • use arrow keys to move the insertion point where you want
    • use ctrl-U to paste in a copy of the previously selected text

What to submit

Use the script command to record yourself compiling your program and running your program 5 times using different cold/flu symptoms. You test runs should result in 5 different diagnoses. Use exit to terminate the recording. Only record yourself compiling and running your program. DO NOT record yourself editing your program. If you mistakenly start up nano while running script, just exit from script and start over. (The new typescript file will overwrite the old one.)

When you are done. Use:


cat typescript

to check the contents of your typescript file. Make sure it does not include you editing your program. Then submit your program and typescript file:


submit cs104 hw05 cold.c typescript