CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes Overview For this lab, you will use: one or more of the conditional statements explained below scanf() or fscanf() to read in integer values entered by the user (from standard input) a loop that will allow the user to go again ASCII table redirection to get input from a file, as well as send output to a file Background Information All programming languages provide constructs to execute a section of code conditionally. In this week s lab, you will implement decision making with the use of one or more of the following conditional statements. if Statements The simplest conditional statement is the if statement. It is used when we want the computer to maybe execute some code based on the truth value of some condition. If the condition is true, the code in the body of the if statement will execute; if it is false, the body of the if statement will be skipped and execution will continue with the code immediately after the if statement. Structure: if (condition) Example of an if statement: if (n < 2) printf( Hello\n ); The above code prints a message only if the value of n is less than two. Otherwise, it does nothing. What does the following code print if the value of n is 1? if (n < 2) printf( Hello ); printf( Tigers!!\n ); What does it print if the value of n is 2? 1
if-else Statements The previous if statement included code that executes when a condition is true. If we want certain code to execute when a condition is true and other code that executes when a condition is false, we use if-else statements. Exactly one of the two possible branches will be taken with an if-else statement. Structure: if (condition) else Example: if ((age >= 13) && (age <= 19)) printf( You are a teenager.\n ); else printf( You are not a teenager.\n ); if-else-if Statements When there are more than two options, instead of nesting if-else statements, C provides us with the if-else-if construct. Structure: if (condition) else if (condition) else if (condition) else Note: The last else is optional and works well whenever there is a fall through case when all other conditions are false. If there is no fall through option needed, then it may make sense to not have that last else at all. 2
Example: if ( (day > 2) && (day <= 6) ) printf( weekday\n ); else if (day == 7) printf( Saturday\n ); else printf( Sunday\n ); Dangling else Problem The compiler associates an else- part with the closest if. The following code illustrates this point. What does it print? (Remember that the compiler ignores formatting.) int n = 5; printf( hello\n ); if (n < 4) if (n > 0) printf( good\n ); else printf( bye\n ); Redirecting Input In lab 3, you learned that you can redirect output to a file by typing something like this:./a.out > output.txt You can also redirect input from a file as well. If your input file is called input.txt, then you would type the following:./a.out < input.txt You can even combine these and do both on the same line:./a.out < input.txt > output.txt ASCII Code The data type for a single character is char. Each character is represented in 1 byte (8 bits) by a specific binary value. On most computers today, that value is defined by the American Standard Code for Information Interchange, or ASCII. The following link will bring up an ASCII table: http://www.asciitable.com/. If you look at the chart starting with the lowest characters, you will find numbers, then upper case letters, and then lower case letters. Special characters are sprinkled around these ranges. The type char in C is really a type of integer. For example, if you had the following line of code in C: printf( %c, 115); the lower case letter s would be printed to the screen because 115 is the decimal value representing that character. 115 corresponds to the binary number 1110011 which, padded out to 8 bits, would be 01110011. In other words, when we press the s key on the keyboard, the processor gets the binary number 01110011 which to us, in decimal, is 115. What does the following print statement print? printf( %d, k ); 3
Lab Assignment Reminder About Style, Formatting, and Commenting Requirements The top of your file should have a header comment, which should contain: o Your name o Date o Lab section o Lab number o Brief description about what the program does o Any other helpful information that you think would be good to have. Variables should be declared at the top of the main function, and should have meaningful names. Always indent your code in a readable way. Some formatting examples may be found here: https://people.cs.clemson.edu/~chochri/assignments/formatting_examples.pdf Don t forget to use the Wall flag when compiling, for example: gcc Wall lab5.c Part I: Write a program called lab5.c which will prompt the user to enter a single letter or number (i.e., a character) on the keyboard; use fscanf() to get the input. NOTE: use %c instead of %c for the format string in your fscanf where there is a space in front of the % inside the quotes. Then print to the user the decimal (integer) representation of that character. Use the ASCII table to confirm that it is correct. Compile and run to verify this much works before continuing. Then calculate 2 times that integer value, showing that result. Compile and run after this step. Use that product as a hypothetical wind speed value and show what category hurricane it would be. Using a conditional statement, incorporate the values from the following table to show the result to the user: Compile again and run to make sure it works before continuing. Add a loop asking the user if they would like to go again, using a 1 for yes and a 0 for no. See sample output on the next page for an example of what your program should look like when run. 4
Enter a character: # The decimal representation of your character # is 35. Would (2 * # ) be a hurricane?? (2 * # ) = 70 Wind speed of 70 is not strong enough to be a hurricane. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Enter 1 to go again or 0 to quit: 1 Enter a character: T The decimal representation of your character T is 84. Would (2 * T ) be a hurricane?? (2 * T ) = 168 Wind speed of 168 is a Category 5 hurricane. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Enter 1 to go again or 0 to quit: 1 Enter a character: 3 The decimal representation of your character 3 is 51. Would (2 * 3 ) be a hurricane?? (2 * 3 ) = 102 Wind speed of 102 is a Category 2 hurricane. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Enter 1 to go again or 0 to quit: 0 Part II: Create a file called input.txt that contains the input values for testing your program. Make sure to test all the possible conditions of your if-statement. Then, run your program using your input file by typing:./a.out < input.txt and verify the output. You do not have to change anything in your code - your fscanf() statement will still be using stdin as the first argument. When you redirect the input from a file, it acts as if the values are still being entered on the keyboard by hand. Once you verify your program works, type the following to redirect the output as well:./a.out < input.txt > output.txt Then, as you did in lab 3, use the cat command to view the contents of your output file, clearing the screen first: clear cat output.txt 5
Turn In Work 1. Before turning in your assignment, make sure you have followed all of the instructions stated in this assignment and any additional instructions given by your lab instructor(s). Always test, test, and retest that your program compiles and runs successfully on our Unix machines before submitting it. 2. Show your TA that you completed the assignment. Then submit your lab5.c program, and your input.txt & output.txt files using the handin page: http://handin.cs.clemson.edu. Don t forget to always check on the handin page that your submission worked. You can go to your bucket to see what is there. Grading Rubric If your program does not compile on our Unix machines or your assignment was not submitted on time, then you will receive a grade of zero for this assignment. Otherwise, points for this lab assignment will be earned based on the following criteria: Functionality 60 Formatting 10 Use of conditional statement 10 Use of loop to go again 10 Inclusion of input and output files 10 Penalty for not following this lab s instructions (incorrect I/O, etc.) -5 if warnings when compile, and other point deductions decided by grader 6