Modify problem 1 so that it runs within a loop that prompts the user whether to
ID: 3732986 • Letter: M
Question
Modify problem 1 so that it runs within a loop that prompts the user whether to continue at the end of each iteration and does another if the user enters y or Y. (Sample run below)
>> HW07_06
Enter angle 1 in degrees: 45
Enter angle 2 in degrees: -45
cos(45.000000) = cos(-45.000000) = 0.707107.
Enter Y to do another: y
Enter angle 1 in degrees: 30
Enter angle 2 in degrees: 60
cos(30.000000) = 0.866025 which is greater than cos(60.000000) = 0.500000. Enter Y to do another: y
Enter angle 1 in degrees: 90
Enter angle 2 in degrees: 0
cos(90.000000) = 0.000000 which is less than cos(0.000000) = 1.000000.
Explanation / Answer
% Code
choice = 'y';
% looping as long as user enters y
while choice == 'y' || choice == 'Y'
% taking user input of angles
prompt = "Enter angle 1 in degrees: ";
angle1_degree = input(prompt);
prompt = "Enter angle 2 in degrees: ";
angle2_degree = input(prompt);
% converting degrees to radians
angle1_radian = deg2rad(angle1_degree);
angle2_radian = deg2rad(angle2_degree);
% calculating cos of the angles
cos1 = cos(angle1_radian);
cos2 = cos(angle2_radian);
% checking condition and printing output
if (cos1 > cos2)
fprintf('cos(%f) = %f which is greater than cos(%f) = %f. ',angle1_degree,cos1,angle2_degree, cos2);
elseif (cos1 < cos2)
fprintf('cos(%f) = %f which is less than cos(%f) = %f. ',angle1_degree,cos1,angle2_degree, cos2);
else
fprintf('cos(%f) = cos(%f) = %f. ',angle1_degree, angle2_degree, cos1)
end
% asking for user's choice of one more round
choice = input(" Enter Y to do another: ",'s');
end
% Output
Enter angle 1 in degrees: 45
Enter angle 2 in degrees: -45
cos(45.000000) = cos(-45.000000) = 0.707107.
Enter Y to do another: y
Enter angle 1 in degrees: 30
Enter angle 2 in degrees: 60
cos(30.000000) = 0.866025 which is greater than cos(60.000000) = 0.500000.
Enter Y to do another: Y
Enter angle 1 in degrees: 90
Enter angle 2 in degrees: 0
cos(90.000000) = 0.000000 which is less than cos(0.000000) = 1.000000.
Enter Y to do another: n
% Code
choice = 'y';
% looping as long as user enters y
while choice == 'y' || choice == 'Y'
% taking user input of angles
prompt = "Enter angle 1 in degrees: ";
angle1_degree = input(prompt);
prompt = "Enter angle 2 in degrees: ";
angle2_degree = input(prompt);
% converting degrees to radians
angle1_radian = deg2rad(angle1_degree);
angle2_radian = deg2rad(angle2_degree);
% calculating cos of the angles
cos1 = cos(angle1_radian);
cos2 = cos(angle2_radian);
% checking condition and printing output
if (cos1 > cos2)
fprintf('cos(%f) = %f which is greater than cos(%f) = %f. ',angle1_degree,cos1,angle2_degree, cos2);
elseif (cos1 < cos2)
fprintf('cos(%f) = %f which is less than cos(%f) = %f. ',angle1_degree,cos1,angle2_degree, cos2);
else
fprintf('cos(%f) = cos(%f) = %f. ',angle1_degree, angle2_degree, cos1)
end
% asking for user's choice of one more round
choice = input(" Enter Y to do another: ",'s');
end
% Output
Enter angle 1 in degrees: 45
Enter angle 2 in degrees: -45
cos(45.000000) = cos(-45.000000) = 0.707107.
Enter Y to do another: y
Enter angle 1 in degrees: 30
Enter angle 2 in degrees: 60
cos(30.000000) = 0.866025 which is greater than cos(60.000000) = 0.500000.
Enter Y to do another: Y
Enter angle 1 in degrees: 90
Enter angle 2 in degrees: 0
cos(90.000000) = 0.000000 which is less than cos(0.000000) = 1.000000.
Enter Y to do another: n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.