Sunteți pe pagina 1din 18

Assignment 05

“Introductory Progamming
in Matlab”
Group : B01
Members :
- Arya Nur Dewangga 03411640000034
- Panji Indra Wadharta 03411640000037
- Refsi Pratiwi 03411640000045
- Aceha Jazaul Aufa 03411640000057

Concept
1. What is fprintf function?
a. Function that provides additional control over the display of
information
b. Function that provides a handy way to display a value
c. Types of loops
d. Function that ends after a specified number of repetitions
e. Function that ends on the basis of a logical condition
Answer :A
Solutions : Fprintf function is function that provides additional control over
the display of information. A simple representation of its syntax is
fprintf('format', x, ...)

2. Which statement is true about the function below?


printf(fileID,formatSpec,A1,...,An)
a. Applies the formatspec to all elemnts of arrays A1,...An in colomn
order
b. Write the data to a text file
c. Returns the number of bytes that fprintf writes
d. Formats data
e. issuing plot(_,Name,Value)
Answer : A,B
Solutions : printf(fileID,formatSpec,A1,...,An) applies
the formatSpec to all elements of arrays A1,...An in column order, and
writes the data to a text file. fprintf uses the encoding scheme specified in
the call to fopen.

3. To save the results of some computation to a file in a text format requires


the following steps:
a. Open a file using fopen, save with .txt, close the file using fclose
b. Open a file using fopen, write the file using editor, close the file using
fclose
c. Open a file using fopen, write the output using fprintf, close the file
using fclose
d. Open a file using fopen, write the output using editor, close the file
using fclose
e. Open a file using fopen, write the output using editor, save the file,
close the file using fclose
Answer :C
Solution : To save the results of some computation to a file in a
text format requires the following steps: 1. Open a file using fopen 2.
Write the output using fprintf 3. Close the file using fclose.

4. Simple form of the if statement is....


a. If expression, end
b. If expression, value, end
c. If expression
d. If expression, condition, end
e. If expression, statements, end
Answer :E
Solution : The simplest form of the if statement is
if expression
statements
end

5. What is meant by while 1?


a. Loop runs finitely
b. Loop runs infintely
c. The condition always be true
d. The condition always be false
e. The opposite of while 2
Answer : B, C
Solution : while 1 is an infinite loop which will run till a break
statement is issued explicitly. It means, the condition will always be true.
Interestingly not while(1) but any integer which is non-zero will give the
similar effect as while(1). Therefore, while(1), while(2) or while(-255), all
will give infinite loop only.

6. What is the syntax of while loop?


a. While expression, statements, end
b. While condition, expression, statement, end
c. While condition, statement, end
d. While condition, expression, end
e. While statement, condition, end
Answer :A
Solution :
while expression
statements
end
7. What’s the meaning of the syntax on the while loop?
a. Evaluates a condition, and repeats the execution of a group of
expression in a loop while the condition is true
b. Evaluates an expression, and repeats the execution of a group of
statement in a loop while the expression is true
c. Evaluates a statement, and repeats the execution of a group of
expression in a loop while the condition is true
d. Evaluates a condition, and repeats the execution of a group of
statement in a loop while the condition is true
e. Evaluates a condition, and repeats the execution of a group of
expression in a loop while the statement is true
Answer :B
Solution : while expression, statements, end evaluates
an expression, and repeats the execution of a group of statements in a
loop while the expression is true.

8. When the expression of while loop is true?


a. When the condition is match
b. When its result is nonempty and contains only nonzero elements
c. When the statement is match
d. When its result is contain the logical and numeric
e. When the expression is match
Answer :B
Solution : An expression of while loop is true when its result is
nonempty and contains only nonzero elements (logical or real numeric).
Otherwise, the expression is false.

9. What’s the syntax of for loop?


a. For values, statement, end
b. For values = expression, statement, end
c. For values = index, statements, end
d. For index = values, statements, end
e. For statements = index, values, statement, end
Answer :D
Solution :

10. What’s the meaning of the syntax on the for loop?


a. Executes a group of values in a loop for a specified number of
times.
b. Executes a group of a expression in a loop for a specified number
of values.
c. Execute a group of a statements in a loop for a specified number of
values.
d. Execute a group of a statements in a loop for a specified number of
index.
e. Execute a group of a statements in a loop for a specified number of
times.
Answer :E
Solution :

11. What is num2str?


a. function that converts a numeric array into a character array
b. function that converts a numeric array into a file
c. function that converts a numeric array into a matrix
d. function that converts a numeric array into a m-files
e. function that converts a numeric array into a character array
Answer : A,E
Solution : the s = num2str(A) converts a numeric array into a
character array that represents the numbers. The output format depends
on the magnitudes of the original values. num2str is useful for labeling
and titling plots with numeric values.

12. What’s the meaning of format codes (%d) that employed with the
fprintf function?
a. The decimal format
b. The integer format
c. The scientific format with lowercase e
d. The scientific format with uppercase E
e. The more compact of %e or %f
Answer :B
Solution :

13. What is the meaning of control codes (\n) that employed with the
fprintf function?
a. The decimal format
b. The integer format
c. The scientific format with lowercase e
d. Start new line
e. The more compact of %e or %f
Answer :D
Solution :

14. What is the used of diary filename in Matlab?


a. To disable logging
b. To enables logging
c. To enables logging using the current diary file name
d. To toggles logging on and off
e. To saves the resulting log to filename.
Answer :E
Solution : diary filename saves the resulting log to filename. If the
file already exists, MATLAB appends the text to the end of the file. To see
the name of the current diary file, type get(0,'DiaryFile').

15. What is the meaning of fclose(‘all’) function?


a. To closes all open files
b. To closes an open files
c. To return a status of 0 when the close operation is succesful
d. To return the status of ‘all’ when the close operation is succesful
e. Meshgrid()
Answer :A
Solution : fclose('all') closes all open files.
Calculation/Computation
Hint: in this season, just follow the options that given then operate it in Matlab to get the right
illustration.

16. Suppose that the variable ‘velocity’ has a value of 5.6175. to display
the value using eight digits with four digits to the right of the decimal
point along with a amessage, the statement along with the resulting
output would be.........
a. >> fprintf(‘The velocity is “8.4f m/s/n\’, velocity)
The velocity is 50.6175 m/s
b. >> fprintf (‘The velocity is %8.4f% m/s\n’, velocity)
The velocity is 50.6175 m/s
c. >> fprintf (‘The velocity is “8.4f m/s\n”, velocity)
The velocity is 50.6175 m/s
d. >> fprintf (‘The velocity is %8.4f m/s\n’, velocity)
The velocity is 50.6175 m/s
e. >> fprintf (‘The velocity is %8.4f m/s\n, velocity)
The velocity is 50.6175 m/s

Answer :D
Solution :

17. The fprintf function can also be used to display several values per
line with different formats. What will appear in the command
window when typing the command below?
a. 100 6.283 3.14159e+000
b. 100 6.283 3.14159000
c. 100 2.283 3.14159e+000
d. 100 6.283 3.14159e^+000
e. 100 6.283 3.14159e^(+000)

Answer :A
Solution :

Answer :A
Solution :

18. Fprintf can also be used to display vectors and matrices. Which the
right set between the M-file and the result?
a. M-file:
function fprintfdemo
x = [1 2 3 4 5];
y = [20.4 12.6 17.8 88.7 120.4];
z = [x;y];
fprintf(' x y\n');
fprintf('%5d %10.3f\n',z);
The result of running this M-file:
>> fprintfdemo
x y
1 20.400
2 2 12.600
3 3 17.800
4 4 88.700
5 5 120.400
b. M-file:
function fprintfdemo
x = [1 2 3 4 5];
y = [20.4 12.6 17.8 88.7 120.4];
z = [x;y];
fprintf(' x y\n');
fprintf('%5d %10.3f\n',z);

The result:
>> fprintfdemo
x y
1 20.400
2 2 12.600
3 3 17.800
4 4 88.700
5 5 120.400
c. M-file:
function fprintfdemo
x = [1 2 3 4 5];
y = [20.4 12.6 17.8 88.7 120.4];
z = [x;y];
fprintf(' x y n');
fprintf('%5d %10.3f\n,z);
The result:
>> fprintfdemo
x y
1 20.400
2 12.600
3 17.800
4 88.700
5 120.400
d. M-file:
function fprintfdemo
x = [1 2 3 4 5];
y = [20.4 12.6 17.8 88.7 120.4];
z = [x;y];
fprintf(' x y n');
fprintf('5d , 10.3f\n,z);
The result:
>> fprintfdemo
x y
1 20.400
2 12.600
3 17.800
4 88.700
5 120.400
e. M-file:
function fprintfdemo
x = [1 2 3 4 5];
y = [20.4 12.6 17.8 88.7 120.4];
z = [x;y];
fprintf(' x y n');
fprintf('5d , 10.3f\n,z);
The result:
>> fprintfdemo
x y
6 20.400
7 12.600
8 17.800
9 88.700
10 120.400

Answer :A
19. Execute the command below, and choose the answer
for j = 1:5
disp(j)
end

a. 1, 2, 3, 4, 5
b. 5, 4, 3, 2, 1
c. 1, 3, 5
d. 2, 3, 4, 5
e. 0, 1, 2 , 3, 4, 5
Answer :A
Solution :

20. What command to execute to get the display of classic countdown


sequence: 10,9,8,7,6,5,4,3,2,1 ?
a. for j = 1:1:10
disp(y)
end
b. for j = 1:-1:10
disp(-j)
end
c. for j = 1:1:10
disp(-j)
end
d. for j = 10:-1:1
disp(-j)
end
e. for j = 10:-1:1
disp(j)
end
Answer :E
Solution :

21. Develop an M-file to compute the factorial below:


0! = 1
1! = 1
2! = 1 x 2 = 2
3! = 1 x 2 x 3 = 6
4! = 1x2x3x4 = 24
5! = 1x2x3x4x5 = 120

a. Function fout = factor(n)


%factor(n)
%computes the product of all the integers from 1 to n
X=1
For 1 =1:n
Fout = X
End

b. Function fout=factor(n)
%factor(n)
%computes the product of all the integers from 1 to n
X=1
For 1 =1:n
end
Fout = X
End

c. Function fout=factor(n)
%factor(n)
%computes the product of all the integers from 1 to n
X=1
For 1 =1:n
Fout = X
End
end
d. Function fout=factor(n)
%factor(n)
%computes the product of all the integers from 1 to n
X=1
For 1 =1:n
Fout = X
end
e. %factor(n)
%computes the product of all the integers from 1 to n
X=1
For 1 =1:n
Fout = X
end
Answer :B
Solution :

>> factor(5)
ans =
120

22. The statement between while and the end are repeated as long as the
condition is true. A simple example is:
x= 8
while x > 0
x = x -3
disp(x)
end
When this code is run, the result is....
a. -1, 2, 5, 8
b. -2, -1, 2, 5, 8
c. -2, -1, 0 , 1, 2, 3, 4, 5, 6, 7, 8
d. -2, 2, -1, 1, -5, 5, -8, 8
e. -1, 2, 5, 7
Answer :A
Solution :
23. Sum of sequence of random numbers until the next random number is
greater than an upper limit. Then, exit the loop using a break statement.
Which code is correct?

a. limit = 0.8; b. limit = 0.8;


s = 0; s = 0;

while 1 while 1
tmp = rand; if tmp > limit
if tmp > limit break
break end
end s = s + tmp;
s = s + tmp; end
end
c. limit = 0.8; d. limit = 0.8;
s = 0; s = 0;

while tmp > limit while tmp > limit


if s = s + tmp; if s = s + tmp;
break break
end end
end 1
end
e. limit = 0.8;
s = 0;

while s = s + tmp;
if tmp > limit
break
end
tmp = rand;
end
Answer :A
Solution :

24. Convert the floating-point values returned by pi and eps to character


vectors.
a. s = %3.1416
b. s = 3.1461
c. s = 3.146.1
d. s = ‘3.1416’
e. s = (3.1416)
Answer :D
Solution :
25. Create a prompt to request the name of a file to open.
a. >> op = fprintf(‘weekdays.m’)
b. >> op = fopen(‘weekdays.m’,’wt’)
c. >> op = fopen(weekdays.txt’,’wt’)
d. >> op = fopen(‘weekdays.text’,’wt’)
e. >> op = fprint(op, ‘weekdays\n’);
Answer :C
Solution :

S-ar putea să vă placă și