Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB. You are given a code (the correct one) as well as an incorrect version i

ID: 3879000 • Letter: M

Question

MATLAB. You are given a code (the correct one) as well as an incorrect version in which their task consists of  converting dollars to yens, yens to german marks then to dollars, and german marks to euros and then to dollars as well as making a conversion table for dollars = [0:1:100] to yens, german marks and euros (and you have to print your results in an external file in steps of 1 dollar from [0,10] and in steps of 10 dollar from [10,100]. The output of the correct code and the incorrect code will be shown. Please identify and comment what changes were done to the incorrect code that make the correct code run as specified.  

--------------------------------------------------------------------------------------

Correct Code

clc, clear

FID=fopen('ConversionTable2.txt','w');

Y=@(D) D.*80; % converts Dollars to Yens
G=@(D) Y(D)./62.42; % converts Dollars to Yens and then Yens to German Marks
E=@(D) G(D)./1.95583; % converts Dollars to German Marks and then GM to Euros]
D=[0:9 10:10:100]; % dollar vector

fprintf(FID,'%9s %9s %9s %9s ','Dollar', 'Yen', 'Mark','Euro');

for ii=1:numel(D)

fprintf(FID,'%9.2f %9.2f %9.2f %9.2f ',D(ii), Y(D(ii)),G(D(ii)), E(D(ii)));

end
fclose(FID);

--------------------------------------------------------------------------------------

Incorrect Code

clc, clear

FID=fopen('ConversionTable2.txt','w');
Y=@(D) D.*80; % converts Dollars to Yens
G=@(D) Y(D)./62.42; % converts Dollars to Yens and then Yens to German Marks
E=@(D) G(D)./1.95583; % converts Dollars to German Marks and then GM to Euros
D=[0,100]; % dollar vector
fprintf(FID,'%9s %9s %9s %9s ','Dollar', 'Yen', 'Mark','Euro');
jj=20; % aux variable to jump 10 by 10

for ii=1:numel(D)
if ii<=9
fprintf(FID,'%9.2f %9.2f %9.2f %9.2f ',D(ii), Y(D(ii)),G(D(ii)), E(D(ii)));
else
if ii==jj
fprintf(FID,'%9.2f %9.2f %9.2f %9.2f ',D(ii), Y(D(ii)),G(D(ii)), E(D(ii)));
jj=jj+10; % jumps 10 by 10 and reduce output results
end
end

end
fclose(FID);

--------------------------------------------------------------------------------------

Correct Output

------------------------------------------------------------------------------------

Incorrect Output

incorrect code.nm correct_code.m Dollar 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 Yen 0.00 80.00 160.00 240.00 320.00 400.00 480.00 560.00 640.00 720.00 800.00 20.00 1600.00 30.00 2400.00 40.00 3200.00 50.00 4000.00 60.00 4800.00 70.00 5600.00 80.00 6400.00 90.00 7200.00 100.00 8000.00 Mark 0.00 1.28 2.56 3.84 5.13 6.41 7.69 8.97 10.25 11.53 12.82 25.63 38.45 51.27 64.08 76.90 89.71 102.53 115.35 128.16 ConversionTable2.txt Euro 0.00 0.66 1.31 1.97 2.62 3.28 3.93 4.59 5.24 5.90 6.55 13.11 19.66 26.21 32.76 39.32 45.87 52.42 58.98 65.53 1 2 3 4 6 7 10 12 13 14 15 16 17 18 19 20 21

Explanation / Answer

%In the incorrect code, the logic that was thought was the following
%1. A dollar vector was specified which was supposed to account for dollars from 0 to 100
%2.Then in a for loop, the conversion was to be done and written in the
%file for each dollar from 0 to 9, and after then the dollar value was
%added 10 whenever it reached a value of 10,20 till 100. At these points,
%the value was converted to different currencies and written to file.

%however this code had an errorto begin with, as the vector for Dollar
%vector contained only 2 elements 0 and 100. So the resut was that the
%conversion took place only twice.


%the correct code took a simpler approach as it used a dollar vector which
%already had the correct values to be converted i.e 0 to 9 and then
%10,20..90,100. So a for loop was used to perform conversion for each
%element in dollar.
%also, a ' ' was added in the fprintf statement so that notepad could read
%the nextline properly in windows devices.