Convert Fahrenheit to Celcius while the inpu Fahrenheit is an array

3 ビュー (過去 30 日間)
Muhammad Aiman Al-Amin
Muhammad Aiman Al-Amin 2023 年 4 月 9 日
編集済み: Image Analyst 2023 年 4 月 9 日
"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?

採用された回答

VBBV
VBBV 2023 年 4 月 9 日
clear
T = 0:10:100;
C = (5*(T-32)/9)
C = 1×11
-17.7778 -12.2222 -6.6667 -1.1111 4.4444 10.0000 15.5556 21.1111 26.6667 32.2222 37.7778
fprintf('%4.2f F = %4.2f C\n',[T;C])
0.00 F = -17.78 C 10.00 F = -12.22 C 20.00 F = -6.67 C 30.00 F = -1.11 C 40.00 F = 4.44 C 50.00 F = 10.00 C 60.00 F = 15.56 C 70.00 F = 21.11 C 80.00 F = 26.67 C 90.00 F = 32.22 C 100.00 F = 37.78 C
  2 件のコメント
Muhammad Aiman Al-Amin
Muhammad Aiman Al-Amin 2023 年 4 月 9 日
Oh I see, so whenever we want to have multiple set of arrays as a set of inputs, I need to put the matrice square bracket,
Thank you!
Image Analyst
Image Analyst 2023 年 4 月 9 日
編集済み: Image Analyst 2023 年 4 月 9 日
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)
C = 1×11
-17.7778 -12.2222 -6.6667 -1.1111 4.4444 10.0000 15.5556 21.1111 26.6667 32.2222 37.7778
for k = 1 : numel(C)
fprintf('%4.2f F = %4.2f C\n', T(k), C(k))
end
0.00 F = -17.78 C 10.00 F = -12.22 C 20.00 F = -6.67 C 30.00 F = -1.11 C 40.00 F = 4.44 C 50.00 F = 10.00 C 60.00 F = 15.56 C 70.00 F = 21.11 C 80.00 F = 26.67 C 90.00 F = 32.22 C 100.00 F = 37.78 C

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by