Pseudocode and comments

From 22116
Jump to navigation Jump to search
Previous: The path and simple file reading Next: String manipulation

Required course material for the lesson

Powerpoint: Pseudocode, comments and user interface
Video: The use of pseudocode and comments
Video: Playing the guessing game, detecting lies and endings.
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

Pseudocode, 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

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. This is how you should do normally.
The pseudo code is part of the hand-in.
Again, lists are unnecessary to solve the exercises, but you can make 2 solutions if you want to show your list skills.

  1. Ask for a file name, read the numbers in the file, calculating the average. Continue to ask for file names and calculate the average in the given file until you enter nothing - no file name. Test on the files ex1_1.dat, ex1_2.dat & ex1_3.dat.
  2. Ask for 2 input file names (ex1.acc & ex1.dat are good) and an output file name. Copy (read and write) the input files into the output file, one after the other.
  3. Ask for 2 input file names (ex1.acc & ex1.dat are good) and an output file name. Now copy/print the lines in the input files into the output file so that the first line of each input file are merged together with a tab and becomes the first line of the output. Continue that way with all the lines in the input file. A mental picture of what is supposed to happen is this: Imagine the 2 input files to be two pieces of paper with lines. In the exercise before the papers were put one after the other in the output. In this exercise the papers are put next to each other (sideways) in the output.
  4. The files numbers1.txt and numbers2.txt contain 100 different numbers each in sorted order. Write a program that merges these two files in numbers3.txt which will then contain all 200 numbers in sorted order. No matter what you think, this does not require lists nor extra sorting.
  5. Unfortunately, some bad numbers got mixed into numbers3.txt. In the file badnumbers.txt is a sorted list of bad numbers. If any of these numbers are in numbers3.txt they must be removed and a new file with only the good numbers goodnumbers.txt must be created. No lists, no sorting. Hint: There are 8 bad numbers in numbers3.txt.
  6. Playing time again. Make the guessing program (lesson 1, exercise 9) count how many attempts it needed to guess the number and print it when done guessing. It must be able to detect if you lie (and say so, of course). Also, if you haven't done it before, make the program guess in the fewest possible guesses (an example of 'binary search'). This is what most people naturally do by themselves when they play the game. You 'just' have to do it in the program. There is a system, a method - find it. Hint: it is probably better to start over than reuse your old program.

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.