- What is the difference between a variable and a constant in pseudocode?
- A variable can be changed during the execution of a program. A constant cannot be changed during the execution of a program. Declare each one before it is used, with a meaningful name that has no spaces.
- Which data type should I use for a whole number, a decimal number or a postcode?
- Choose the data type from the data. A whole number is an integer, and a number that can have a fractional part is real. A postcode contains letters, so it is a string, not a number.
- Why does a REPEAT loop always run once but a WHILE loop may not run at all?
- A WHILE loop, written WHILE ... DO ... ENDWHILE, tests its condition at the start, so it may not run at all. A REPEAT ... UNTIL loop checks the condition at the end of the loop, so it always runs at least once. A FOR loop runs a pre-determined number of times.
- How do I use DIV and MOD to split a number of seconds into minutes and seconds?
- DIV gives the whole number part and MOD gives the remainder. For a video of 5000 seconds, DIV(5000, 60) gives the whole minutes, 83, and MOD(5000, 60) gives the seconds left over, 20. Check that 4980 plus 20 gives 5000.
- How do I find the length of a string and take out part of it in pseudocode?
- LENGTH(Fact) gives the number of characters, and it counts every character, including the spaces. SUBSTRING(Fact, 13, 7) takes 7 characters from position 13. The first character can be position zero or one, so position 12 is also accepted.
- Do I write CALL when I use a function in pseudocode?
- No. A function call has no CALL, so write Value ← Power(A, B) and the returned value is stored. A procedure is run with CALL, so CALL is only for a procedure.
- What is an array in pseudocode and how do I declare a one-dimensional array?
- An array is a list of items of the same data type stored under a single name. A one-dimensional array is declared with its index range and its data type, for example DECLARE Prices : ARRAY[1:40] OF REAL. The declaration fixes whether the first index is zero or one.
- What is the difference between a one-dimensional and a two-dimensional array?
- A one-dimensional array is declared with one index range, such as ARRAY[1:40] OF REAL. A two-dimensional array is a table with two index ranges, rows first then columns, such as ARRAY[1:1000, 1:2] OF STRING. All elements of either array have the same data type.
- Can I use a variable as the index of an array?
- Yes. The index in square brackets can be a variable, so Runners[Index] means the element whose position is the current value of Index. The index must be a whole number inside the declared range, because an index outside the range makes the algorithm crash.
- How do I loop through every element of a two-dimensional array?
- Use nested iteration: an outer loop for the rows and an inner loop for the columns. Write one OUTPUT statement that uses both counters, OUTPUT Names[Row, Column]. The loop limits must match the declaration, and the inner loop must close before the outer one.
- Why does my array loop crash when it fills an array?
- A common error is a loop that runs one time too many. For INPUT into Readings[1:8], FOR Index ← 1 TO 9 reaches Readings[9], which does not exist, so the algorithm crashes. Correct the line to FOR Index ← 1 TO 8. Changing the start to 0 does not help, because the array has no element 0.
- How do I set every element of an array to a starting value?
- A FOR loop counts for you, so use its counter as the index: Marks[Index] ← 0. Do not update a FOR counter inside the loop. A REPEAT or WHILE loop needs its own counter initialised before the loop and updated inside it.
- Why store data in a file instead of only in a variable?
- Storing data in a file means it is stored permanently. It is not lost if the machine or program is switched off or crashes. The data can be used again later, kept as a backup, and moved to a different computer.
- Which pseudocode keyword reads a line of data from a file?
- READFILE reads a line of data from a file into a variable. To read a line, open the file FOR READ, use READFILE, then close the file. OPENFILE only opens the file, and CLOSEFILE finishes the use of it.
- How do I open a file for reading or for writing in pseudocode?
- A file must be opened before it is used and closed when the program has finished with it. OPENFILE "Scores.txt" FOR WRITE opens the file for writing, and FOR READ is for reading data from the file. The file name is written in quotes.
- How do I write a line of text to a file in pseudocode?
- Open the file FOR WRITE, use WRITEFILE, then close the file. WRITEFILE takes the file name and the data to store. An input is first stored in a variable, and the variable is then written to the file.
- Is OPENFILE the statement that retrieves data from a file?
- No. READFILE retrieves a line of data from a file into a variable. OPENFILE only opens the file, so it never puts data into a variable. Writing open for retrieves data from a file is the wrong answer.
- In what order should file statements be written when storing a name?
- Input the string into a variable first. Then open the file FOR WRITE, write the data that was input to the file, and close the file. Open first and close last, with the write between them.