fork download
  1. //************************************************************
  2. // Name: Anthony Principe
  3. // Class: C Programming, Fall 2025
  4. // Assignment: Midterm Exam
  5. // Date: October 26, 2025
  6. //
  7. // Description: Midterm covering functions, loops, conditionals,
  8. // structures, and string manipulation. Includes
  9. // examples of function documentation, arithmetic
  10. // operations, decision making, and structure usage.
  11. //************************************************************
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h> // used for tolower() in Question 6
  15.  
  16. //************************************************************
  17. // Function Prototypes
  18. //************************************************************
  19. float calcAreaOfTriangle(float base, float height);
  20. float calcAreaOfRectangle(float length, float width);
  21. float calcAreaOfSquare(float side);
  22. float calcAreaOfCircle(float radius);
  23. float calcAreaOfParallelogram(float base, float height);
  24. float calcAreaOfTrapezoid(float base1, float base2, float height);
  25.  
  26. int frequency(int theArray[], int n, int x);
  27. int irishLicensePlateValidator(int year, int halfYear, char countyCode);
  28. float calcDogToHumanYears(int dogYear);
  29. char calcLetterGrade(int score);
  30.  
  31. struct countyTotals freqOfIrishCounties(char countyArrayList[], int size);
  32.  
  33. float toCelsius(int theFahrenheitTemp);
  34. float toFahrenheit(int theCelsiusTemp);
  35.  
  36. // supporting structures for later questions
  37. struct countyTotals {
  38. int totalCorkCodes;
  39. int totalDublinCodes;
  40. int totalGalwayCodes;
  41. int totalLimerickCodes;
  42. int totalTiperaryCodes;
  43. int totalWaterfordCodes;
  44. int totalInvalidCountryCodes;
  45. };
  46.  
  47. struct date {
  48. int day;
  49. int month;
  50. int year;
  51. };
  52.  
  53. struct dd2209 {
  54. int id;
  55. char officerName[25];
  56. struct date trainingDate;
  57. };
  58.  
  59. //************************************************************
  60. // MAIN FUNCTION
  61. //************************************************************
  62. int main(void)
  63. {
  64. // Example for Question 1
  65. float triArea = calcAreaOfTriangle(5.0f, 10.0f);
  66. printf("Triangle Area: %.2f\n\n", triArea);
  67.  
  68. // Example for Question 2
  69. int numbers[5] = {1, 3, 3, 4, 3};
  70. printf("Frequency of 3 = %d\n\n", frequency(numbers, 5, 3));
  71.  
  72. // Example for Question 3
  73. printf("Valid Plate: %d\n\n", irishLicensePlateValidator(2020, 1, 'D'));
  74.  
  75. // Example for Question 4
  76. printf("Dog age 5 = %.1f human years\n\n", calcDogToHumanYears(5));
  77.  
  78. // Example for Question 5
  79. printf("Letter grade for 88 = %c\n\n", calcLetterGrade(88));
  80.  
  81. // Example for Question 6
  82. char counties[6] = {'C', 'd', 'g', 'w', 'x', 'L'};
  83. struct countyTotals totals = freqOfIrishCounties(counties, 6);
  84. printf("Cork: %d Dublin: %d Galway: %d Limerick: %d Invalid: %d\n\n",
  85. totals.totalCorkCodes, totals.totalDublinCodes,
  86. totals.totalGalwayCodes, totals.totalLimerickCodes,
  87. totals.totalInvalidCountryCodes);
  88.  
  89. // Example for Question 7: Conversion charts
  90. printf("Celsius to Fahrenheit Chart:\n");
  91. printf("Celsius\tFahrenheit\n");
  92. for (int c = 0; c <= 100; c += 10)
  93. printf("%d\t%.1f\n", c, toFahrenheit(c));
  94.  
  95. printf("\nFahrenheit to Celsius Chart:\n");
  96. printf("Fahrenheit\tCelsius\n");
  97. for (int f = 32; f <= 212; f += 20)
  98. printf("%d\t\t%.1f\n", f, toCelsius(f));
  99.  
  100. return 0;
  101. }
  102.  
  103. //************************************************************
  104. // Question 1 - Shape Area Calculations
  105. //************************************************************
  106.  
  107. // **************************************************
  108. // Function: calcAreaOfTriangle
  109. //
  110. // Description: Calculates the area of a triangle.
  111. //
  112. // Parameters: base - base of the triangle
  113. // height - height of the triangle
  114. //
  115. // Returns: area - area of triangle
  116. // **************************************************
  117. float calcAreaOfTriangle(float base, float height)
  118. {
  119. float area; // area of the triangle
  120. area = (base * height) / 2.0f;
  121. return area;
  122. }
  123.  
  124. // **************************************************
  125. // Function: calcAreaOfRectangle
  126. //
  127. // Description: Calculates the area of a rectangle.
  128. //
  129. // Parameters: length - length of rectangle
  130. // width - width of rectangle
  131. //
  132. // Returns: area - area of rectangle
  133. // **************************************************
  134. float calcAreaOfRectangle(float length, float width)
  135. {
  136. float area = length * width;
  137. return area;
  138. }
  139.  
  140. // **************************************************
  141. // Function: calcAreaOfSquare
  142. //
  143. // Description: Calculates area of a square.
  144. //
  145. // Parameters: side - length of one side
  146. //
  147. // Returns: area - area of square
  148. // **************************************************
  149. float calcAreaOfSquare(float side)
  150. {
  151. return (side * side);
  152. }
  153.  
  154. // **************************************************
  155. // Function: calcAreaOfCircle
  156. //
  157. // Description: Calculates area of a circle.
  158. //
  159. // Parameters: radius - radius of the circle
  160. //
  161. // Returns: area - area of circle
  162. // **************************************************
  163. float calcAreaOfCircle(float radius)
  164. {
  165. const float PI = 3.14159f;
  166. return (PI * radius * radius);
  167. }
  168.  
  169. // **************************************************
  170. // Function: calcAreaOfParallelogram
  171. //
  172. // Description: Calculates area of a parallelogram.
  173. //
  174. // Parameters: base - base length
  175. // height - height length
  176. //
  177. // Returns: area - area of parallelogram
  178. // **************************************************
  179. float calcAreaOfParallelogram(float base, float height)
  180. {
  181. return (base * height);
  182. }
  183.  
  184. // **************************************************
  185. // Function: calcAreaOfTrapezoid
  186. //
  187. // Description: Calculates area of a trapezoid.
  188. //
  189. // Parameters: base1, base2 - bases of trapezoid
  190. // height - height of trapezoid
  191. //
  192. // Returns: area - area of trapezoid
  193. // **************************************************
  194. float calcAreaOfTrapezoid(float base1, float base2, float height)
  195. {
  196. return ((base1 + base2) * height) / 2.0f;
  197. }
  198.  
  199. //************************************************************
  200. // Question 2 - Frequency Function
  201. //************************************************************
  202.  
  203. // **************************************************
  204. // Function: frequency
  205. //
  206. // Description: Counts how many times a value appears
  207. // in an array.
  208. //
  209. // Parameters: theArray - array of integers
  210. // n - number of elements
  211. // x - value to search for
  212. //
  213. // Returns: frequency - how many times x was found
  214. // **************************************************
  215. int frequency(int theArray[], int n, int x)
  216. {
  217. int frequency = 0; // initialize count
  218.  
  219. for (int i = 0; i < n; i++)
  220. {
  221. if (theArray[i] == x)
  222. frequency++;
  223. }
  224.  
  225. return frequency;
  226. }
  227.  
  228. //************************************************************
  229. // Question 3 - Irish License Plate Validator
  230. //************************************************************
  231.  
  232. // **************************************************
  233. // Function: irishLicensePlateValidator
  234. //
  235. // Description: Validates an Irish plate based on
  236. // year, half year (1 or 2), and county.
  237. //
  238. // Parameters: year - year portion of plate
  239. // halfYear - 1 or 2
  240. // countyCode- county code letter
  241. //
  242. // Returns: 1 for valid, 0 for invalid
  243. // **************************************************
  244. int irishLicensePlateValidator(int year, int halfYear, char countyCode)
  245. {
  246. int valid = 1;
  247.  
  248. if (year < 1987 || year > 2025)
  249. valid = 0;
  250. else if (halfYear != 1 && halfYear != 2)
  251. valid = 0;
  252. else if (!isalpha(countyCode))
  253. valid = 0;
  254.  
  255. return valid;
  256. }
  257.  
  258. //************************************************************
  259. // Question 4 - Dog to Human Years
  260. //************************************************************
  261.  
  262. // **************************************************
  263. // Function: calcDogToHumanYears
  264. //
  265. // Description: Converts dog years to human years.
  266. //
  267. // Parameters: dogYear - age of dog in years
  268. //
  269. // Returns: humanYears - equivalent in human years
  270. // **************************************************
  271. float calcDogToHumanYears(int dogYear)
  272. {
  273. float humanYears;
  274.  
  275. if (dogYear <= 2)
  276. humanYears = dogYear * 10.5f;
  277. else
  278. humanYears = 21 + (dogYear - 2) * 4.0f;
  279.  
  280. return humanYears;
  281. }
  282.  
  283. //************************************************************
  284. // Question 5 - Letter Grade
  285. //************************************************************
  286.  
  287. // **************************************************
  288. // Function: calcLetterGrade
  289. //
  290. // Description: Converts numeric score to letter grade.
  291. //
  292. // Parameters: score - numeric test score
  293. //
  294. // Returns: result - letter grade (A-F)
  295. // **************************************************
  296. char calcLetterGrade(int score)
  297. {
  298. char result;
  299.  
  300. if (score > 100 || score < 0)
  301. result = 'X'; // invalid score
  302. else if (score >= 90)
  303. result = 'A';
  304. else if (score >= 80)
  305. result = 'B';
  306. else if (score >= 70)
  307. result = 'C';
  308. else if (score >= 60)
  309. result = 'D';
  310. else
  311. result = 'F';
  312.  
  313. return result;
  314. }
  315.  
  316. //************************************************************
  317. // Question 6 - County Frequency
  318. //************************************************************
  319.  
  320. // **************************************************
  321. // Function: freqOfIrishCounties
  322. //
  323. // Description: Counts how many times each county code
  324. // appears in an array.
  325. //
  326. // Parameters: countyArrayList - array of county codes
  327. // size - size of array
  328. //
  329. // Returns: myCountyTotals - struct with counts
  330. // **************************************************
  331. struct countyTotals freqOfIrishCounties(char countyArrayList[], int size)
  332. {
  333. struct countyTotals myCountyTotals = {0,0,0,0,0,0,0};
  334.  
  335. for (int i = 0; i < size; i++)
  336. {
  337. char code = tolower(countyArrayList[i]);
  338.  
  339. switch (code)
  340. {
  341. case 'c': myCountyTotals.totalCorkCodes++; break;
  342. case 'd': myCountyTotals.totalDublinCodes++; break;
  343. case 'g': myCountyTotals.totalGalwayCodes++; break;
  344. case 'l': myCountyTotals.totalLimerickCodes++; break;
  345. case 't': myCountyTotals.totalTiperaryCodes++; break;
  346. case 'w': myCountyTotals.totalWaterfordCodes++; break;
  347. default: myCountyTotals.totalInvalidCountryCodes++; break;
  348. }
  349. }
  350.  
  351. return myCountyTotals;
  352. }
  353.  
  354. //************************************************************
  355. // Question 7 - Temperature Conversion
  356. //************************************************************
  357.  
  358. // **************************************************
  359. // Function: toCelsius
  360. //
  361. // Description: Converts Fahrenheit to Celsius.
  362. //
  363. // Parameters: theFahrenheitTemp - Fahrenheit temp
  364. //
  365. // Returns: Celsius temperature
  366. // **************************************************
  367. float toCelsius(int theFahrenheitTemp)
  368. {
  369. return (theFahrenheitTemp - 32) * 5.0f / 9.0f;
  370. }
  371.  
  372. // **************************************************
  373. // Function: toFahrenheit
  374. //
  375. // Description: Converts Celsius to Fahrenheit.
  376. //
  377. // Parameters: theCelsiusTemp - Celsius temp
  378. //
  379. // Returns: Fahrenheit temperature
  380. // **************************************************
  381. float toFahrenheit(int theCelsiusTemp)
  382. {
  383. return (theCelsiusTemp * 9.0f / 5.0f) + 32.0f;
  384. }
  385.  
  386. //************************************************************
  387. // Question 8 - Structures (Officer Example)
  388. //************************************************************
  389. // Example initialization:
  390. void exampleStructUsage(void)
  391. {
  392. struct dd2209 officer1 = {1001, "John Murphy", {12, 10, 2025}};
  393. printf("Officer ID: %d Name: %s Training Date: %d/%d/%d\n",
  394. officer1.id, officer1.officerName,
  395. officer1.trainingDate.day,
  396. officer1.trainingDate.month,
  397. officer1.trainingDate.year);
  398. }
  399.  
  400. //************************************************************
  401. // Question 9 - Compiler Likes/Dislikes
  402. //************************************************************
  403. // 1) Like: Easy to test small programs quickly.
  404. // 2) Like: Good error messages that point to line numbers.
  405. // 3) Dislike: Some warnings are vague for beginners.
  406. // 4) Dislike: Output window closes too fast on Windows.
  407.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Triangle Area: 25.00

Frequency of 3 = 3

Valid Plate: 1

Dog age 5 = 33.0 human years

Letter grade for 88 = B

Cork: 1  Dublin: 1  Galway: 1  Limerick: 1  Invalid: 1

Celsius to Fahrenheit Chart:
Celsius	Fahrenheit
0	32.0
10	50.0
20	68.0
30	86.0
40	104.0
50	122.0
60	140.0
70	158.0
80	176.0
90	194.0
100	212.0

Fahrenheit to Celsius Chart:
Fahrenheit	Celsius
32		0.0
52		11.1
72		22.2
92		33.3
112		44.4
132		55.6
152		66.7
172		77.8
192		88.9
212		100.0