Pseudocode and comments: Difference between revisions
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{| width=500 style="float:right; margin-left: 10px; margin-top: -56px;" | {| width=500 style="font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;" | ||
|Previous: [[The path and simple file reading]] | |Previous: [[The path and simple file reading]] | ||
|Next: [[ | |Next: [[String manipulation]] | ||
|} | |} | ||
== Required course material for the lesson == | == Required course material for the lesson == | ||
Powerpoint: [https://teaching.healthtech.dtu.dk/material/22116/22116_03-PseudoComments.ppt Pseudocode, comments and user interface]<br> | Powerpoint: [https://teaching.healthtech.dtu.dk/material/22116/22116_03-PseudoComments.ppt Pseudocode, comments and user interface]<br> | ||
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=79ac24c4-603c-4a6e-9343-af2701292c8c The use of pseudocode and comments] <br> | Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=79ac24c4-603c-4a6e-9343-af2701292c8c The use of pseudocode and comments] <br> | ||
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id= | Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=5e77fe45-be78-4ecf-a5f5-b1f600a24a62 Playing the guessing game], detecting lies and endings.<br> | ||
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=d5c0132e-cc4e-4e1a-bc99-af27012a777e Live Coding]<br> | Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=d5c0132e-cc4e-4e1a-bc99-af27012a777e Live Coding]<br> | ||
Blog: [https://www.joelonsoftware.com/2000/04/18/affordances-and-metaphors/ On User Interface 1], by the founder of StackExchange.<br> | Blog: [https://www.joelonsoftware.com/2000/04/18/affordances-and-metaphors/ On User Interface 1], by the founder of StackExchange.<br> | ||
Line 15: | Line 15: | ||
== Subjects covered == | == Subjects covered == | ||
'' | ''Pseudocode'', which is "pretend" code, that tries to look like a program.<br> | ||
''Comments'', which is essential for understanding, maintenance, explanation, memory and other good stuff.<br> | ''Comments'', which is essential for understanding, maintenance, explanation, memory and other good stuff.<br> | ||
''User interface'', how the program should interact with the user. | ''User interface'', how the program should interact with the user. | ||
Line 24: | Line 24: | ||
== Exercises to be handed in == | == 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.<br> | In all these exercises, start with writing pseudo code first and the program after. This is how you should do normally.<br> | ||
The pseudo code is part of the hand-in. | The pseudo code is part of the hand-in.<br> | ||
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 in the given file until you enter nothing - no file name. Test on the files ''ex1_1.dat'', ''ex1_2.dat'' & ''ex1_3.dat''. | ||
# 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. | |||
# 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. | |||
# 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''. | |||
# 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. | |||
</font> | </font> | ||
== Exercises for extra practice == | == 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''. |
Latest revision as of 23:50, 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.
- 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.
- 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.
- 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.
- 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.