Q:

Write a script that will loop to prompt the user for n circle radii. The script will call a function to calculate the area of each circle, and will write the results in sentence form to a file

0

Write a script that will loop to prompt the user for n circle radii. The script will call a function to calculate the area of each circle, and will write the results in sentence form to a file.

All Answers

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

Ch9Ex27.m

% Write the areas of circles to a file

% Prompt the user for the radii

n = 5; 

%Open a new file for writing

fid = fopen('circles.dat','w');

for i = 1:n

 radius = input('Enter the radius of a circle: ');

 while radius <= 0 %Error-check user input

 radius = input('Enter the radius of a circle: ');

 end

 area = calcarea(radius);

 

 %Write to file "circles.dat"

 fprintf(fid,'The area of circle #%d is %.2f\n',i,area);

end

%Error-check file close

closeresult = fclose(fid);

if closeresult == 0

 disp('File close successful')

else

 disp('File close not successful')

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