fork download
  1. /*
  2.  * Name: Haroon Malik
  3.  * Fall 2026 Intro to Programming with C - Midterm
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define PI 3.14159265358979323846f
  9.  
  10. /* Question 1: Dimensions are assumed to be nonnegative. */
  11.  
  12. /* **************************************************
  13.  * Function: calcAreaOfTriangle
  14.  * Description: Calculates a triangle's area.
  15.  * Parameters: base - base length; height - perpendicular height.
  16.  * Returns: Area in square units.
  17.  * ************************************************** */
  18. float calcAreaOfTriangle(float base, float height)
  19. {
  20. return 0.5f * base * height;
  21. }
  22.  
  23. /* **************************************************
  24.  * Function: calcAreaOfRectangle
  25.  * Description: Calculates a rectangle's area.
  26.  * Parameters: length - length; width - width.
  27.  * Returns: Area in square units.
  28.  * ************************************************** */
  29. float calcAreaOfRectangle(float length, float width)
  30. {
  31. return length * width;
  32. }
  33.  
  34. /* **************************************************
  35.  * Function: calcAreaOfSquare
  36.  * Description: Calculates a square's area.
  37.  * Parameters: side - side length.
  38.  * Returns: Area in square units.
  39.  * ************************************************** */
  40. float calcAreaOfSquare(float side)
  41. {
  42. return side * side;
  43. }
  44.  
  45. /* **************************************************
  46.  * Function: calcAreaOfCircle
  47.  * Description: Calculates a circle's area.
  48.  * Parameters: radius - circle radius.
  49.  * Returns: Area in square units.
  50.  * ************************************************** */
  51. float calcAreaOfCircle(float radius)
  52. {
  53. return PI * radius * radius;
  54. }
  55.  
  56. /* **************************************************
  57.  * Function: calcAreaOfParallelogram
  58.  * Description: Calculates a parallelogram's area.
  59.  * Parameters: base - base length; height - perpendicular height.
  60.  * Returns: Area in square units.
  61.  * ************************************************** */
  62. float calcAreaOfParallelogram(float base, float height)
  63. {
  64. return base * height;
  65. }
  66.  
  67. /* **************************************************
  68.  * Function: calcAreaOfTrapezoid
  69.  * Description: Calculates a trapezoid's area.
  70.  * Parameters: base1, base2 - parallel side lengths;
  71.  * height - perpendicular distance between them.
  72.  * Returns: Area in square units.
  73.  * ************************************************** */
  74. float calcAreaOfTrapezoid(float base1, float base2, float height)
  75. {
  76. return 0.5f * (base1 + base2) * height;
  77. }
  78.  
  79. /* Question 2 */
  80. /* **************************************************
  81.  * Function: frequency
  82.  * Description: Counts occurrences of x in the first n elements.
  83.  * Parameters: theArray - array with at least n elements;
  84.  * n - nonnegative element count; x - value to count.
  85.  * Returns: Number of elements equal to x.
  86.  * ************************************************** */
  87. int frequency(int theArray[], int n, int x)
  88. {
  89. int frequency = 0;
  90.  
  91. for (int i = 0; i < n; i++)
  92. {
  93. if (theArray[i] == x)
  94. {
  95. frequency++;
  96. }
  97. }
  98. return frequency;
  99. }
  100.  
  101. /* Question 3: ASSUMPTIONS because the validation rules were omitted.
  102.  * This classroom model accepts full years 2013-2026, halfYear 1 or 2,
  103.  * one county letter from C/D/G/L/T/W (either case), and sequence
  104.  * numbers 1-999999. These are chosen exercise rules, not a complete
  105.  * validator for actual Irish registration plates. Replace them with
  106.  * your instructor's rules. The year range is inclusive and does not
  107.  * attempt to check whether a registration is in the future.
  108.  */
  109. /* **************************************************
  110.  * Function: irishLicensePlateValidator
  111.  * Description: Checks the four fields against the exercise rules above.
  112.  * Parameters: year - full year; halfYear - half of year;
  113.  * countyCode - single county letter;
  114.  * sequenceNumber - registration sequence.
  115.  * Returns: 1 if every field is valid; otherwise 0.
  116.  * ************************************************** */
  117. int irishLicensePlateValidator(int year, int halfYear,
  118. char countyCode, int sequenceNumber)
  119. {
  120. if (year < 2013 || year > 2026)
  121. {
  122. return 0;
  123. }
  124. if (halfYear != 1 && halfYear != 2)
  125. {
  126. return 0;
  127. }
  128. if (sequenceNumber < 1 || sequenceNumber > 999999)
  129. {
  130. return 0;
  131. }
  132.  
  133. /* Accept either case for each supported county. */
  134. switch (countyCode)
  135. {
  136. case 'C': case 'c':
  137. case 'D': case 'd':
  138. case 'G': case 'g':
  139. case 'L': case 'l':
  140. case 'T': case 't':
  141. case 'W': case 'w':
  142. return 1;
  143. default:
  144. return 0;
  145. }
  146. }
  147.  
  148. /* Question 4 */
  149. /* **************************************************
  150.  * Function: minimumOfFour
  151.  * Description: Finds the minimum using conditional operators and AND.
  152.  * Parameters: value1, value2, value3, value4 - integers to compare.
  153.  * Returns: The smallest input, including when inputs are tied.
  154.  * ************************************************** */
  155. int minimumOfFour(int value1, int value2, int value3, int value4)
  156. {
  157. /* <= ensures tied minimum values are handled correctly. */
  158. int min4 = ((value1 <= value2) && (value1 <= value3) &&
  159. (value1 <= value4)) ? value1 :
  160. ((value2 <= value1) && (value2 <= value3) &&
  161. (value2 <= value4)) ? value2 :
  162. ((value3 <= value1) && (value3 <= value2) &&
  163. (value3 <= value4)) ? value3 : value4;
  164. return min4;
  165. }
  166.  
  167. /* Question 5 */
  168. /* **************************************************
  169.  * Function: calcLetterGrade
  170.  * Description: Converts a score using 90/80/70/60 grade thresholds.
  171.  * Parameters: score - integer percentage.
  172.  * Returns: A, B, C, D, or F; returns '?' for values outside 0-100.
  173.  * ************************************************** */
  174. char calcLetterGrade(int score)
  175. {
  176. char result;
  177.  
  178. /* Reject invalid scores before assigning a letter grade. */
  179. if (score < 0 || score > 100)
  180. result = '?';
  181. else if (score >= 90)
  182. result = 'A';
  183. else if (score >= 80)
  184. result = 'B';
  185. else if (score >= 70)
  186. result = 'C';
  187. else if (score >= 60)
  188. result = 'D';
  189. else
  190. result = 'F';
  191.  
  192. return result;
  193. }
  194.  
  195. /* Question 6: Preserve the member names supplied in the question.
  196.  * Assumed single-character codes: C, D, G, L, T, W.
  197.  */
  198. struct countyTotals
  199. {
  200. int totalCorkCodes;
  201. int totalDublinCodes;
  202. int totalGalwayCodes;
  203. int totalLimerickCodes;
  204. int totalTiperaryCodes;
  205. int totalWaterfordCodes;
  206. int totalInvalidCountryCodes;
  207. };
  208.  
  209. /* **************************************************
  210.  * Function: freqOfIrishCounties
  211.  * Description: Counts county letters without regard to case.
  212.  * Parameters: countyArrayList - array with at least size elements;
  213.  * size - nonnegative element count.
  214.  * Returns: Structure of six county counts and an invalid-code count.
  215.  * ************************************************** */
  216. struct countyTotals freqOfIrishCounties(char countyArrayList[], int size)
  217. {
  218. struct countyTotals myCountyTotals = {0, 0, 0, 0, 0, 0, 0};
  219.  
  220. for (int i = 0; i < size; i++)
  221. {
  222. switch (countyArrayList[i])
  223. {
  224. case 'C': case 'c':
  225. myCountyTotals.totalCorkCodes++;
  226. break;
  227. case 'D': case 'd':
  228. myCountyTotals.totalDublinCodes++;
  229. break;
  230. case 'G': case 'g':
  231. myCountyTotals.totalGalwayCodes++;
  232. break;
  233. case 'L': case 'l':
  234. myCountyTotals.totalLimerickCodes++;
  235. break;
  236. case 'T': case 't':
  237. myCountyTotals.totalTiperaryCodes++;
  238. break;
  239. case 'W': case 'w':
  240. myCountyTotals.totalWaterfordCodes++;
  241. break;
  242. default:
  243. myCountyTotals.totalInvalidCountryCodes++;
  244. break;
  245. }
  246. }
  247. return myCountyTotals;
  248. }
  249.  
  250. /* Question 7: Function prototypes */
  251. float toCelsius(int theFahrenheitTemp);
  252. float toFahrenheit(int theCelsiusTemp);
  253.  
  254. /* **************************************************
  255.  * Function: main
  256.  * Description: Prints both temperature tables in one-degree steps.
  257.  * Parameters: None.
  258.  * Returns: 0 on successful completion.
  259.  * ************************************************** */
  260. int main(void)
  261. {
  262. int i; /* Current temperature in the source scale. */
  263.  
  264. printf("Celsius to Fahrenheit\n");
  265. printf("%10s %12s\n", "Celsius", "Fahrenheit");
  266. printf("---------- ------------\n");
  267. for (i = 0; i <= 100; i++)
  268. {
  269. printf("%10d %12.2f\n", i, toFahrenheit(i));
  270. }
  271.  
  272. printf("\nFahrenheit to Celsius\n");
  273. printf("%10s %12s\n", "Fahrenheit", "Celsius");
  274. printf("---------- ------------\n");
  275. for (i = 32; i <= 212; i++)
  276. {
  277. printf("%10d %12.2f\n", i, toCelsius(i));
  278. }
  279. return 0;
  280. }
  281.  
  282. /* **************************************************
  283.  * Function: toCelsius
  284.  * Description: Converts Fahrenheit to Celsius.
  285.  * Parameters: theFahrenheitTemp - temperature in Fahrenheit.
  286.  * Returns: Corresponding Celsius temperature.
  287.  * ************************************************** */
  288. float toCelsius(int theFahrenheitTemp)
  289. {
  290. /* Floating-point division preserves the fractional part. */
  291. return (theFahrenheitTemp - 32.0f) * (5.0f / 9.0f);
  292. }
  293.  
  294. /* **************************************************
  295.  * Function: toFahrenheit
  296.  * Description: Converts Celsius to Fahrenheit.
  297.  * Parameters: theCelsiusTemp - temperature in Celsius.
  298.  * Returns: Corresponding Fahrenheit temperature.
  299.  * ************************************************** */
  300. float toFahrenheit(int theCelsiusTemp)
  301. {
  302. return theCelsiusTemp * (9.0f / 5.0f) + 32.0f;
  303. }
  304.  
  305. /* Question 8: The prompt does not specify the additional fields or
  306.  * required operations. The following is an illustrative completion
  307.  * using strings, nested structures, and the supplied 100-officer array.
  308.  * Confirm the required fields with your full question.
  309.  */
  310. struct date
  311. {
  312. int day;
  313. int month;
  314. int year;
  315. };
  316.  
  317. struct assignment
  318. {
  319. char shipName[51]; /* Up to 50 characters plus '\0'. */
  320. char department[31]; /* Up to 30 characters plus '\0'. */
  321. };
  322.  
  323. struct officer
  324. {
  325. char starFleetID[9]; /* Up to 8 characters plus '\0'. */
  326. struct date dateOfBirth;
  327. char firstName[31];
  328. char lastName[31];
  329. char rank[31];
  330. struct assignment currentAssignment;
  331. };
  332.  
  333. struct officer officers[100];
  334.  
  335. /* **************************************************
  336.  * Function: initializeExampleOfficer
  337.  * Description: Demonstrates assigning strings and nested members.
  338.  * Parameters: None; modifies the global officers array at index zero.
  339.  * Returns: Nothing.
  340.  * ************************************************** */
  341. void initializeExampleOfficer(void)
  342. {
  343. /* Each string literal fits its destination, including '\0'. */
  344. strcpy(officers[0].starFleetID, "SF000001");
  345. strcpy(officers[0].firstName, "Alex");
  346. strcpy(officers[0].lastName, "Morgan");
  347. strcpy(officers[0].rank, "Ensign");
  348. officers[0].dateOfBirth.day = 15;
  349. officers[0].dateOfBirth.month = 6;
  350. officers[0].dateOfBirth.year = 2300;
  351. strcpy(officers[0].currentAssignment.shipName, "Enterprise");
  352. strcpy(officers[0].currentAssignment.department, "Engineering");
  353. }
  354.  
  355. /* Question 9:
  356.  * Your Compiler/IDE: Ideone
  357.  *
  358.  * 1) I like that error messages show line numbers, making mistakes
  359.  * easier to find.
  360.  *
  361.  * 2) Compiling and running programs is simple because I can paste
  362.  * my C code and run it directly in the browser.
  363.  *
  364.  * 3) I dislike the limited debugging tools. I use printf statements
  365.  * to check variable values and find errors.
  366.  *
  367.  * 4) Entering input before running can be awkward when a program
  368.  * asks for several values.
  369.  */
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Celsius to Fahrenheit
   Celsius   Fahrenheit
---------- ------------
         0        32.00
         1        33.80
         2        35.60
         3        37.40
         4        39.20
         5        41.00
         6        42.80
         7        44.60
         8        46.40
         9        48.20
        10        50.00
        11        51.80
        12        53.60
        13        55.40
        14        57.20
        15        59.00
        16        60.80
        17        62.60
        18        64.40
        19        66.20
        20        68.00
        21        69.80
        22        71.60
        23        73.40
        24        75.20
        25        77.00
        26        78.80
        27        80.60
        28        82.40
        29        84.20
        30        86.00
        31        87.80
        32        89.60
        33        91.40
        34        93.20
        35        95.00
        36        96.80
        37        98.60
        38       100.40
        39       102.20
        40       104.00
        41       105.80
        42       107.60
        43       109.40
        44       111.20
        45       113.00
        46       114.80
        47       116.60
        48       118.40
        49       120.20
        50       122.00
        51       123.80
        52       125.60
        53       127.40
        54       129.20
        55       131.00
        56       132.80
        57       134.60
        58       136.40
        59       138.20
        60       140.00
        61       141.80
        62       143.60
        63       145.40
        64       147.20
        65       149.00
        66       150.80
        67       152.60
        68       154.40
        69       156.20
        70       158.00
        71       159.80
        72       161.60
        73       163.40
        74       165.20
        75       167.00
        76       168.80
        77       170.60
        78       172.40
        79       174.20
        80       176.00
        81       177.80
        82       179.60
        83       181.40
        84       183.20
        85       185.00
        86       186.80
        87       188.60
        88       190.40
        89       192.20
        90       194.00
        91       195.80
        92       197.60
        93       199.40
        94       201.20
        95       203.00
        96       204.80
        97       206.60
        98       208.40
        99       210.20
       100       212.00

Fahrenheit to Celsius
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