Pseudocode and comments: Difference between revisions
mNo edit summary |
|||
Line 27: | Line 27: | ||
Again, lists are unnecessary to solve the exercises, but you can make 2 solutions if you want to show your list skills. | Again, lists are unnecessary to solve the exercises, but you can make 2 solutions if you want to show your list skills. | ||
<font color="#AA00FF"> | <font color="#AA00FF"> | ||
# Ask for a file name, read the numbers in the file, calculating the average. Continue to ask for file names and calculate the average until you enter nothing - no file name. Test on the files ''ex1_1.dat'', ''ex1_2.dat'' & ''ex1_3.dat''. | # 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''. | ||
# Read the file ''orphans.sp'' and find all accession numbers (and only the accession numbers), save them in another file of your choosing. Hint: an accession number might look like this AB000114.CDS.1 or like this AB000114 or like this AB000114.CDS.3. CDS means CoDing Sequence followed by a number. If the accession number contains the CDS part, consider .CDS.1 as a part of the accession number. Accession numbers differ in length for historical reasons. You can assume that the accession number comes straight after the '''>''', which is first on the line. | # Read the file ''orphans.sp'' and find all accession numbers (and only the accession numbers), save them in another file of your choosing. Hint: an accession number might look like this AB000114.CDS.1 or like this AB000114 or like this AB000114.CDS.3. CDS means CoDing Sequence followed by a number. If the accession number contains the CDS part, consider .CDS.1 as a part of the accession number. Accession numbers differ in length for historical reasons. You can assume that the accession number comes straight after the '''>''', which is first on the line. | ||
# 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. | # 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. |
Revision as of 19:03, 27 August 2025
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.
- 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.
- Read the file orphans.sp and find all accession numbers (and only the accession numbers), save them in another file of your choosing. Hint: an accession number might look like this AB000114.CDS.1 or like this AB000114 or like this AB000114.CDS.3. CDS means CoDing Sequence followed by a number. If the accession number contains the CDS part, consider .CDS.1 as a part of the accession number. Accession numbers differ in length for historical reasons. You can assume that the accession number comes straight after the >, which is first on the line.
- 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.
- 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.
- 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.