Computer Science E50a Wednesday Section . 7:30 . Emerson 106 TF: Alix Marchandise-Franquet November 18th, 1998 1- A word on Asst 2.1 * Comments: include a comment at the top of your file that describes what the program does. * Testing: when you make .out files for your programs, you must show how your program behaves with different input. Therefore, you should include examples of the general case, but also cases where invalid input is entered. Finally, I would like you to include the examples from the problem set handout. You may run the same programs several times in one script (i.e. only one .out file per program, but with different sample outputs of the program in it). * You MUST provide printouts of the .out files! * Constants: they should be in uppercase! Give them an appropriate name. * Variables: make sure you always give a variable the appropriate type (for example, int vs. float for the number of days in the rocks program). Also, give them a name that means something, not just x, y or foo. . Finally, you don't need to initialize the ones that get their value from user input. * Integer division: the result of dividing an integer by another integer is going to be an integer. To get a float as the result, you must cast one of the integers. Ex: int days, rocksum; float average; // get the values for days and rocksum average = float(days)/rocksum; * Return: If you write int main(), it means that the function main will return an integer (you will get a warning at compile time if it doesn't). Therefore, you should have return; before the end of your main function. Usually, return 0 if the program terminates successfully, 1 if there was a problem (ex: bad input...) If you write void main(), then you cannot return 0 or return 1, you can only write return; (otherwise, you will also get warnings at compile time). 2- Switch The switch statement is a good way of avoid multiple if statement. switch (expression) { case value1: // do this if expression has value 1; break; case value2: // do this if expression has value 2; break; case value 3: // do this if expression has value 3; break; default: // do this if expression has neither value 1, nor value 2, nor value 3 } Warning: don't omit the break. If you do, everything will be executed until either a break is encountered or until the end of the switch. 3- Enumeration enum lets you define your own variable types, and specify the possible values (the enumeration constants) for this type. You can only define a given enumeration constant once. You should put the name of the enumeration constants in uppercase, and start the name of the enumeration with an uppercase letter. enum EnumName { ENUMCONST1, ENUMCONST2, ENUMCONST3 }; By default, ENUMCONST1 = 0, ENUMCONT2 = 1 and ENUMCONST3 = 2. You can change that by assigning an integer value to the first enumeration constant. enum EnumName { ENUMCONST1 = 1, ENUMCONST2, ENUMCONST3 }; in this case, ENUMCONST1 = 1, ENUMCONT2 = 2 and ENUMCONST3 = 3. Why is this useful? You can now create new types, i.e. restrict the values that a variable can have. For example, you can create an enum for the months of the year, therefore limiting the values of a variable representing a month between 1 and 12. ex: enum Month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};