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

P4 Linearsearch

Uploaded by

Nansu Baniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

P4 Linearsearch

Uploaded by

Nansu Baniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6

2 A program stores the following ten integers in a 1D array with the identifier arrayData.

10 5 6 7 1 12 13 15 21 8

(a) Write program code for a new program to:

• declare the global 1D array, arrayData, with ten elements


• initialise arrayData in the main program using the data values shown.

Save your program as question2.

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

(b) (i) A function, linearSearch(), takes an integer as a parameter and performs a linear
search on arrayData to find the parameter value. It returns True if it was found and
False if it was not found.

Write program code for the function linearSearch().

Save your program.

Copy and paste the program code into part 2(b)(i) in the evidence document.
[6]

(ii) Edit the main program to:

• allow the user to input an integer value


• pass the value to linearSearch() as the parameter
• output an appropriate message to tell the user whether the search value was found
or not.

Save your program.

Copy and paste the program code into part 2(b)(ii) in the evidence document.
[4]

(iii) Test your program with one value that is in the array and one value that is not in the
array.

Take a screenshot to show the result of each test.

Save your program.

Copy and paste the screenshots into part 2(b)(iii) in the evidence document.
[2]

© UCLES 2021 9618/43/M/J/21


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

One source file is used to answer Question 1 and two source files are used to answer Question 3.
The files are called Data.txt, AnimalData.txt and ColourData.txt

A class declaration can be used to declare a record.

A list is an alternative to an array.

1 A program reads data from a file and searches for specific data.

(a) The main program needs to read 25 integer data items from the text file Data.txt into a
local 1D array, DataArray

(i) Write program code to declare the local array DataArray

Save your program as Question1_J2023.

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

(ii) Amend the main program to read the contents of Data.txt into DataArray

Save your program.

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

(b) (i) The procedure PrintArray() takes an integer array as a parameter and outputs the
contents of the array in the order they are stored.

The items are printed on the same line, for example:

10 4 5 13 25

Write program code for the procedure PrintArray()

Save your program.

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

© UCLES 2023 9618/41/M/J/23


3

(ii) Amend the main program to output the contents of DataArray using the procedure
PrintArray()

Save your program.

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

(iii) Test your program.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 1(b)(iii) in the evidence document.
[1]

(c) The function LinearSearch():

• takes an integer array and integer search value as parameters


• counts and returns the number of times the search value is found in the array.

Write program code for the function LinearSearch()

Save your program.

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

(d) (i) Amend the main program to:

• prompt the user to input a whole number between 0 and 100 inclusive
• read and validate the input from the user
• call LinearSearch() with DataArray and the validated input value
• output the result in the format:
The number 7 is found 2 times.

Save your program.

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

(ii) Test your program by inputting the number 12.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]

© UCLES 2023 9618/41/M/J/23 [Turn over

You might also like