c和matlab代写-VERSION 00000001

void Foo(int a);

int main(void)
{
int a = 20;
Foo(a);
printf("Result = %d ", a);
return 0;
}

void Foo(int a)
{
a = 10;
printf("Result = %d ", a);
}

(a) Result = 20 Result = 10
(b) Result = 20 Result = 20
(c) Result = 10 Result = 10
(d) Result = 20 Result = 0
(e) Result = 10 Result = 20
Question 7
What output is produced by the following program?

#include

int Test(int a, int b)
{
return (a * b) % 2 == 0;
}

int main(void)
{
if (Test(10, 20)) {
printf("One ");
}
if (Test(3, 3)) {
printf("Two ");
}
if (Test(2, 3)) {
printf("Three ");
}
return 0;
}

(a) Three
(b) One Two
(c) Two Three
(d) One Three
(e) One
VERSION 00000001 ENGGEN131

Page 6 of 34

Question 8
The nested loop below is incomplete because the inner loop is missing the initialisation and the condition:

int i, j;
for (i = 0; i < 5; i++) {
for (j = ????; j < ????; j++) {
printf("(%d %d) ", i, j);
}
printf("\n");
}

If this nested loop produces the following output:

(0 1)
(1 1) (1 2)
(2 1) (2 2) (2 3)
(3 1) (3 2) (3 3) (3 4)
(4 1) (4 2) (4 3) (4 4) (4 5)

what must be the completed initialisation and condition for the inner loop?

(a) for (j = 0; j < i+1; j++) {
(b) for (j = 1; j < 5-i; j++) {
(c) for (j = i; j < 5; j++) {
(d) for (j = i+1; j < i+3; j++) {
(e) for (j = 1; j < i+2; j++) {
Question 9
Consider the following function definition that is passed an array and the number of elements in the array as
inputs:

int Test(int *values, int length)
{
int i;
for (i = 0; i < length; i++) {
if (values[i] == 0) {
return i;
}
}
return -1;
}

Which of the following statements best describes the purpose of this function?

(a) It returns the index position of the first occurrence of the value 0, or -1 if no values are 0
(b) It returns true if the array contains a 0, and false otherwise
(c) It returns the value 0 if the array contains a 0, otherwise it returns -1
(d) It returns the index position of the last occurrence of the value 0, or -1 if no values are 0
(e) It returns a count of the number of elements in the array that are equal to zero
VERSION 00000001 ENGGEN131

Page 7 of 34

Question 10
Consider the following function definition that is passed an array and the number of elements in the array as
inputs:

int Test(int *values, int length)
{
int i;
int test = 0;
for (i = 1; i < length; i++) {
if (values[test] < values[i]) {
test = i;
}
}
return test;
}

Which of the following statements best describes the purpose of this function?

(a) It returns the first occurrence of any repeated value in the array
(b) It returns the index position of the smallest value in the array
(c) It returns the index position of the largest value in the array
(d) It returns the smallest value in the array
(e) It returns the largest value in the array
Question 11
Consider the definition of the ComparePattern() function below:

int ComparePattern(char *word, char *pattern)
{
int i = 0;
int result = 0;
while (word[i] != '\0') {
if (pattern[i] != '-') {
if (word[i] != pattern[i]) {
result++;
}
}
i++;
}
return result;
}

If this function is called with the following inputs, and the resulting output is printed, what is produced?

char word = "banana";
char pattern = "b-s-t-";

int result = ComparePattern(word, pattern);
printf("Result = %d", result);

(a) Result = 3
(b) Result = 5
(c) Result = 4
(d) Result = 2
(e) Result = 1
VERSION 00000001 ENGGEN131

Page 8 of 34

Question 12
Consider the definition of the UpdateString() function below:

{
int i = 0;
while (s[i] != '\0') {
if (s[i] != t) {
s[i] = s[i+1];
}
i++;
}
}

What output would be produced by the following code that calls this function?

char word = "ABBA";
printf("Result = %s", word);

(a) Result = BBB
(b) Result = BBBA
(c) Result = BBA
(d) Result = ABAA
(e) Result = ABB
Question 13
The following function takes, as input, the area of carpet you have (in square meters). The function should
return the side length (assuming a square room) of the largest room that you can cover with that amount of
carpet. The return value should be an integer. However, there is a bug (a logic error) in this implementation:

int LargestRoom(int carpetArea)
{
int side = 0;
while (side * side < carpetArea) {
side++;
}
return side - 1;
}

Even though there is a logic error in the function above, for certain inputs it still returns the expected output.
Which one of the following function calls below would return an unexpected output and therefore reveal the
bug that exists in the code?

(a) LargestRoom(8)
(b) LargestRoom(2)
(c) LargestRoom(16)
(d) LargestRoom(20)
(e) LargestRoom(10)
VERSION 00000001 ENGGEN131

Page 9 of 34

Question 14
The following function takes three integer inputs, a, b and c, and it should return the middle value. That is, the
value that would appear in the middle position if the three values were placed in numerical order. However,
there is a bug (a logic error) in this implementation:

int MiddleOfThree(int a, int b, int c)
{
if ((a <= b) && (b <= c)) {
return b;
} else if ((c <= b) && (b <= a)) {
return b;
} else if ((a <= c) && (c <= b)) {
return c;
} else {
return a;
}
}

Even though there is a logic error in the function above, for certain inputs it still returns the expected output.
Which one of the following function calls below would return an unexpected output and therefore reveal the
bug that exists in the code?

(a) MiddleOfThreeError(5, 6, 4)
(b) MiddleOfThreeError(4, 6, 5)
(c) MiddleOfThreeError(5, 4, 6)
(d) MiddleOfThreeError(6, 5, 4)
(e) MiddleOfThreeError(6, 4, 5)
VERSION 00000001 ENGGEN131

Page 10 of 34

Question 15
Consider the following function which takes two inputs: an array, and the number of elements in the array:

int Exam(int nums[], int size) {
int i = 1;
int index = 0;
while (i < size) {
if (nums[i] < nums[i - 1]) {
index = i;
}
i++;
}
return index;
}

Also consider the following FOUR arrays:

int valuesA = {4, 2, 7, 3, 6};
int valuesB = {1, 3, 5, 2, 6};
int valuesC = {6, 4, 7, 2, 3};
int valuesD = {4, 5, 2, 1, 3};

If the function is called FOUR times, once with each of these four arrays passed as input (along with the value 5
to indicate the length of the arrays), how many times would the value 3 be returned from the function as output?

(a) two times
(b) three times
(c) one time
(d) four times
(e) zero times
Question 16
The following function, ArraySum(), should compute the sum of all of the elements in the array vals
between index start and index end inclusive. It is defined recursively, however the recursive function call is
missing and has been replaced with ????.

int ArraySum(int *vals, int start, int end)
{
if (start == end) {
return vals[start];
} else {
????
}
}

Which of the following lines of code would correctly complete the recursive definition of this function?

(a) return vals[start] + ArraySum(vals, start, end-1);
(b) return vals[start] + ArraySum(vals, start+1, end);
(c) return vals[start] + ArraySum(vals, start+1, end-1);
(d) return vals[end] + ArraySum(vals, start+1, end);
(e) return vals[end] + ArraySum(vals, end, start-1);
VERSION 00000001 ENGGEN131

Page 11 of 34

Question 17
Consider the program below that generates and prints two pseudo-random numbers:

#include
#include
#include

int main(void)
{
int r1;
int r2;

srand((unsigned int)time(NULL));

r1 = (rand() % 10) + 5;
r2 = (rand() % 5) + 10;

printf("Result = %d %d", r1, r2);

return 0;
}

Only one of the following outputs could NOT have been produced by this program. Which one is not possible?

(a) Result = 5 14
(b) Result = 6 10
(c) Result = 12 11
(d) Result = 14 10
(e) Result = 15 13
Question 18
The following function takes two strings as inputs and determines if the strings are equal (i.e. if every character
in the two strings is identical). If the strings are equal, the function returns true, otherwise it returns false. Most
of the function is complete, but the return statement is missing from the code below and has been replaced by
????. What code should replace the ???? below so that the function works correctly?

int StringsEqual(char *w1, char *w2)
{
int i = 0;
while ((w1[i] == w2[i]) && (w1[i] != '\0')) {
i++;
}
????
}

(a) return w1[i] != '\0';
(b) return w1[i] == '\0';
(c) return w1[i] != w2[i];
(d) return w1[i] == w2[i];
(e) return i > 0;
VERSION 00000001 ENGGEN131

Page 12 of 34

Question 19
What output is produced by the following code?

int a = 10;
int b = 20;

int *p, *s;

p = &a;
s = p;
p = &b;

b = *s;
*s = b;

printf("Result = %d %d\n", a, b);

(a) Result = 0 0
(b) Result = 10 10
(c) Result = 10 20
(d) Result = 20 10
(e) Result = 20 20
VERSION 00000001 ENGGEN131

Page 13 of 34

Question 20
Consider the text file, called “input.txt”, which is stored on disk and contains the following 5 digits, each
separated by a single space character:

input.txt

What output would be produced by the following program that processes this text file?

#define _CRT_SECURE_NO_WARNINGS
#include

int main(void)
{
FILE *fp;
int i, x, n;

fp = fopen("input.txt", "r");
fscanf(fp, "%d", &x);

printf("Result = ");
for (i = 0; i < x; i++) {
fscanf(fp, "%d", &n);
printf("%d ", n);
}
fclose(fp);

return 0;
}

(a) Result = 4 5 6
(b) Result = 3 4 5 6
(c) Result = 2 3 4 5 6
(d) Result = 3 4
(e) Result = 3 4 5

2 3 4 5 6
VERSION 00000001 ENGGEN131

Page 14 of 34
ROUGH WORKING SPACE - WILL NOT BE MARKED

VERSION 00000001 ENGGEN131
Page 15 of 34
SECTION B

MATLAB PROGRAMMING

This booklet may be detached and must be handed in

This section consists of several long answer MATLAB Programming questions. You will be marked on both
style and functionality.

IMPORTANT: Remember to comment all your files.

There are five questions in this section and each question is worth 12 marks, with the marks for any sub parts as
indicated. Some questions may require fewer lines of writing than others. Ample answer space has been
provided in case you make errors and wish to cross out material.

All answers to SECTION B are to be written in the spaces provided on the question and answer sheet. If you
require more space than is available, you may request a blank four page script book.

Surname ……………………………………………………

Forenames ………………………………………………….

ID …………………………………………………………...

Also write your ID Number at the top of each odd-numbered page.

MARKS: Section A
Q 1 – 20 C Programming Multiple Choice 

MARKS: Section B
Q 21 - 25 MATLAB Long Answer 

Total 
VERSION 00000001 ENGGEN131
Page 16 of 34
The following is background material for questions 21, 22 and 23. Please read it carefully.

The Mandelbrot set, shown below, is a very famous fractal.

The image above was created using the Matlab script shown on the next page, which colours each
pixel according to whether or not a corresponding point from a regular grid over the complex plane is
a member of the Mandelbrot set.

For any point, c, in the complex plane we can determine if that point is within the Mandelbrot set by
investigating its behaviour when repeatedly applying the complex quadratic polynomial formula:

() = 2 +

When investigating the nature of a point, c, we start with = 0. The result of applying the formula,
(), is fed back in as the new z value and we apply this process repeatedly. If repeated iterations of
this formula result in the sequence of f values staying within the bounds of a circular region centred
on the origin, we say that the original c value is in the Mandelbrot set, and colour it black. If repeated
iterations result in the sequence of f values exiting the bounds of the circular region then we say the
point is NOT in the Mandelbrot set and colour it accordingly (with a shade of colour picked based on
the number of iterations it took for the value of f to exit the bounded region).

It is not practical to check an infinite number of iterations, so we set a cut-off value for the number of
iterations to perform.

The script shown on the next page will generate an image of the Mandelbrot set, once the following
three helper functions have been written:
• ValidColourmap (Question 21)
• MandelbrotColours (Question 22)
• IterateCompexPolynomial (Question 23)
VERSION 00000001 ENGGEN131
Page 17 of 34
% Display an image of the Mandelbrot set
% Author: Peter Bier

clear
% Determine the size of the grid
gridSize = input('Please enter a grid size:');

% Determine the cut-off value to use
cutoff = input('Please enter a cut-off value:');

% Determine the colourmap to use
validMaps = {'parula', 'jet', 'hot', 'copper'};

% Display options to select from
disp('You can choose from the following colourmaps:')
for map = 1:length(validMaps)
fprintf(1,'%s\n',validMaps{map});
end

% Keep asking the user to select a valid map until they do so
mapName = input('Please enter the colourmap name:','s');
while ~ValidColourmap(mapName,validMaps)
mapName = input('Please enter the colourmap name:','s');
end

% Set up the colour values to use
ColourValues = MandelbrotColours(mapName,cutoff);

% Create a grid of complex values
x = linspace(-2,2,gridSize);
y = linspace(2,-2,gridSize);
[X,Y] = meshgrid(x,y);
grid = X + i*Y;

r = 2; % radius of bounded region

% Determine the nature of each point in the grid (i.e. whether it is in
% the Mandelbrot set or not) and colour appropriately
for i = 1:gridSize
for j = 1:gridSize
pointNature = IterateComplexPolynomial(grid(i,j),r,cutoff);
colourValueRow = pointNature + 1;
MandelbrotImage(i,j,1:3) = ColourValues(colourValueRow,1:3);
end
end

%display fractal image
imshow(MandelbrotImage)

VERSION 00000001 ENGGEN131
Page 18 of 34
Question 21 (12 marks)
Write a Matlab function called ValidColourmap that will return true if a given map name is
contained within a list of valid names (ignoring case). If the map name is not in the list, the function
will display an “Invalid colourmap” message (including the name of the map) and return false.
Example calls

Here are some examples of calls to ValidColourmap. Note that the first input to the function is a
string containing a map name and the second input is a list of valid map names in the form of a cell
array.

>> v = ValidColourmap('jet',{'summer','spring','winter','autumn'})
Invalid colourmap jet
v =
0
>> v = ValidColourmap('winter',{'summer','spring','winter','autumn'})
v =
1
>> v = ValidColourmap('SPRING',{'summer','spring','winter','autumn'})
v =
1
>> mapName = 'spring';
>> validNames = {'parula', 'jet', 'hot', 'copper'};
>> v = ValidColourmap(mapName,validNames)
Invalid colourmap spring
v =
0

Below is the header comment for this function:

% ValidColourmap determines whether or not a given map name is valid by
% checking to see if it is contained within a list of valid map names.
% The map name is only valid if it is contained within the list of names
% (note that when checking this, case does not matter).
%
% If the map name was invalid an error message will be displayed, with the
% words “Invalid colourmap” followed by the name of the map
%
% ValidColourmap takes two input(s) in the following order:
% mapName, a string containing the name of a colourmap
% validColours, a cell array where each entry is a string of a valid
% colourmap name
%
% It returns a single output, a value of true (i.e. 1) if the map name was
% valid and a value of false (i.e. 0) if the map name was invalid.

VERSION 00000001 ENGGEN131
Page 19 of 34
Write your code for ValidColourmap.m in the space below. You do NOT need to write the header comment for this function, as it has already been written for you on the previous page.
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
VERSION 00000001 ENGGEN131
Page 20 of 34
Question 22 (12 marks)
Write a Matlab function called MandelbrotColours that sets up a 2D array of uint8 colour values
to use for colouring the Mandelbrot set. The first row corresponds to black and subsequent rows
correspond to colours from a named colour map of n rows, resulting in an output with + 1 rows and
3 columns.
Example calls

Here are some examples of calls to MandelbrotColours

% Create a 51x3 array of colours, the first row being black and subsequent
% rows corresponding to colours from a hot colourmap 50 rows in length.
Colours1 = MandelbrotColours('hot',50);
% Create a 21x3 array of colours, the first row being black and subsequent
% rows corresponding to colours from a jet colourmap 20 rows in length
Colours2 = MandelbrotColours('jet',20);
Hints
You may find the feval function useful when generating your 2D array of colourmap values.
Remember that colourmap values can be generated using the colourmap function of the corresponding
name. E.g. to generate a 20 row parula colourmap you could use: map = parula(20)

Remember that a colourmap function returns an array of doubles that range from 0 to 1 whereas the
values returned by MandelbrotColours must be uint8 values ranging from 0 to 255.

Below is the header comment for this function:

% MandelbrotColours generates a 2D array of colour values to use for
% shading a Mandelbrot set, where each row contains 3 uint8 values
% (ranging from 0 to 255) that correspond to the amount of red, green and
% blue for a particular shade of colour.
%
% MandelbrotColours takes two input(s) in the following order:
% colourmap, a string containing the name of a colourmap
% n, an integer value that corresponds to the cut-off number used when
% generating a Mandelbrot set
% It returns a single output, a 2D array of colour values, with n+1
% rows, where the first row is the colour black and the remaining rows
% correspond to shades of colour from a colourmap of n rows.
% e.g. the following call:
% colourvalues = MandelbrotColours('jet',10);
% Would result in a 11x3 array of colours.
% The first row will correspond to the colour black.
% Subsequent rows will correspond to a colour from the named 2D colourmap
% of n rows, which in this example would be generated using
% jet(10)
% E.g. the second row of the colourvalues array will correspond to the
% colour in the first row of the generated colourmap.
% The third row of the colourvalues array will correspond to the colour in
% the second row of the generated colourmap and so on.
%
% Note that when calculating uint8 colour values, any non-integer values
% will be rounded to the nearest integer using standard rounding rules
VERSION 00000001 ENGGEN131
Page 21 of 34
Write your code for MandelbrotColours.m in the space below. You do NOT need to write the header comment for this function, as it has already been written for you on the previous page.
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
VERSION 00000001 ENGGEN131
Page 22 of 34
Question 23 (12 marks)
Write a Matlab function called IterateComplexPolynomial that determines if a given point, c, is
within the Mandelbrot set by repeated iteration of the complex polynomial () = 2 + (with a
starting value of = 0). It will return 0 if () is found to still be within the radius (r) of a bounded
region, after a set number of iterations, n. Otherwise it will return the number of iterations it took for
the value of the complex polynomial to be outside the bounded region.
Example calls

Here are some examples of calls to IterateComplexPolynomial

>> p = IterateComplexPolynomial(0+i,2,10) % c is 0+i, r=2, cut-off n=10
p =
0
>> p = IterateComplexPolynomial(0.5+i,2,20) % c is 0.5+i, r=2, cut-off n=20
p =
2
>> p = IterateComplexPolynomial(0.1-i,2,30) % c is 0.5-i, r=2, cut-off n=30
p =
4

Below is the header comment for this function:

% IterateComplexPolynomial repeatedly applies the complex polynomial
% f(z)=z^2+c for a specified value of c (with a starting value of z=0).
% The value for f becomes the value for z to use in the next iteration.
% This continues until either the value of f is no longer within the bounded
% circular region defined by a radius r (i.e. |f(z)|>=r) or the maximum
% number of iterations is reached (specified by the cut-off value, n).
%
% If |f(z)|>=r we return the number of iterations it took until
% this condition was met (indicating that the initial c value is not
% in the Mandelbrot set).
% If the maximum number of iterations was reached and f is still within
% the bounded region (i.e. |f(z)|% c is a member of the Mandelbrot set).
%
% IterateComplexPolynomial takes three input(s) in the following order:
% c, the complex value to determine the nature of
% r, the radius of the bounded circular region
% (used to determine if the iterative process has diverged)
% n, a cut-off value that determines the maximum number of iterations
% to perform (this will be a positive integer value)
%
% It returns a single output, the number of iterations it took until
% |f(z)|>=r or 0 if the maximum number of iterations was reached and
% |f(z)|

VERSION 00000001 ENGGEN131
Page 23 of 34
Write your code for IterateComplexPolynomial.m in the space below. You do NOT need to write the header comment for this function, as it has already been written for you on the previous page. ………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………

VERSION 00000001 ENGGEN131
Page 24 of 34
Question 24 (12 marks)

The Sierpinski triangle is a fractal set that has the overall shape of an equilateral triangle, subdivided
into smaller equilateral triangles. Here is an example plot:

The chaos game is one of many different methods that can be used to plot the Sierpinski triangle.
Here is a brief overview of the method steps:

1) Define three vertex points, 1 = (1, 1), 2 = (2,2) and 3 = (3,3), in 2D Cartesian
coordinates to form an equilateral triangle.

2) Randomly select an initial seed point within this triangle, to act as the current position.

3) Iteratively perform the following:

a) Randomly select one of the three vertex points.
b) Calculate the half-way point between the current position and the selected vertex.
c) Update the current position to be this half-way point.
d) Record the current position’s x and y coordinates.

4) Plot the x and y coordinates of each current position found in step 3) above (note that the initial
seed point is not plotted).

The script shown on the next page will generate an image of the Sierpinski triangle, once the
TriangleCoordinates function has been written.
VERSION 00000001 ENGGEN131
Page 25 of 34
% This script produces a plot of the Sierpinski triangle
% for a specified set of vertex points and iterations, based on
% the chaos game method.
% Author: Number One Triangle Fan

clear all
clf

% get number of iterations from user
n = input('Input the number of iterations: ');

% set the x and y coordinates of each vertex point
p1 = [0, 0];
p2 = [2, 0];
p3 = [1, sqrt(3)];

% set an initial seed position with coordinates randomly selected from
% the range 0.5% this random point is guaranteed to be within the triangle
x = 0.5 + rand(); % x will be between 0.5 and 1.5
y = rand() * sqrt(3)/2; % y will be between 0 and sqrt(3)/2
initial = [x, y];

% set the x and y coordinates of the Sierpinski triangle
[x, y] = SierpinskiTriangle(p1, p2, p3, initial, n);

% plot the Sierpinski triangle
plot(x, y, 'b.')
xlabel('x')
ylabel('y')
title(['Sierpinski triangle for ' num2str(n) ' iterations'])

VERSION 00000001 ENGGEN131
Page 26 of 34
Write a Matlab function called SierpinskiTriangle that generates coordinates for a set of points
within the Sierpinski triangle, defined by three specified vertices, a provided seed point and a
specified number of points to generate.

The function will iteratively select one of the vertex points at random and use this to update the
position to be half-way between the selected vertex point and the current position. The x and y
coordinates of the updated position are stored in 1D arrays x and y. These 1D arrays will have a
length equal to n, the number of points to generate (i.e. the number of iterations to perform).

Write your code for SierpinskiTriangle.m in the space below. You SHOULD write a header
comment for this function, as one has not been written for you.
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
VERSION 00000001 ENGGEN131
Page 27 of 34
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
VERSION 00000001 ENGGEN131
Page 28 of 34
Question 25 (12 marks)

In order to grow grapes for wine production, it is important to avoid prolonged exposure of grape
vines to temperatures below zero degrees Celsius. If the surface temperature drops below zero
degrees, it is possible to use wind turbines or even helicopters to circulate warmer air down to the
surface and prevent frost damage to the grape vines. It is therefore very important for wine producers
to understand the climate at proposed and existing grape growing sites.

The daily minimum temperature for all 365 days in 2019 has been measured at 46 new potential wine
producing sites located across New Zealand. This data is stored in a variable dailyMin2019 within a
.mat file. This variable is a 2D array containing 46 rows and 365 columns.

We are interested in finding out how many days had a daily minimum temperature below zero degrees
at each of the 46 prospective locations. A programmer has attempted to write a function, BelowZero,
to perform this calculation (shown below with line numbers to the left). However, the code contains a
number of bugs.

1 function [count] = BelowZero(temperature)
2 n = 49;
3 m = 365;
4 count = 0;
5 for i = 1:n
6 for j = 1:m
7 if temperature(n,m) == 0
8 count = count + 1
9 end
10 end
11 freezing(i) = count;
12 end
13 end

VERSION 00000001 ENGGEN131
Page 29 of 34
a) Identify four problems with the style, referring to the line numbers, and explain how to fix
them. (4 marks)
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………

VERSION 00000001 ENGGEN131
Page 30 of 34
The programmer’s BelowZero code is repeated below for your convenience.

1 function [count] = BelowZero(temperature)
2 n = 49;
3 m = 365;
4 count = 0;
5 for i = 1:n
6 for j = 1:m
7 if temperature(n,m) == 0
8 count = count + 1
9 end
10 end
11 freezing(i) = count;
12 end
13 end

VERSION 00000001 ENGGEN131
Page 31 of 34
b) Identify three bugs in the function that would prevent it from working as intended and explain
how to fix them. (6 marks)
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
VERSION 00000001 ENGGEN131
Page 32 of 34
The programmer’s BelowZero code is repeated below for your convenience.

1 function [count] = BelowZero(temperature)
2 n = 49;
3 m = 365;
4 count = 0;
5 for i = 1:n
6 for j = 1:m
7 if temperature(n,m) == 0
8 count = count + 1
9 end
10 end
11 freezing(i) = count;
12 end
13 end

VERSION 00000001 ENGGEN131
Page 33 of 34
c) Explain two ways of improving the performance of this function. (2 marks)
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………
………………………………………………………………………………………………..……………………………….……………………

VERSION 00000001 ENGGEN131
Page 34 of 34
ROUGH WORKING SPACE - WILL NOT BE MARKED

________________________________________ 