fork download
  1. //********************************************************
  2. //
  3. // C Midterm - Question 7
  4. //
  5. // Name: Maya Mahin
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 23, 2025
  10. //
  11. // Description: This program prints charts showing the
  12. // Fahrenheit equivalents of all Celsius temperatures from
  13. // 0 to 100 degrees, and the Celsius equivalents of all
  14. // Fahrenheit temperatures from 32 to 212 degrees
  15. //
  16. //********************************************************
  17.  
  18. #include <stdio.h>
  19.  
  20. /*define function prototypes*/
  21. float toCelsius(float fahrenheitTemp);
  22. float toFahrenheit(float celsiusTemp);
  23. void printTableHeader(int tableNum);
  24. void printTable(int TableNum, float temperature1, float temperature2);
  25.  
  26. int main(void) {
  27.  
  28. printTableHeader(1); /*print the header for the first table (i.e., the table that shows
  29.   celsius values converted to fahrenheit values)*/
  30.  
  31. /*Loop through values 0-100 and use them as
  32.   input values for the function that receives
  33.   a temperature value in celsius and returns
  34.   the converted value in fahrenheit*/
  35. for (int i=0; i<101; i++) {
  36. float fahrenheitRetVal=toFahrenheit(i); /*call the toFahrenheit function with the value from the loop*/
  37. printTable(1,i,fahrenheitRetVal); /*print the results from the function in the table*/
  38. }
  39.  
  40. printTableHeader(2); /*print the header for the second table (i.e., the table that shows
  41.   fahrenheit values converted to celsius values)*/
  42.  
  43. /*Loop through values 32-212 and use them as
  44.   input values for the function that receives
  45.   a temperature value in fahrenheit and returns
  46.   the converted value in celsius*/
  47. for (int i=32; i<213; i++) {
  48. float celsiusRetVal=toCelsius(i); /*call the toCelsius function with the value from the loop*/
  49. printTable(2,i,celsiusRetVal); /*print the results from the function in the table*/
  50. }
  51.  
  52. return 0;
  53. }
  54.  
  55. //**************************************************************
  56. // Function: toCelsius
  57. //
  58. // Purpose: Receives a temperature in fahrenheit, converts it
  59. // to Celsius and returns the converted value
  60. //
  61. // Parameters:
  62. //
  63. // fahrenheitTemp - input temperature in fahrenheit
  64. //
  65. // Returns: celsiusTemp - result of converting input temperature to celsius
  66. //
  67. //**************************************************************
  68. float toCelsius(float fahrenheitTemp){
  69. float celsiusTemp=(fahrenheitTemp - 32) * 5/9; /*using the formula to convert fahrenheit to celsius*/
  70. return celsiusTemp; /*returning the celsius value*/
  71. }
  72. //**************************************************************
  73. // Function: toFahrenheit
  74. //
  75. // Purpose: Receives a temperature in celsius, converts it
  76. // to Fahrenheit and returns the converted value
  77. //
  78. // Parameters:
  79. //
  80. // celsiusTemp - input temperature in celsius
  81. //
  82. // Returns: fahrenheitTemp - result of converting input temperature to fahrenheit
  83. //
  84. //**************************************************************
  85. float toFahrenheit(float celsiusTemp){
  86. float fahrenheitTemp=(celsiusTemp * 9/5) + 32; /*applying formula to convert celsius to fahrenheit*/
  87. return fahrenheitTemp; /*returning fahrenheit value*/
  88. }
  89.  
  90. //**************************************************************
  91. // Function: printTableHeader
  92. //
  93. // Purpose: Prints the initial table header information.
  94. //
  95. // Parameters: tableNum - indicates which table header to be provider
  96. // (a value of 1 indicates the first table header,
  97. // a value of 2 indicates the second table header)
  98. //
  99. // Returns: void
  100. //
  101. //**************************************************************
  102.  
  103. void printTableHeader (int tableNum)
  104. {
  105.  
  106. if (tableNum==1){
  107. // print the table header for the celsius to fahrenheit conversion table
  108. printf("\nCelsius Fahrenheit\n");
  109. printf("------------------------\n");
  110. }
  111. else if (tableNum==2){
  112. // print the table header for the fahrenheit to celsius conversion table
  113. printf("\nFahrenheit Celsius\n");
  114. printf("------------------------\n");
  115. }
  116.  
  117. } // printTableHeader
  118.  
  119. //*************************************************************
  120. // Function: printTable
  121. //
  122. // Purpose: Prints out all the temperature information
  123. // in a nice and orderly table format.
  124. //
  125. // Parameters: tableNum - table number that printing output for
  126. // temperature1 - the initial temperature value used to produce the converted temperature value
  127. // temperature2 - the converted temperature value
  128. //
  129. //*************************************************************
  130. void printTable (int tableNum, float temperature1, float temperature2)
  131. {
  132.  
  133. // print the temperature
  134. if (tableNum==1){
  135. printf("%-10.0f %-10.1f\n",
  136. temperature1, temperature2); /*format the temperature values one way in the first table*/
  137. }
  138. else if (tableNum==2){
  139. printf("%-13.0f %-13.2f\n",
  140. temperature1, temperature2); /*format the temperature values in a slightly different way in the second table
  141.   this is just to make sure everything aligns and the formatting is appropriate
  142.   for the type of value being displayed*/
  143. }
  144. }
  145.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Celsius      Fahrenheit
------------------------
0            32.0      
1            33.8      
2            35.6      
3            37.4      
4            39.2      
5            41.0      
6            42.8      
7            44.6      
8            46.4      
9            48.2      
10           50.0      
11           51.8      
12           53.6      
13           55.4      
14           57.2      
15           59.0      
16           60.8      
17           62.6      
18           64.4      
19           66.2      
20           68.0      
21           69.8      
22           71.6      
23           73.4      
24           75.2      
25           77.0      
26           78.8      
27           80.6      
28           82.4      
29           84.2      
30           86.0      
31           87.8      
32           89.6      
33           91.4      
34           93.2      
35           95.0      
36           96.8      
37           98.6      
38           100.4     
39           102.2     
40           104.0     
41           105.8     
42           107.6     
43           109.4     
44           111.2     
45           113.0     
46           114.8     
47           116.6     
48           118.4     
49           120.2     
50           122.0     
51           123.8     
52           125.6     
53           127.4     
54           129.2     
55           131.0     
56           132.8     
57           134.6     
58           136.4     
59           138.2     
60           140.0     
61           141.8     
62           143.6     
63           145.4     
64           147.2     
65           149.0     
66           150.8     
67           152.6     
68           154.4     
69           156.2     
70           158.0     
71           159.8     
72           161.6     
73           163.4     
74           165.2     
75           167.0     
76           168.8     
77           170.6     
78           172.4     
79           174.2     
80           176.0     
81           177.8     
82           179.6     
83           181.4     
84           183.2     
85           185.0     
86           186.8     
87           188.6     
88           190.4     
89           192.2     
90           194.0     
91           195.8     
92           197.6     
93           199.4     
94           201.2     
95           203.0     
96           204.8     
97           206.6     
98           208.4     
99           210.2     
100          212.0     

Fahrenheit      Celsius
------------------------
32              0.00         
33              0.56         
34              1.11         
35              1.67         
36              2.22         
37              2.78         
38              3.33         
39              3.89         
40              4.44         
41              5.00         
42              5.56         
43              6.11         
44              6.67         
45              7.22         
46              7.78         
47              8.33         
48              8.89         
49              9.44         
50              10.00        
51              10.56        
52              11.11        
53              11.67        
54              12.22        
55              12.78        
56              13.33        
57              13.89        
58              14.44        
59              15.00        
60              15.56        
61              16.11        
62              16.67        
63              17.22        
64              17.78        
65              18.33        
66              18.89        
67              19.44        
68              20.00        
69              20.56        
70              21.11        
71              21.67        
72              22.22        
73              22.78        
74              23.33        
75              23.89        
76              24.44        
77              25.00        
78              25.56        
79              26.11        
80              26.67        
81              27.22        
82              27.78        
83              28.33        
84              28.89        
85              29.44        
86              30.00        
87              30.56        
88              31.11        
89              31.67        
90              32.22        
91              32.78        
92              33.33        
93              33.89        
94              34.44        
95              35.00        
96              35.56        
97              36.11        
98              36.67        
99              37.22        
100             37.78        
101             38.33        
102             38.89        
103             39.44        
104             40.00        
105             40.56        
106             41.11        
107             41.67        
108             42.22        
109             42.78        
110             43.33        
111             43.89        
112             44.44        
113             45.00        
114             45.56        
115             46.11        
116             46.67        
117             47.22        
118             47.78        
119             48.33        
120             48.89        
121             49.44        
122             50.00        
123             50.56        
124             51.11        
125             51.67        
126             52.22        
127             52.78        
128             53.33        
129             53.89        
130             54.44        
131             55.00        
132             55.56        
133             56.11        
134             56.67        
135             57.22        
136             57.78        
137             58.33        
138             58.89        
139             59.44        
140             60.00        
141             60.56        
142             61.11        
143             61.67        
144             62.22        
145             62.78        
146             63.33        
147             63.89        
148             64.44        
149             65.00        
150             65.56        
151             66.11        
152             66.67        
153             67.22        
154             67.78        
155             68.33        
156             68.89        
157             69.44        
158             70.00        
159             70.56        
160             71.11        
161             71.67        
162             72.22        
163             72.78        
164             73.33        
165             73.89        
166             74.44        
167             75.00        
168             75.56        
169             76.11        
170             76.67        
171             77.22        
172             77.78        
173             78.33        
174             78.89        
175             79.44        
176             80.00        
177             80.56        
178             81.11        
179             81.67        
180             82.22        
181             82.78        
182             83.33        
183             83.89        
184             84.44        
185             85.00        
186             85.56        
187             86.11        
188             86.67        
189             87.22        
190             87.78        
191             88.33        
192             88.89        
193             89.44        
194             90.00        
195             90.56        
196             91.11        
197             91.67        
198             92.22        
199             92.78        
200             93.33        
201             93.89        
202             94.44        
203             95.00        
204             95.56        
205             96.11        
206             96.67        
207             97.22        
208             97.78        
209             98.33        
210             98.89        
211             99.44        
212             100.00