Weekly Programming Exercises
Following your lab 1 lab notes, attempt C/C++ coding for the following exercise questions. These exercise questions will help you practise coding objectively, in preparation for your coding projects.
1. Personalised greetings: Write a program that asks the user for their name(s) and the uses the clock time to issue a personalised greeting, as follows:
Good morning, Name. Good evening, Name.
2. Square Root: Using function(s), write a program that asks the user for a number, and then determines if the number has an integer squared root or not. e.g.
Example 1: Enter a number: 123 The number does not have an integer root.
Example 2: Enter a number: 64 The number has an integer root of 8.
3. Inverse Matrix: Write a program that asks the user for a matrix dimension and fills the matrix with random initial values. After that the program checks if the matrix can be inversed. If it is - it will then show the original and inverse matrices.
4. Least Common Denominator: Use function(s) to write a program that asks the user for a list of numbers and then finds out the least common denominator for the numbers.
5. Simple Encryption: Write a program (encrypt.cpp) to encrypt a message, which is an array of characters: char msg="Very Important Message" (for example), declared and initialized in the main function. The message will be encrypted as follows:
Update each character's ASCII code (i.e. (unsigned int) msg[c], where c is the iterator) as
Current ASCII code = ((previous ASCII code * 9) % 96) + 31;
Implement the encryption through a function called do_encrypt(char ).Before and also after this encrypt operation, show the encrypted message on the standard console output through another function, called display_message(char ). Declare all the associated variables required.
6. Text Formatter: Write a program which can clean up bad spacing between words. For example, the following line:
"This is a badly spaced line entered by a user"
would look like the following:
"This is a badly space line entered by a user"
To allow for string inputs effectively, you can use the following
string a_line; // the string variable getline (cin, a_line); // getline reads the entire line until carriage return is hit.
7. Hangman: Create your personal dictionary of words using string arrays and then play a Hangman game with a user.
8. Fibonacci Fish: Did you know Fibonacci numbers? If you do not know, read about them. Within nature Fibonacci series manifests in very many forms, for example, the winter birds use Fibonacci series when they fly as a flock, some fish swim under water in a pattern using the Fibonacci series and sunflowers have their seeds organised in a Fibonacci series too. Write a program (fibonacci.cpp) that takes the series sequence number as an argument, generates the fibonacci numbers until the sequence and displays the number of fish in each line based on the fibonacci numbers in the series on the screen. For example, when you have 7 as the argument, it may produce a fish sequence like below:
><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> ><((°> > <((°>
You can use a function to draw the fish -- get some ideas from the characters above.
9. Fibonacci Fish with File: When you have a large number input as the sequence for Fibonacci, it can be difficult to display on the output console. Can you modify the program above and write the fish sequence in an output file called: fish.out? You can open this file as a text and use lower sized font settings to see a large sequence of fish.
10. Voltage Sweep in Power Computation: Power consumption in electronic circuits is given by: Power = Voltage Voltage Frequency; Write a program that takes three arguments as part of the main function: the lowest positive voltage (>0.3V), the highest positive voltage (<1.5V) and the linear step of voltage increase (as a double data type). The program should calculate the power at different voltages and frequencies and produce a CSV (comma separated values) file output (say power.csv) as follows (assuming the inputs as follows: ./main -lowvdd 0.35 -highvdd 1.2 -step 0.05). The 0 values entries should be filled in with the calculated values and the voltage rows will continue until the higher voltage value is reached.
Current voltage, Power@20M Hz, Power@40M Hz, Power@60M Hz, Power@ 80MHz, Power@100MHz
0.35, 0, 0, 0, 0, 0 0.40, 0, 0, 0, 0, 0 0.45, 0, 0, 0, 0, 0 0.50, 0, 0, 0, 0, 0
and so on When you have generated this output file, you can plot the power plot using Excel.
11. Least Common Denominator (re-visit question 4): Re-visit the question 4, this time break the program in multiple functions, and use variadic function (we will see examples this week) to allow for funding the least common denominator from an arbitrary number of parameters, e.g. lcd(2, 4, 7) or lcd(4, 5, 7, 9, 13). Make sure you have your functions in your own header file, and you are able to use them by including your header file/library.
12. PI computation: Did you know PI can be computed using the following series:
1 1 - 4 ( 1 -1 -3- + -5 - - 1 9 -...1
Write a program that can compute PI using a recursive function. Your recursive function will take number of series terms as the input (the above example shows 5 series terms until 1/9) and return the resulting PI value. Use functions and header files where you can.
13. The perimeter of Earth: Use the program you generated in Q 12 above to calculate the perimeter of the earth in meters (use the formula: 2 x PI x radius). Assume the earth has a radius of 6377768 meters and use the PI computation with 12 series terms as the most accurate reference for the PI value. Now find the errors (in meters) in perimeter calculation when lower number of series terms are used to approximate the nearest PI values. Computer Systems & Programming
14. Hangman Game with a dictionary file: Re-design your Hangman game with a dictionary file (made by you) to choose a word randomly and ask the user to guess the word one alphabet at a time. Have separate libraries and sources files for implementing the game. Total 6 guesses are allowed. Show a Hangman on display incrementally with more misses, like below: Guess the following word: an
15. Address Book: Use C/C++ structures to build an address book manager (e.g. first name, last name, phone number, date of birth and correspondence address). You should be able to add new records, delete existing records and list the records and sort them by any column within the address book.
16. Sequence Generator: Ask the user for a set of characters and then create a combination of all possible sequences of the characters -- sort the sequence alphabetically.