Logout

Home Topic 4 Last Next

4.3.9

Construct algorithms using loops, branching.

 

Teaching Note:

Teachers must ensure algorithms use the symbols from the approved notation sheet.

LINK Approved notation sheet.

MYP Mathematics: using flow charts to solve problems in real-life contexts, logic, algorithms

MYP Technology: design cycle (inputs, processes, outputs, feedback, iteration).

LINK Connecting computational thinking and program design.

 

Sample Questions - From former curriculum, examples of algorithm construction - so might apply from here on for the next few assessment statements:

A car-park opens from 07:30 to 18:00 each day and functions as follows:
When a car is about to enter the car-park, a ticket is issued to the driver and the barrier is raised, allowing the car to enter. When the driver wishes to leave, he/she must insert the ticket into the pay-machine and pay the amount displayed. Part of the program that controls the operation of the car park is shown below.

  public class CarPark
  {
      public static void main(String[ ] args)
      {
          String start, finish;                   // times of entry and
                                                  // departure in 24 hour
                                                  // format e.g. 07:30
                                                  
          int hours = time(start, finish);        // the function ‘time’
                                                  // returns the hours parked
                                                  
          double cost = charges(hours);           // the function ‘charges’
                                                  // returns the cost of
                                                  // parking
          output(¨Cost of Parking = $ ¨+ cost);
      }
  }

(b) If the car-park charges $3 for the first hour of parking and then $2.50 for any
additional hour, construct the method charges. [4 marks]

Recall that the string method .substring (a, b) returns part of a string, where the first character
is in position a and the last is in position (b-1).

For example, if name = "Smith", then name.substring(0,4) would return the string "Smit".

(c) If name = "South America", determine the result of
name.substring(6,13) [1 mark]

The method time returns the length of stay in hours. Parts of an hour are always rounded up, for
example, if start = “07:30” and finish = “09:35”, the method time would return the value 3.

(d) Given that the integer method Integer.parseInt(string) converts a string
into an integer, construct the method time(). [8 marks]

It is now decided to open the car-park 24 hours a day.

(e) (i) Describe a problem that might now arise with the program. [2 marks]
(ii) Suggest how the problem might be solved. [2 marks]

 


Sample Question 3:

Consider the following method:

public void multiples(int a, int y)
{
    for (int x = a; x < y; x = x+a)
    {
        System.out.println(x); //output the value of x
    }
}

(ii) Construct the method multiples using a do…while loop structure, so that
it performs the same as the original method shown at the top of this page. [4 marks]

(iii) Explain why replacing the condition "x < y" by "x ! = y" (x does not
equal y), would not necessarily produce the same result in the method
multiples. [3 marks]

 


This sample question is more analysis than construction, from the former curriculum, but might help to look at:

Consider the following method:

public void multiples(int a, int y)
{
    for (int x = a; x < y; x = x+a)
    {
        System.out.println(x); //output the value of x
    }
}

(a) (i) Explain how the code "x < y" functions in the above loop structure. [3 marks]

The above method could be rewritten using a do…while loop structure instead of the
for… loop structure. This has been partly shown below:

public void multiples(int a, int y)
{
  do
  while…
}

JSR Notes:

This will pretty well always be part of more involved questions.

But just to be clear, what is referred to here is for loops, while loops, and if/else conditional blocks.

(The term "branching" means for the flow of control of the program to "branch" one of several ways; i.e. to go a certain way based on the evaluation of an if, or an if/else, or an if/else/else if (etc.) block of code. If you are not familiar with the word, think of the branches of a tree, and how they go off in different directions.)