Author: Jeff Kinne Contents: This file lists programs that you can make to practice your C programming skills. Each question describes what the program should do, gives a hint for what is needed, and has the name of a correct program that has been compiled in this directory. You should run your program and the correct program and many different inputs, and make sure that they have the exact same behavior and output. Try to think of many different types of inputs to test the programs. Correct programs: For many of the questions below, there is a correct program in this directory. Note those are compiled programs, they are not the C/C++ code. They are here so you can run the program to see how it is supposed to behave. These are compiled for linux. You can run them by logging into the CS server and copying them from the directory ~jkinne/public_html/Cpractice/ Or, if you have linux installed on your own computer, you can download the file and run it on your own computer. Note: Some of you have started with C++, others have started with C. For all of the questions in this file, you can use either. References: Here are some references/tutorials on C/C++ programming. I encourage you to read these again and again. When they give an example program, you should type in the program yourself, compile it, and make sure it behaves the way they're talking about. * http://www.cplusplus.com - Reference on C and C++ language and standard library. The tutorial on the C++ language is very good. Most of the information up until "Classes" applies also to the C language. * http://pelusa.fis.cinvestav.mx/tmatos/LaSumA/LaSumA2_archivos/Supercomputo/The_C_Programming_Language.pdf - The standard reference for C programming. Answers to the exercises online at various places, e.g., http://users.powernet.co.uk/eton/kandr2/ It would be very good for you to read through the book, pick out a few exercises to try, and look up the "official" answers after you've solved the problem. * http://markburgess.org/CTutorial/C-Tut-4.02.pdf - A bit more introductory/background information about C programming than the last reference. * http://www.oercommons.org/courses/introduction-to-computer-science-i/view - Videos for a course teaching C, and some other things, like Java. Programming Questions: 1) name: hello behavior: print the message "Hello World." use: cout or printf 2) name: evenodd behavior: ask for an integer and tell whether it is even or odd. use: if 3) name: lowerupper behavior: ask for a chracter and tell whether it is upper case, lower case, or not a letter use: if, isupper, islower, isalpha, #include 4) name: plustest behavior: pick two random integers between 1-10, ask the user what their sum is, tell the user if they are right. use: #include, #include, srand(time(NULL)), rand() % 10, if 5) name: timestest behavior: similar to plustest, but multiply 6) name: alphaorder behavior: ask the user for two different words, tell them which is first in alphabetical order. use: C++ string and <, or strcmp, if 7) name: count behavior: program prints number 1 up to 20 use: for loop or while loop 8) name: countdown behavior: program prints numbers 10 down to 1 use: for loop or while loop 9) name: countmore behavior: program asks user for a number, then prints numbers from 1 up to that number. use: for loop or while loop 10) name: countmultiples behavior: program asks user for start, stop, and multiple. program then prints from start to stop, going adding multiple each time. use: for loop or while loop 11) name: asciitable behavior: program prints out a table for printable ASCII characters use: for loop, printf or cout note: if using C++, see http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/ about writing into a string just like writing to the screen or into files, you need to #include. also, see http://www.cprogramming.com/tutorial/iomanip.html about formatting the output of cout, you need to #include 12) name: multtable behavior: print out a multiplication table for numbers between 1 and 10 use: nested for loops 13) name: addtable behavior: print out an addition table for numbers between 1 and 10 use: nested for loops 14) name: factornum behavior: ask the user for an integer, and then print out the factors of the number. use: for loop or while loop 15) name: primesnum behavior: ask the user for an integer, and print out all the primes up to that number. use: make an isprime function that tests if a number is prime, then use a for loop to test each number 16) name: letterfreq behavior: ask the user for the name of a file, output the frequencies of each character in the file. use: C or C++ files, keep an array of 256 integers, set each to zero intially, read the file one character at a time, each time a character is read increase one of the counts. when the file is done, output the counts. If using C++, see http://www.cplusplus.com/reference/istream/istream/get/ note: if there is a problem opening the file, print an error message. if using C++, see http://www.cplusplus.com/reference/fstream/ifstream/is_open/ 17) name: letterfreq2 behavior: same as above, but now print the frequencies from highest to smallest. use: qsort, see http://www.cplusplus.com/reference/cstdlib/qsort/ 18) name: showfile behavior: ask the user for the name of a file, open the file, read it in and print the contents to the screen 19) name: flipcoins behavior: ask the user for a number of coins to flip, and flip that many coins, output the number of heads, number of tails, and longest run of "heads in a row" use: #include, #include, srand(time(NULL)), rand() % 2 20) name: binomials behavior: ask the user for an integer, and print the binomial coefficients up to that number. If they type 4, you'd print 4 choose 0, 4 choose 1, 4 choose 2, 4 choose 3, 4 choose 4. This is a row in Pascal's triangle. use: make a function called factorial that computes a factorial, then make a function called binomial that computes a binomial coefficient, then use a for loop to print out all the binomial coefficients up to the given number 21) name: powers behavior: ask the user for a power and an upper bound, and print all "perfect powers" of that power up to the given bound. If they enter 2 for the power, you'll print squares. If they enter 3 as the power, you'll print cubes. Etc. use: make function called power that has two parameters (base and power) and computes base^power using a for loop, then have a for or while loop that computes 1^power, 2^power, ... i^power as long as i^power is <= the upper bound.