Elements of Computer Code  |  Cootie Catcher Code  |  Experiment  |  Further Exploration  |  Contact Information


Double X Computer Cootie Catcher

In order to get a piece of electronics to execute an algorithm the most flexible method is writing a computer program. In today's experiment, we will use C++ to make cootie catchers on the computer. After we write the code we compile the program which translates our high-level code into machine language that the computer can execute.

Elements of Computer Code

VARIABLES - A variable is a name that refers to a value. The data represented by a variable can assume different values. All variables have a type or class associated with them, for example a variable might represent an integer (int), real number (float), or a character (char). Before a variable canbe used in a program it must be declared, i.e. the name and type of variable must be stated.

FUNCTIONS - A function is a procedure that returns a value. When a function is declared it must indicate what type of variable it will return. If the function does not return anything, the return type is called void. A function can also take input parameters to operate on. A simple function to add two numbers is included below to show the structure of a function.

int                     // Function Add will return an integer value
Add(int num1, int num2) // Function Add has two integer input parameters,
{                       //  num1 and num2
 int sum;               // Declare integer variable sum
 sum = num1 + num2;     // Assign num1 + num2 to sum
 return sum;            // Function Add returns sum
}

COMMENTS - Comments are notes written by a programmer for the sole purpose of providing information about the program. These notes are ignored by the compiler. A double backslash indicates that the rest of the line is a comment.

STATEMENTS - Several statements play a key role in programming. A few are listed below.
  • An if statement allows selective execution of parts of a program based on the truth of a condition.
  • An else statement can follow an if statement and will be executed if the condition of the if statement if false.
  • A while statement is an iterative statement that repeats as long as a given condition is true.
OPERATORS - Operators are symbols that represent an action to be performed. The assignment operator, denoted by "=", assigns a value to a variable. Mathematical operators perform mathematical functions and logical operators perform a logical operation on two operands, returning a "1" if it is true and a "0" if it is false. The following table lists several operators.

==   Equal relational operator +    Addition operator
!=   Not-equal relational operator -    Subtraction operator
&&   Logical AND operator *    Multiplication operator
||   Logical OR operator /    Division operator
>    Greater-than relational operator %    Modulus (remainder) operator
<    Less-than relational operator

Cootie Catcher Code

#include // This allows for input and output

// Fortune is a function that prints out a message depending on what number
// is input.

void // Return type is void
Fortune(int number)
{
  if (number == 1) // If number is equal to one
  {
    cout << "Congratulations! You will win the lottery today!" << endl;
  }
  else if (number == 7 || number == 8) // If number is equal to seven or eight
  {
    cout << "Romance is in your future." << endl;
  }
  else if (number > 2 && number <= 5) // If number is less than or equal
  { // five and greater than two

    cout << "Your smile brings happiness to others." << endl;
  }
  else if (number == 2 || number == 6) // If number is equal to two or six
  {
    cout << "Fame and fortune will shower their blessings upon you." << endl;
  }
  else
  {
    cout << "The full moon will bring good luck to you." << endl;
  }

  return; // Return nothing
}

void
main()
{
  int num1; // Declare variables num1 and num2 to be integers
  int num2;

  cout << "Pick a number from 1 to 8: " << endl;
  cin >> num1;

  if (num1 % 2 == 0) // If input number is even
  {
    cout << "Pick one of the following numbers - 1,2,5,6: " << endl;
    cin >> num2;
  }
  else
  {
    cout << "Pick one of the following numbers - 3,4,7,8: " << endl;
    cin >> num2;
  }

  Fortune(num2); // Call Fortune to print message
}

Experiment

1.  Before we compile and run the cootie catcher code, let's examine it. What do you think the output will be if you enter a 4 and then 6? What about if you enter 10 and then 12?

2.  Compile the cootie catcher code and try entering in the above sets of numbers. Was your hypothesis correct?

3.  Modify the code by removing the else statement so that only valid numbers will produce output. Compile the code and check the above sets of numbers to see what happens.

4.  Using the information gleaned from the steps above, modify the cootie catcher code to make your own cootie catcher. Things to consider are:

a.  How many levels of numbers do you want the user to enter?
b.  What is the range of numbers you want to use?
c.  What fortunes do you want to print?
d.  How can you keep the user from entering invalid numbers?

Further Exploration

Many of the definitions for this handout are from "Practical C++ Programming" by Steve Oualline. This is another great reference if you are interested fin this material. Taking your high school's C++ course is another great way to learn more. The web is also a great resource and I've included the websites with C++ tutorials below.

http://members.tripod.com/avinashs/cppref.html
http://www.acm.org/crossroads/xrds1-1/ovp.html
http://www.icce.rug.nl/docs/cplusplus/cplusplus.html
Contact Information

If you have any further questions about the experiment or electrical engineering, please feel free to send me an email. Lisa Brown - lbrown@eecs.berkeley.edu




Double X Home