Q:

Write a script to read in division codes and sales for a company from a file that has the following format: A 4.2 B 3.9 Print the division with the highest sales

0

Write a script to read in division codes and sales for a company 

from a file that has the following format:

A 4.2

B 3.9

Print the division with the highest sales.

All Answers

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

Ch9Ex22.m

% Read in company data and print the division with the 

% highest sales

fid = fopen('sales.dat');

if fid == -1

 disp('File open not successful')

else

 %Read in data from file

 C = textscan(fid,'%c %f');

 [sales, index] = max(C{2}); %Find the max sales and its index 

 code = C{1}(index); 

 fprintf('Division %s has the highest sales of %.1f\n',...

 code,sales)

 

 %Error-check file close

 closeresult = fclose(fid);

 if closeresult == 0

 disp('File close successful')

 else

 disp('File close not successful')

 end

end

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