Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Primary Examination, Semester 2, 2021

Programming (MATLAB & C)

ENG 1002, 1002UAC

Programming Structures: All Code Questions in this Section must be answered in MATLAB

Question 1

Variables and Operators

Write MATLAB code that initialises a variable t, which describes the number of seconds an object has been falling towards Earth to 10 seconds, and then calculates the distance the object has fallen in that time, storing this value in a variable d. The distance travelled can be calculated by the formula: d = 1/2 × 9.81 × t2 .  Your program must round the value of d up to the nearest whole number.

[5 marks]

[Total for Question 1: 5 marks]

Question 2

Libraries

List one MATLAB library function you have used in your code this semester and describe what it does.

[2 marks]

[Total for Question 2: 2 marks]

Question 3

Iteration

(a) Consider the following MATLAB code:

i=0;

while  i<  -20

disp(i);

i=i-3;

end

disp(’done’);

 

i. What is the output produced by this code?  Briefly explain why this output is produced.

[2 marks]

ii. Re-write this code, using a for loop, to print out the sequence: 0  -3  -6  -9  -12  -15  -18

for full marks, there should be only a single line of code within the loop.

[3 marks]

(b) Write a MATLAB script, using either a single or nested for-loop that will print

the largest divisor of each of the integers between 2 and 100 (inclusive). The largest divisor of a number n is the largest number, less than n that divides evenly into n.  For example, the largest divisor of 42 is 21 and the largest divisor of 43 is 1.

[6 marks]

[Total for Question 3: 11 marks]

Question 4

Selection

(a) Given the if-condition: if ∼ (A l B) && (A l C)

which of the following will evaluate to true?  Circle all of the cases that will evaluate to true (you must select all that evaluate to true and none that evaluate to false to receive full credit for this question)

i. A = true, B = true, C = true

ii. A = false, B = false, C = true

iii. A = true, B = false, C = true

iv. A = true, B = true, C = false

[4 marks]

(b) Encode the following logic in MATLAB: if the temperature is less than 3422

C the metal tungsten will be solid, if the temperature is greater or equal to 5930 C then tungsten is boiling, otherwise it is liquid.

Write a MATLAB if-statement that encodes the logic. Display solid”, ”boil- ing” and liquid” respectively, in each if statement.

[4 marks]

[Total for Question 4: 8 marks]

Question 5

Input and Output

Write MATLAB code to read in a name and a course mark and then print the mark given. For example, if the user enters Brad and 84.0, the code should print:       Brad  you have  scored  84 .0%  in  your  course .

(Hint, be careful to remember the difference between reading in a name and a number in MATLAB)

If you do not remember how to print the percent sign (%) you can receive credit for that part by explaining how you could nd this information from the MATLAB command window.

[4 marks]

[Total for Question 5: 4 marks]

Question 6

Functions

(a) Write a MATLAB function that takes three numbers and returns the two largest numbers (order is not important).

[7 marks]

(b) Write a MATLAB statement that calls the function written above and stores

the returned values.

[3 marks]

[Total for Question 6: 10 marks]

Question 7

Code Style

The code below contains several program style problems.  Make this code as readable as possible by applying your programming style knowledge.

s  =  input(‘numbers?:  ’);

numbers  =  [];

for  i  =  1:s

v  =  input(‘number:  ’);

n  =  [n,  v];

end

[5 marks]

[Total for Question 7: 5 marks]

Question 8

Data Structures - arrays, matrices and strings

(a) Write a MATLAB script that will copy all values on the outside of a matrix,

M, to a new vector N. Thus, given a matrix M:

1  2  3  4

5  6  7  8

9  0  1  2

your script should make a vector N containing:

1  2  3  4  5  8  9  0  1  2

[9 marks]

(b) Write a MATLAB script that reads in two strings from the user and creates a new string where each character is the alphabetically greater (later in the alphabet) of the corresponding characters from both strings.

For example, if the user typed in car’ and bat’, the script would create a new string with the characters: cat as c is after b, a and a are equal, and t comes after r. For this question, you can assume the two strings entered are of the same length.

[7 marks]

[Total for Question 8: 16 marks]

Question 9

Problem Solving, Design, Implementation and Testing in MATLAB

(a) Write a function interleave that accepts two vectors A and B and returns a

new vector that contains the elements of A and B interleaved. For example with A=[1  3  5] and B=[2  4  6] the result will be  [1  2  3  4  5  6].  In your answer you must use a loop to perform this operation rather than shortcuts. In your answer you can also assume that A and B are of equal length.

[6 marks]

(b) For the interleave function above, give three test cases that will test a range

of boundary cases and equivalence classes. For each test case explain what types of inputs that case is testing and give the expected output.

[3 marks]

[Total for Question 9: 9 marks]

All Questions in this Section must be answered in C

Question 10

Fundamental C programming structures and Defensive Programming in C -

(a) What will be printed when the following code runs? Explain your answer.

1   i n t   increment ( i n t   counter )   {

2            counter++;

3            return   counter ;

4   }

5

6   i n t   main ( void )   {

7            i n t   counter  =  4; 8

9            while   ( counter  <  6  &&  ( counter  =  counter +1))  {

10                     i n t   counter  =  increment ( counter ) ;

11                     p r i n t f ( ”%d/n” ,   counter ) ;

12            }

13            p r i n t f ( ”%d/n” ,   counter ) ; 14   }

[4 marks]

(b) Other than syntax differences and keywords, explain how C switch state-

ments differ from MATLAB switch statements.

[4 marks]

(c) The code listings below include unsafe coding practices. Modify the code to implement defensive programming practices.

i. The code below is intended to read a name of up to 10 characters. There are two unsafe practices in this part: Identify the problems and correct them.

char name[10];

scanf("%s", name);

[4 marks]

ii. There is one unsafe practice in this part: Identify the problem and cor- rect it.

int  values_size  =  5;

int  *  values  = malloc(values_size  *  sizeof(int));

for  (int  i=0;  i<values_size;  i++)  {

values[i]  =  i;

}

[2 marks]

[Total for Question 10: 14 marks]

Question 11

Memory & Data Representation in C

(a) Explain, with the aid of an example, why it might be benecial to use char-

acters as numbers in C.

[2 marks]

(b) Write a function revconcatenate that takes two strings as arguments and returns a new string that is the concatenation of the two strings with the second string reversed.  For example, if the strings hello” and universe”

were passed as arguments the function would return the string:

“helloesrevinu”

For this question, you cannot use the strcat or strncat library functions but you may use other string functions.

[16 marks]

(c) What will be printed by the program below? Explain the behaviour using a drawing of the memory used by the program variables.

void  increment(int  *value)  {

(*value)++;

}

int main(void)  {

int num1  =  6;

increment(&num1);

printf("num1  %d\n", num1);

return  0;

}

[4 marks]

Write a C program that stores student names.  The program must rst ask the user for an int, which is how many names they will store, and then prompt the user for the names storing each of the names as they are entered. For full credit, you must use the minimum amount of memory necessary to store the names throughout the execution of the program. For this question, you may restrict the size of names to be no more than 10 characters and can assume that the names do not contain whitespace.

[14 marks]

[Total for Question 11: 36 marks]