Académique Documents
Professionnel Documents
Culture Documents
1.- A for loop running through a 2-D array of values. Explain the output of the following
loop. Is the ii loop’s control variable running row- or column-wise?
2.- Loops, accumulators and series. Write a program to compute the sum of
the multiples of seven in the range [7,777].
% documentation
clc, clear
x=[7:7:777];
s=0;
for ii=1:1:length(x)
s = s+x(ii);
end
3.- Nested Loops. How many times will this program print "Hola Mundo"?
for a=10:10:50
for b=0:0.1:1
disp('Hola Mundo')
end
end
4.- A Tricky Loop. What sequence of numbers will the following for loop print?
n = 10;
for j = 1:n
n = n-1;
j
end
5.- Loop Indices. How many times will this program print "Bonjour Monde"?
for a=-1:1:-50
disp('Bonjour Monde')
end
6.- Loop Indices. How many times will this program print "Guten Tag Welt"?
for a=-1:-1:-50
disp('Guten Tag Welt')
end
7.- Loop Indices. How many times will this program print "Hello There World"?
for a=0:1:50
disp('Hello There World')
end
8.- Nested Loops Indices. What is the values of the loops parameters (i.e., a, b, c, d, e,
f, and g) to print
1 for j=a:b:c
12 for i=d:e:f
123 fprintf(‘%d’,g);
1234 end
12345 fprintf(‘\n’);
end
ANSWER
3
for j=1:1:5
for
i=1:1:j
fprintf(‘%d’,i)
; end
end
9.- Nested Loop Indices. What is the values of the loops parameters (i.e, a, b, c, d, e, f,
and g) to print
ANSWER is 5
for j=5:-1:1
for i=j:-1:1
fprintf(‘%d’,i);
end
fprintf(‘\n’);
end
10.- Nested Loops Indices. What is the values of the loops parameters (i.e, a, b, c, d, e,
f, and g) to print
ANSWER
2
for j=5:-1:1
for i=1:1:j
fprintf(‘%d’,j);
end
fprintf(‘\n’);
end