Q:

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

0

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. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now