Convert Fahrenheit to Celcius while the inpu Fahrenheit is an array
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Muhammad Aiman Al-Amin
am 9 Apr. 2023
Bearbeitet: Image Analyst
am 9 Apr. 2023
"Write a script file to compute and display a table to convert from degrees Fahrenheit to degrees Celcius over the temperature (T) range 0 to 100 F in increments of 10 degrees. The conversion relation is T C = 5(T F – 32)/9"
Basically, that the question. However, my main problem is on the fprintf part

As you guys can see, instead of
Fahrenheit1 F = Celcius1 C
Fahrenheit2 F = Celcius2 C
,
it give that output which is
Fahreheit1 F = Fahrenheit2 C
Celcius1 F = Celcius2 C
Anyone know what I should modified my code to make sure the program display the fahrenheit and celcius alternately instead of filling in all the fahrenheit values first in the %format?
0 Kommentare
Akzeptierte Antwort
VBBV
am 9 Apr. 2023
clear
T = 0:10:100;
C = (5*(T-32)/9)
fprintf('%4.2f F = %4.2f C\n',[T;C])
2 Kommentare
Image Analyst
am 9 Apr. 2023
Bearbeitet: Image Analyst
am 9 Apr. 2023
When you have more items in the variable list than % format specifiers in the format specifier string, like 11 instead of 2 in this case, it basically "reuses" the format specifier string for the remaining variable values. It evidently does this row-by-row for the [T, C] 2-D matrix.
Or you could put it into a for loop if you prefer, or think it's simpler.
clear
T = 0:10:100;
C = (5*(T-32)/9)
for k = 1 : numel(C)
fprintf('%4.2f F = %4.2f C\n', T(k), C(k))
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!