Case studies over control structures and iterative structures Instructor – GokcenCilingir Cpt S 121 (July 6, 2011) Washington State University
Problem 1 • Problem statement: Calculate number of digits in an integer. Prompt the user for an integer and output the number of digits Sample I/O: Input: 123, Output: 3 Input: 123456, Output: 6 Input: 0, Output: 1
Problem 1 – another related application • Problem statement: Print a given integer such that its digits are reversed. Sample I/O: Input: 123, Output: 321 Input: 123456, Output: 654321 Input: 0, Output: 0
Problem 1 – another related application • Problem statement: Calculate the integer that has the reversed digits of a given integer. • Prompt the user for an integer and output the reversedInteger and reversedInteger *2 Sample I/O: Input: 123, Output: 321, 642 Input: 123456, Output: 654321, 1308642 Input: 0, Output: 0, 0
Problem 2 • Problem statement: Write a program that experiments with the random number generation function rand() of C. Program should prompt user for sample size, and calculates the average of random numbers generated for the given sample size. • Generate random numbers between [0, 10] and observe that average will be closer to 5.0 when your sample size increases.
Problem 3 • Problem statement: Write a program that reads and flattens a text file. • Assume file contains words (which are comprised by alphanumeric characters) separated by whitespace and punctuation characters. • Program should flatten the file, meaning should output the word list to an output file in the order of encounter, ignoring whitespace & punctuation, separating them by newline characters. • If time doesn’t allow to solve, it will be assigned as a lab problem!
Problem 4 • Problem statement: Write a program to print a one month calendar. The user specifies the number of days in the month and the day of the week on which the month begins. • Sample execution: • If time doesn’t allow to solve, it will be assigned as a lab problem!