You are on page 1of 7

Cody Anderson

ECE105

ECE105 Homework 6
Sections: if/else mistakes; Even or Odd; if/else by hand; while loops by hand
Submission type:
• sections 1-2: pdfs uploaded to Canvas
• sections 3-4: work by hand, take photos/scans of finished work, upload to Canvas
Points: 20

Section 1: if/else mistakes


Complete the following problem in a Microsoft Word document. Save and upload as a pdf.
The following statements are intended to display a student’s final letter grade after averaging
all of the semester’s scores and storing the results into the variable named AVG. There are
multiple mistakes. Answer the questions following the commands.
if AVG < 60
disp(‘Grade is an F’)
elseif AVG>=60 && AVG<70
disp(Grade is a D)
elseif AVG>70 && AVG<80
disp(‘Grade is a C’)
elseif AVG>=80
disp(‘Grade is a B’)
else AVG>=90
disp(‘Grade is an A’)
end
Questions:
1. If AVG equals 58, what will appear in the command window? Would any if statement
(conditional) be evaluated after the first one?
2. If AVG equals 63, an error will occur. What is the error? How can it be fixed?
3. If AVG equals 70, what will appear in the command window? How can this error be fixed?
4. If AVG equals 93, what will appear in the command window? How can this error be fixed?
5. The statement “AVG>=90” follows an else command. Does “AVG>=90” accomplish anything
useful?
6. For this situation, a statement like “AVG>=60 && AVG<70” could be written as just
“AVG<70” and still provide the same results. Explain why the “AVG>=60” is unnecessary.
7. In Word, rewrite this entire if/else statement to work properly and efficiently.

1
Cody Anderson
ECE105

Section 2: Even or Odd


The function mod returns the remainder of the first input argument divided by the second input
argument. Some examples:

• mod(9,2)  1
• mod(8,2)  0
• mod(8,3)  2
• mod(5.5,1)  0.5
Write a MATLAB script that requests a numeric input from the user (input function) and then
uses an if/else branching statement to write a sentence that states whether the number is
even, odd, or not an integer.
A sample display of successful operation for all three types of outputs is shown below.

Publish the completed script to pdf. It will give an error during publishing due to the input
function—do not worry about this.

2
Cody Anderson
ECE105

Section 3: if/else by hand


Two separate if/else blocks of code are given to you. For each, complete the table to show
what the output would be for each set of inputs.

a b c
7 9
5 0
-2 1
2 0
3 1

x y z
3 7
3 0
9 2
5 2
5 3

3
Cody Anderson
ECE105

Section 3: while loops by hand


For each of the following example codes, create a table to keep track of each variable throughout the processing of the loop. Then
type the code into a script, run it, and record what appears in the Command Window. The first example is completed for you.

Code Table Output to Command Window


%% Example A Step # a a=
a = 100; 0 100 50
while a > 10 1 50 25
a = a/2 2 25 12.5
end 3 12.5 6.25
a 4 6.25 6.25
X

%% Example B
b = 56;
while rem(b,2)==0
b = b/2
end

4
Cody Anderson
ECE105

Code Table Output to Command Window


%% Example C
c = 5;
while c > 0
c = c + 1
end
% you may need to type
ctrl+c

%% Example D
a = 1;
b = 7;
while a~=b
b = b-1
end

5
Cody Anderson
ECE105

Code Table Output to Command Window


%% Example E
a = 1;
b = 11;
while a~=b
b = b-1
a = a*2
end

%% Example F
a = 1;
b = 12;
while a~=b
b = b-1
a = a*2
end
% you may need to type
ctrl+c

6
Cody Anderson
ECE105

Code Table Output to Command Window


%% Example G
A = ones(1,5);
ind = 2;
while ind<=length(A)
A(ind) = ind^3;
ind = ind + 1;
end
A

%% Example H
B = ones(1,5);
ind = 2;
while ind<=length(B)
B(ind) = ind^3;
ind = ind + 2;
end
B

You might also like