Write a script that will prompt the user for N integers, and then write the positive numbers (>= 0) to an ASCII file called pos.dat and the negative numbers to an ASCII file called neg.dat. Error-check to make sure that the user enters N integers
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:5| Question number:39.5
All Answers
total answers (1)
Ch5Ex39.m
% Prompt the user for N integers, writing the positive
% integers to one file and the negative integers to another
% initialize vectors to store pos and neg integers
posints = [];
negints = [];
% loop n times
n=10;
for i=1:n
inputnum = input('Enter an integer: ');
num2 = int32(inputnum);
% error check to make sure integers are entered
while num2 ~= inputnum
inputnum = input('Invalid! Enter an integer: ');
num2 = int32(inputnum);
end
% add to appropriate vector
if inputnum < 0
negints = [negints inputnum];
else
posints = [posints inputnum];
end
end
% write vectors to files
save pos.dat posints -ascii
save neg.dat negints -ascii
need an explanation for this answer? contact us directly to get an explanation for this answer