CpSc Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input 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) the pow() function from the math library a loop that will allow the user to go again 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? if (n < 2) printf( Hello ); printf( Tigers!!\n ); What does it print if the value of n is 2?
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 >= 3) && (age <= 9)) 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 ); The math.h Library Another library provided by C is the math.h library. This library provides math functions that you can use, such as pow(), sqrt(), abs(), cos(), sin(), tan(), log(), among many others. Pages 485-492 of the Programming in C book list many of the functions in this library. You can also learn about the math library functions by typing man at the Unix command prompt, a space, the function name, and then pressing Enter. For example, to learn about sqrt you would enter: man sqrt To get some experience using the math library, for this lab assignment, you will be using the pow() function to cube a number. One last thing for you to know - you will need to compile your program with an option (l for library, m for math), for example: gcc -Wall lab6.c lm Redirecting Input In lab 3, you learned that you can redirect output to a file by typing something like this: > output.txt You can also redirect input from a file as well. If your input file is called following: input.txt, then you would type the < input.txt You can even combine these and do both on the same line: < input.txt > output.txt 3
Lab Assignment Part I: Write a program called lab5.c which will prompt the user to enter a single letter or number on the keyboard. If the user enters a lower case letter, the program will print out the same letter as a capital letter. If the user enters a capital letter, the program will print out the same letter in lower case. If the user enters a number, the program will print out a message saying if it is an even number or an odd number. Also, the program will print to the user what that number cubed is. If the user enters anything that is not a letter or a number, the program will print out a message indicating that they entered an invalid character. Your output should look similar to the following, which shows 5 separate runs: Enter a letter or number from the keyboard. d The upper case of that letter is: D. Enter a letter or number from the keyboard. R The lower case of that letter is: r. Enter a letter or number from the keyboard. 6 You entered an even number. 6 cubed is 26. Enter a letter or number from the keyboard. 3 You entered an odd number. 3 cubed is 27. Enter a letter or number from the keyboard. $ Invalid input. Try again. Part II: Once you get that working, make the program loop continually, asking the user if they want to go again, using a for yes and a 0 for no. If they enter a, then they will be prompted to enter a single letter or number on the keyboard again, displaying the results explained above. If they enter a 0, then the program will quit. With this loop added, your output should look similar to the following: Enter a letter or number from the keyboard. d The upper case of that letter is: D. Would you like to go again? Enter for yes, 0 for no. Enter a letter or number from the keyboard. R The lower case of that letter is: r. Would you like to go again? Enter for yes, 0 for no. Enter a letter or number from the keyboard. 6 You entered an even number. 6 cubed is 26. Would you like to go again? Enter for yes, 0 for no. Enter a letter or number from the keyboard. 3 You entered an odd number. 3 cubed is 27. Would you like to go again? Enter for yes, 0 for no. 0 4
Once you are certain that your program works with the addition of the loop, add some code that will check for invalid input for going again. For example: Enter a letter or number from the keyboard. 3 You entered an odd number. 3 cubed is 27. Would you like to go again? Enter for yes, 0 for no. 5 Invalid input. Enter a to go again, or a 0 to quit: 2 Invalid input. Enter a to go again, or a 0 to quit: 0 Only add a check to see if they entered a number other than or 0 to go again you don t need to check for other characters. Part III: Create a file called input.txt and in that file, put the inputs to the program that would otherwise have been entered from the keyboard. Using the first example output from Part II above, your input file would contain the following: d R 6 3 0 Run your program again, redirecting the input, like the following (you should see the same results as above): < input.txt Then try the following: < input.txt > output.txt Type ls to verify that the file output.txt was created. Then type the following to view the contents of that file: cat output.txt Hint #: You will need to refer to an ASCII table for this program. Remember that characters are really a type of integer, and you can refer to a character by its corresponding integer value. Hint #2: With your scanf statements asking the user to enter a character or number, for the format strings, inside the quotes put a space in front of the %. For example, use %c instead of %c. 5
Reminder About Formatting and Comments The top of your file should have a header comment, which should contain: o Your name o Course and semester 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 Turn In Work Show your ta that you completed the assignment. Then submit your lab5.c file to the handin page. Grading Rubric For this lab, points will be based on the following: Functionality 80 (correct use of conditionals: 60; loop to go again: 20) Formatting 0 Use of pow() function 5 Catches invalid input 5 (any number not a or 0; don t worry about catching other invalid input, such as a character) 6