0% found this document useful (0 votes)
26 views

2D Array and Insertion Sort

A computer program is needed to store jobs in order of priority. Each job has a job number and priority level. The program uses a 2D array to store the jobs and a variable to track the number of jobs. Code is provided to declare the array and variable, add jobs to the array, sort the array, print the array contents, and a main program to test it.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

2D Array and Insertion Sort

A computer program is needed to store jobs in order of priority. Each job has a job number and priority level. The program uses a 2D array to store the jobs and a variable to track the number of jobs. Code is provided to declare the array and variable, add jobs to the array, sort the array, print the array contents, and a main program to test it.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2

Open the evidence document, evidence.doc

Make sure that your name, centre number and candidate number will appear on every page of this
document. This document must contain your answers to each question.

Save this evidence document in your work area as:

evidence_ followed by your centre number_candidate number, for example: evidence_zz999_9999

A class declaration can be used to declare a record.

If the programming language does not support arrays, a list can be used

instead. A source file is used to answer Question 2. The file is called

Characters.txt

1 A computer program is needed to store jobs in order of priority. Each job has a job number (for
example, 123) and a priority from 1 to 10, with 1 being the highest priority and 10 the lowest.

The program stores the jobs in a global 2D array.

The pseudocode declaration for the array is:

DECLARE Jobs : ARRAY[0:99, 0:1] OF INTEGER

For example:

• Jobs[0, 0] stores the job number of the first job.


• Jobs[0, 1] stores the priority of the first job.

The global variable, NumberOfJobs, stores the number of jobs currently in the array.

(a) Write program code to declare the global 2D array Jobs and the global variable
NumberOfJobs.

Save your program as Question1_N22.

Copy and paste the program code into part 1(a) in the evidence document.
[3]

(b) The procedure Initialise() stores –1 in each of the array elements and assigns 0 to
NumberOfJobs.

Write program code for the procedure Initialise().

Save your program.


Copy and paste the program code into part 1(b) in the evidence document.
[3]
3

(c) The procedure AddJob():

• takes a job number and priority as parameters


• stores the job in the next free array element
• outputs ‘Added’ if the job was successfully stored in the array
• outputs ‘Not added’ if the job was not successfully stored in the array.

Write program code for the procedure AddJob().

Save your program.

Copy and paste the program code into part 1(c) in the evidence document.
[5]

(d) The main program should call the procedure Initialise() and then use the AddJob()
procedure to store the following jobs in the order given:
Job number Priority

12 10

526 9

33 8

12 9

78 1

Write program code for the main program and perform the tasks described. Save

your program.

Copy and paste the program code into part 1(d) in the evidence document.
[2]

(e) When a new job has been added, the array is sorted into ascending numerical order of priority
using an insertion sort.

Write program code for the procedure InsertionSort() to sort the data into ascending numerical
order of priority.
Save your program.

Copy and paste the program code into part 1(e) in the evidence document.
[5]
4

(f) The procedure PrintArray() outputs each job number and priority on a line, for example:

123 priority 1

39 priority 3

120 priority 7

Write program code for the procedure PrintArray().

Save your program.

Copy and paste the program code into part 1(f) in the evidence document.
[3] (g) The main program needs to sort the array and then output the contents of the array.

(i) Amend the main program by writing program code to call procedures InsertionSort() and
PrintArray().

Save your program.

Copy and paste the program code into part 1(g)(i) in the evidence document. [1]

(ii) Test your program.

Take a screenshot of the output.

Copy and paste the screenshot into part 1(g)(ii) in the evidence document.

[1]

You might also like