1 - 2 Memo - PRG100S - MainTest - Nov2022 - 2
1 - 2 Memo - PRG100S - MainTest - Nov2022 - 2
2.1. Increment
2.2. do while
2.3. altered / changed
2.4. Validation
2.5. Loops
2.6. index.
2.7. group
2.8. flag
2.9. constants
2.10. subscript
3.2. What are the three steps that should occur in every loop?
1. The loop control variable is initialized before entering the loop.
2. The loop control variable is tested, and, if the result is true, the loop body is
entered.
3. The loop control variable is altered within the body of the loop so that the while
expression eventually evaluates as false.
3.3. When are you required to use a for statement, and what are the advantages
of for statements?
You never are required to use a for statement for any loop. A while loop can
always be used instead of a for loop, but when a loop’s execution is based on a
loop control variable progressing from a known starting value to a known ending
value in equal steps, the for loop provides a convenient shorthand. It is easy for
others to read, and because the loop control variable’s initialization, testing, and
alteration are all performed in one location, you are less likely to leave out one of
these crucial elements.
3.6. What happens when a beginning programmer forgets that array subscripts
start with 0?
A common error by beginning programmers is to forget that array subscripts start
with 0. If you assume that an array’s first subscript is 1, you will always be “off by
one” in your array manipulation.
3.10. What are the steps to verify that an item number exists in an array? Provide
an example.
Set a flag variable named foundIt to "N". A flag is a variable that is set to
indicate whether some event has occurred. In this example, N indicates that
the item number has not yet been found in the list.
A subscript, sub, is set to 0. This subscript will be used to access each
element in the array.
A loop executes, varying sub from 0 through one less than the size of the
array. Within the loop, the customer’s ordered item number is compared to
each item number in the array. If the customer ordered item matches any
item in the array, the flag variable is assigned "Y". After all six valid item
numbers have been compared to the ordered item, if the customer item
matches none of them, then the flag variable foundIt will still hold the
value "N".
If the flag variable’s value is "Y" after the entire list has been searched, it
means that the item is valid and an appropriate message is displayed, but if
the flag has not been assigned "Y", the item was not found in the array of
valid items. In this case, an error message is output and 1 is added to a
count of bad item numbers.
SECTION B 40 Marks
QUESTION 4 [10]
QUESTION 5 [10]
5.1. for (var i=1; i <= row; i++) { //outer loop to process the rows 3
5.2. for(var j=1; j <= col; j++){ // inner loop to process the columns 3
5.3. if(value < 10) //check if value is less than 10 2
5.4. text += value + 1; //increment value by 1 2
QUESTION 6 [20]