This program is to be written in Octave (a program largely compatible with MATLA
ID: 3539473 • Letter: T
Question
This program is to be written in Octave (a program largely compatible with MATLAB) . Submit only a single .m file.
Problem: Given an external data file that contains an unknown number of integers determine which, if any, of the patterns below are met by each number.
Pattern #1 - XYXY (first and third, second and fourth digits are the same)
Pattern #2 - XXYY (first and second, third and fourth digits are the same)
Pattern #3 - XYYX (first and fourth, second and third digits are the same)
Other specifications:
All data will be found in a file called labData, (Create one of your own with the example values to check the output)
the file will contain an unknown quantity of integers (range from 0 to 9999),
and all output is to the monitor.
Example labData file:
Expected output generated to monitor:
Few Notes:
1. All variables in your program MUST be scalar, no arrays, vectors, matrices are permitted on this assignment.
2. This is an end of file problem as you don't know how much data will be in the external file.
3. You WILL NOT make use of any user-defined functions in this assignment.
Explanation / Answer
fid = fopen('labData.txt','r','n');
while ~feof(fid)
num = fscanf(fid,'%d',1);
fprintf('%d satisfies:',num);
a = floor(num/1000);
num = mod(num,1000);
b = floor(num/100);
num = mod(num,100);
c = floor(num/10);
d = mod(num,10);
check = 0;
if (and((a==c),(b==d)))
check = 1;
fprintf(' - Pattern #1 ');
end
if (and((a==b),(c==d)))
fprintf(' - Pattern #2 ');
check = 1;
end
if (and((a==d),(b==c)))
fprintf(' - Pattern #3 ');
check = 1;
end
if (check == 0)
fprintf(' none of the patterns. ');
end
fprintf(' ');
end
fclose(fid);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.