Retest Solutions - Subprograms, Arrays, and File I/O
1.
Functions
- returns a single value of a particular data type (e.g., int, real, string, boolean)
- input parameters are never modified
Procedures
- has no return value
- it is possible to modify any number of input parameters (zero, one, or more)
2.
Initializing a variable is assigning a starting value to the variable. This is important because our programs cannot perform any operations (e.g., calculations) on a variable that is 'empty'. In Turing, an error will occur if you try to use an uninitialized variable.
3.
var grades : array 1..30 of int
for i : 1..30
grades(i) := 0
end for
4.
function min2(val1 : real, val2 : real) : real
if val1 <= val2 then
result val1
else
result val2
end if
end min2
5.
procedure swapInt ( var val1, val2 : int )
var temp : int
temp := val1
val1 := val2
val2 := temp
end swapInt
6.
An include file is an additional file with code that you wish to use in your current program/file. There are many options for the kind of information in an include file, such as definitions for records, or procedures and functions. An include file is also useful for reducing the size of the main program file by moving some code into secondary files.
7.
var total : int := 0
var count : int := 0
for i : 2005 .. 2009
for j : 1..8
total := total + grades(i)(j)
count := count + 1
end for
end for
put "average = ", total/count
8.
var file : int
var file2 : int
var value : int
open : file, "test2009.txt", get
open : file2, "prime2009.txt", put
loop
exit when eof(file)
get : file, value
if isPrime(value) then
put : file2, value
end if
end loop
Comments (0)
You don't have permission to comment on this page.