Physics 5—Scientific Computing Instructor: Geoff Hagopian, |
|||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||
Assignments3.15: Math Tutor Write a program that can be used as a math tutor for a young student. The program should display two random numbers to be added, such as
The program should then pause while the student works on the problem. When the student is ready to check the answer, he or she can press a key and the program will display the correct solution:
3.17: Monthly Payments The monthly payment on a loan may be calculated by the following formula: Here R is the monthly interest rate, which is the annual interest rate divided by 12 so that, for instance, 12% annual interest is 1% monthly interest. N is the number of payments and L is the amount of the loan. Write a program that asks for these values and displays a report similar to
3.25. Word Game Write a porgram that plays a word game with the user. The program shoulod ask the user to enter the following :
After the user has entered these items the program should display the following story, inserting the user's input into the appropriate locations: There once was a person named NAME who lived in CITY. At the age of AGE, NAME went to college at COLLEGE. NAME graduated and went to work as a PROFESSION. Then, NAME adopted a(n) ANIMAL named PETNAME. They both lived happily ever after. 4.2. Roman Numeral Converter Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. Input validation: Do not accept a number less than 1 or greater than 10. 4.9 Math Tutor This is a modification of 3.15 above. Write a program that can be used as a math tutor for a young student. The program should display two random numbers to be added, such as
The program should then wait for the student to enter the answer. If the answer is correct, print a message of congratulations. If the answer is incorrect, a message should be printed shoing the correct answer. Here's an pseudocode/outline for how to carry out this game. Initialize word, fillIn, list of bad letters, number of turns, number of good letters, etc. Repeat the following: So far you've got ----- What would you like to guess? a What would you like to guess? e What would you like to guess? t What would you like to guess? o What would you like to guess? i ==and so on================== What would you like to guess? p What would you like to guess? c Congrats, you guessed it! crimp! ============================ Doublets is a game invented by Lewis Carroll (aka Dodgson) which you can get detailed information about doublets at Riddles On Line, ThinksDotCom , LogicVille, or the Google Books result. To play doublet, start with 2 four-letter words (appropriate for polite company) and by changing only one letter at a time, produce a sequence of words starting with the first given and ending with the last given. For example, the sequence "good, food, fool, foil, fail" goes from "good" to "fail" in a minimum of 3 words in between. The challenge of this assignment is to write c++ code that will play this game. In the lecture last week, we developed a sequence of problems you would need to solve to work out the code for such a program. Here's the list of tasks we came up with: 1. Obtain two words from the user and validate that they are four letter words. To do this validation we used a while loop
If you have another way to do this, please go ahead and use it. 2. Validate that the words the user enters are in our list of four-letter words. You can use this file: FourLetterWords.txt. You will need to include the fstream header file with #include <fstream> and create an input file stream object, say with Note that there are a number of ways to manipulate the ifstream object. The web site http://www.cplusplus.com/reference/iostream/istream/ is a good place to get information about these. In particular, the member function seekg is useful for positioning. For instance, when you want to look for another word in the FourLetterWords file, reset the read position of the readFile "object" using its member function, seekg()like so: readFile.seekg(0,ios::beg); The function seekg is a member function of the ifstream object we created and it is accessed by the dot operator. Also note the use of the binary scope resolution operator (::). Now here's the code we developed in class for validating that the word is in the approved list:
The idea is that for each letter in the given word we can read through the FourLetterWords text file and look for a match. If no match is found, we'll know because we're at the end of the file. But there's a logic flaw. Look at the following output to see the flaw in action:
So neither "COUT" nor "CRIP" are in our dictionary, though some dictionaries list "crip" as word for "cripple" which is generally considered offensive, so I'm just as glad to leave it out.
In the following pseudocode for the basic logic I used in tackling this, I use two boolean flags: one to indicate whether or not the word has been found yet and one for whether or not the search has failed: 1. Set flags to indicate that the word has not yet been found and the search has not failed. 4. To play this game on a computer, it may be useful to define the "value" of a four-letter word as the number of four-letter words which differ from that word by 1 letter.
When you're done with problem 3 you should put it all together in program that will reliably produce output similar this:
As an aside, the Scrabble dictionary contains some questionable words that traditional dictionaries don't necessarily include, but all of these seem to be good. Merriam Webster offers this for GAMP: Britain : a large umbrella *halfway to the station my gamp blew inside out Sydney (Australia) Bulletin*; often : one that is untidily or loosely tied up *if you carry an umbrella use it tightly rolled and never as a gamp S.D.Barney TAM is short for tam-o-shanter, a woolen cap of Scottish origin that is made with a tight headband and a very wide flat circular crown usually with a pompon in the center compare BONNET TUMP has two definitions: 1 chiefly dialect England : a small rise of ground: as a : MOUND, HUMMOCK b : MOLEHILL c : ANTHILL d : 1BARROW 2, TUMULUS TYMP is short for tympan: the stone or the water-cooled iron casting protecting the top of the opening through which molten slag and iron continually pass into the forehearth in an old type open-front iron blast furnace VAMP is loaded with meaning. 1 dialect : a short hose coming to the calf formerly sometimes worn over a stocking : SOCK transitive verb ============================================================== Write C++ code to implement the Babylonian algorithm as described in the flow chart below ======================================= Write a function called isPrime that will take an integer and determine whether or not it is prime. Start be validating that the integer is greater than 1. Then write a program that uses isPrime to write the prime numbers less than 100 to a file.
==========================================
================================= Read the paper here and answer the questions about it at ILRN.COM. The ultimate goal here is to develop some robust tools for investigating king's tours under various rules and conditions. We'll take it one baby step at a time.
|