- (very easy) Calculate the fibonacci sequence. (1,1,2,3,5,8,13,21,...)
The first two numbers are 1, and after that, each number is the sum of the last two numbers. - (easy) Compute prime numbers. See if you can find the first 100 prime numbers.
- (quickie) Base conversion: Write a program to convert numbers to/from binary.
- (easy/moderate) Write a program that reads 10 numbers, and can determine if they are a permutation of the numbers 1-10. A permutation means that each number from 1 to 10 comes exactly once in the sequence. (It also means that if you sort them, you will have 1,2,3,4,5,6,7,8,9,10.)
- (moderate) Write a program that lets 2 people play a game of Tic-Tac-Toe...
Your program will display the board, and reads moves from the players.
It should know when they make an illegal move. As a bonus, your program should be able to know when somebody wins. - (somewhat difficult) Calculate all prime numbers between 1 and 10000 using your algorithm from #6. Use these to find all 7-digit prime numbers. (Don't output them, just count them.)
- (Really good) Try to figure out and implement your own sorting routine. That is, write a program that reads 10 integers, and outputs them, sorted from smallest to largest. After that, try to make it work for N integers, where you get N from the user, too.
- (Really good) Write a binary search. This is where you find a number based on queries in which you guess a number and are told if the real value is less than that or greater than that. A nice way to write it would be this:
You ask the user to pick a number between 1 and 100. Your program tries to figure this number out, by giving you its "guess"es. When it gives you a guess, you tell it the guess is smaller, than the number, or larger, or if it's correct. (This can be done by giving the program a 0, 1, or 2.)
Your program would always keep a range of numbers it knows your number to be in.
It gives you the number in the middle.. Then it either wins, or it can divide it's range in half. (So for example, it starts by asking 50, you say it's lower than 50, then it asks 25, you say it's higher than 25, then it asks 37, and so on.)
This is a very good exercise, and I really recommend that you try it. - (difficult and a bit tiresome) Implement large-number functions. For example, write a program that can multiply 100-digit numbers together.
- (Very very difficult!) Write a program that prints itself. (Just kidding! Don't try to do this! (But note that it is possible.. It's called a quine))
Thursday, February 11, 2010
C++ Practice problems
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment