Pseudocode and Comments

From 22101
Jump to navigation Jump to search
Previous: Simple File Reading Next: Python Input-Output

Required course material for the lesson

Powerpoint: Pseudocode, comments and user interface
Video: The use of pseudocode and comments
Video: Playing the guessing game, strategies and advice.
Resource: Example code - File Reading
Video: Live Coding
Blog: On User Interface 1, by the founder of StackExchange.
Blog: On User Interface 2, by the founder of StackExchange.
Blog: On User Interface 3, by the founder of StackExchange.
Blog: Designing the software product, by the founder of StackExchange.

Subjects covered

Pseudo code, which is "pretend" code, that tries to look like a program.
Comments, which is essential for understanding, maintenance, explanation, memory and other good stuff.
User interface, how the program should interact with the user.

Advice

You might want to consider what would happen if you feed an empty file to your program.
Are you looking for files? See the top line of the Programme page.

Exercises to be handed in

In all these exercises, start with writing pseudo code first and the program after.

  1. Make a program that asks for two numbers (integers), and calculates the mean (average) of those numbers. The mean must be converted to an integer. Displays result along with input numbers. Make sure what is displayed is explained well.
  2. Write a program that asks for a file name and then open the file and count the number of empty lines and non-empty lines in the file. Display the two counts. Test it on the mixedlines.txt file.
    Warning: Just because you can not see it, it does not mean it does not exists. Invisible characters have long been a curse of programmers. So do not make them yourself.
  3. Returning to last weeks exercise 4, where you had to ask for a file name and you program would then calculate the average of the numbers in the file. Do the same this time, except you have to continue to ask for file names and calculate the average until you enter nothing - no file name. Test on the same files as last time.
  4. Now for some playing. Make a program that guesses a number between 1 and 10 that you think of. It should make a guess, and you shall answer yes (if correctly guessed), higher (if the number you think of is higher than the guess) or lower (if the number you think of is lower than the guess). The program ends when the number is guessed correctly, otherwise it tries again. It is NOT considered OK to guess at a number more than once, i.e. no repeats. It is counterproductive to try to make this random guessing. Also think about how simple you can make this and still fulfill the wording of the exercises. Just don't run with the first idea you get. Don't embellish with additional functionality - it will just get difficult and we save that for later. Just to be clear - the number you think of must nowhere be in the program.
  5. Make a program that asks for a positive integer. Then it should print the same number of lines. On the first line there must be one #, second line two #, third line three #, and so forth. Do not enter too high a number or you will regret it. This will give the shape of a triangle.
  6. Let's play with shapes for a bit. Make programs that prints at least two of these shapes, one program per shape. It does not have to match my shapes exactly. Hint; think of printing one char at a time in a grid. That means one each line one char is printed at a time. You have to decide what to print based on the line and position in the line of the char.

Exercises for extra practice

  • Ask for a filename. Open the file and read and display the lines in a loop. However, if the line is empty, do not display it. Try on mixedlines.txt.
  • Ask for a number. Now ask for a filename - ex1_1.dat, ex1_2.dat & ex1_3.dat are good. Open the file and count how many numbers are lower than your input number. Display the result.
  • Ask for a number. Now ask for a filename - ex1_1.dat, ex1_2.dat & ex1_3.dat are good. Open the file and read the number on the lines. Compute two sums; the sum of numbers higher than your input number and the sum of numbers lower than your input number. Display the result.