Programming Questions for Test 2 1. Ueinm ether afor-oaor while-on write the cod
ID: 3698794 • Letter: P
Question
Programming Questions for Test 2 1. Ueinm ether afor-oaor while-on write the code to display a Celsius to Fahrenheit Table. Begin your table at 0C and display each incremental 5'C up to the maximum "C. You will need to prompt for the maximum C. For Exampie, if the maximum Celsis temperature to display is 22'C, your table should print the folowing values Celsius Fahrenheit 32 10 15 20 50 59 68 Your table does nat have to print exactly as shown, but it must print both Celsius and Fahrenheit values. The formula for converting Celsius to Fahrenheit is as ollows Fahrenheit-Celus32 2. Using a whila-lngn implemen the Divde-and-Conquer algorithm for guessing a number between a minimum and maximum 1. Compute a guess which is the middie value of minimum and maximum (See given code: the min and max variables are already input Aso, the random number seleced between min and max is also aready given) 2. If your guess is correct you are fnished and should exit the loop. 3. If your guess is incomect then determine whether your guess was higher or lower than the randomy selected number 3. If yourguess is oo high then change your maximum value to your guess and return to step , 4, If your guess is too low then change your minimum value to your guess and reurn to step 1Explanation / Answer
Solution:
Using for loop:
%Save this script as CelsiustoFahrenheit.m
%Get user input for maxTemp
maxTemp=input('Enter the maximum Celsius to convert to Fahrenheit: ');
%print header for for loop
fprintf('Celsius Fahrenheit (For loop) ');
%for values of Celsius starting with 0 and increment 5 degrees
%till it reaches maxTemp,calculate Fahrenheit and display the values
for Celsius=0:5:maxTemp
Fahrenheit=9/5*Celsius+32;
fprintf('%d %d ',Celsius, Fahrenheit);
end
Using while loop:
%Make Celsius 0 for start of while loop and print header for while loop
Celsius = 0;
fprintf(' Celsius Fahrenheit (While loop) ');
%until Celsius is less than maxTemp, calculate Fahrenheit and display their
%values and increment Celsius by 5
while Celsius<maxTemp
Fahrenheit=9/5*Celsius+32;
fprintf('%d %d ',Celsius, Fahrenheit);
Celsius=Celsius+5;
end
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.