|
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. 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 } STATEMENTS - Several statements play a key role in programming. A few are listed below.
Cootie Catcher Code #include // 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 } 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: 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://www.acm.org/crossroads/xrds1-1/ovp.html http://www.icce.rug.nl/docs/cplusplus/cplusplus.html 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 |