fork download
  1. /***************************************************** * COMPLETE SINGLE FILE — ALL QUESTIONS 1 THROUGH 8 * *****************************************************/
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. /*********************** * 1) REQUIRED MACROS * ***********************/
  7. /* The original macros were incomplete; the names were not followed by parentheses in the definition. */
  8. #define TRIANGLE_PERIMETER(a, b, c) ((a) + (b) + (c))
  9. #define RECTANGLE_AREA(l, w) ((l) * (w))
  10. #define CIRCLE_AREA(r) (3.14159265 * (r) * (r))
  11. #define C_TO_F(c) ((c) * 9.0/5.0 + 32)
  12. #define MAX2(x, y) (((x) > (y)) ? (x) : (y))
  13. /* SWAP should be a statement expression or put in a do-while loop for safety in all contexts */
  14. #define SWAP(a, b, temp) do { temp = a; a = b; b = temp; } while (0)
  15.  
  16. /*************************************** * 2) FUNCTION: euroTotal() * ***************************************/
  17. float euroTotal(int c1, int c2, int c5, int c10, int c20, int c50, int e1, int e2)
  18. {
  19. float total = 0.0;
  20. total += c1 * 0.01;
  21. total += c2 * 0.02;
  22. total += c5 * 0.05;
  23. total += c10 * 0.10;
  24. total += c20 * 0.20;
  25. total += c50 * 0.50;
  26. total += e1 * 1.00;
  27. total += e2 * 2.00;
  28. return total;
  29. }
  30.  
  31. /********************************************** * 3) FUNCTION: blackjackHandTotal() * **********************************************/
  32. /* The original code had syntax errors (returning strings/non-ints, missing braces). */
  33. int blackjackHandTotal(char card1, char card2)
  34. {
  35. int value1, value2;
  36.  
  37. /* Process card1 value */
  38. if (card1 >= '2' && card1 <= '9') {
  39. value1 = card1 - '0';
  40. } else if (card1 == 'T' || card1 == 'K' || card1 == 'Q' || card1 == 'J') {
  41. value1 = 10;
  42. } else if (card1 == 'A') {
  43. value1 = 11;
  44. } else {
  45. return -1; /* Signal an illegal card */
  46. }
  47.  
  48. /* Process card2 value */
  49. if (card2 >= '2' && card2 <= '9') {
  50. value2 = card2 - '0';
  51. } else if (card2 == 'T' || card2 == 'K' || card2 == 'Q' || card2 == 'J') {
  52. value2 = 10;
  53. } else if (card2 == 'A') {
  54. value2 = 11;
  55. } else {
  56. return -1; /* Signal an illegal card */
  57. }
  58.  
  59. int total = value1 + value2;
  60.  
  61. /* Handle the 'Two Aces' case where 11+11 = 22, should be 12 (11+1) */
  62. if (total == 22) {
  63. total = 12;
  64. }
  65.  
  66. return total;
  67. }
  68.  
  69. /******************************** * 4) FUNCTION: isLegal() * ********************************/
  70. /* Function to check if a single character is a legal card representation (A, 2-9, T, J, Q, K) */
  71. int isLegal(char card) {
  72. /* Convert to uppercase to handle 'a' through 'k' as well */
  73. char upper_card = toupper((unsigned char)card);
  74.  
  75. if ((upper_card >= '2' && upper_card <= '9') ||
  76. upper_card == 'T' ||
  77. upper_card == 'J' ||
  78. upper_card == 'Q' ||
  79. upper_card == 'K' ||
  80. upper_card == 'A') {
  81. return 1; /* True, it is legal */
  82. } else {
  83. return 0; /* False, it is not legal */
  84. }
  85. }
  86.  
  87. /************************************** * 5) FUNCTION: arrayAvg() * **************************************/
  88. /* Function to calculate the average of an integer array */
  89. float arrayAvg(int arr[], int size) {
  90. if (size <= 0) {
  91. return 0.0; /* Prevent division by zero */
  92. }
  93. long sum = 0; /* Use long to prevent potential overflow of sum */
  94. for (int i = 0; i < size; i++) {
  95. sum += arr[i];
  96. }
  97. return (float)sum / size; /* Cast sum to float for division */
  98. }
  99.  
  100. /***************************************** * 6) FUNCTION: revString() * *****************************************/
  101. /* Function to reverse a string in place */
  102. void revString(char str[]) {
  103. int len = strlen(str);
  104. int start = 0;
  105. int end = len - 1;
  106. char temp;
  107.  
  108. while (start < end) {
  109. /* Use the SWAP macro defined in Q1 */
  110. SWAP(str[start], str[end], temp);
  111. start++;
  112. end--;
  113. }
  114. }
  115.  
  116. /***************************************** * 7) FUNCTION: checkPalindrome() * *****************************************/
  117. /* Function to check if a string is a palindrome (ignoring case and non-alphanumeric characters) */
  118. int checkPalindrome(const char original[]) {
  119. char processed[100]; /* Assuming max 100 useful chars after processing, adjust size as necessary */
  120. int j = 0;
  121.  
  122. /* 1. Process the string: only keep letters/numbers and convert to lowercase */
  123. for (int i = 0; original[i] != '\0'; i++) {
  124. if (isalnum((unsigned char)original[i])) {
  125. processed[j++] = tolower((unsigned char)original[i]);
  126. }
  127. }
  128. processed[j] = '\0'; /* Null-terminate the new string */
  129.  
  130. /* 2. Check if the processed string is the same forwards and backwards */
  131. int len = strlen(processed);
  132. for (int i = 0; i < len / 2; i++) {
  133. if (processed[i] != processed[len - 1 - i]) {
  134. return 0; /* Not a palindrome */
  135. }
  136. }
  137.  
  138. return 1; /* Is a palindrome */
  139. }
  140.  
  141. /*********************************** * 8) MAIN FUNCTION AND TESTING * ***********************************/
  142. int main() {
  143. printf("--- 1) MACRO TESTS ---\n");
  144. float r = 5.0;
  145. int l = 10, w = 5;
  146. int a_swap = 10, b_swap = 20, temp_swap;
  147. printf("Circle Area (r=%.1f): %.2f\n", r, CIRCLE_AREA(r));
  148. printf("Rectangle Area (l=%d, w=%d): %d\n", l, w, RECTANGLE_AREA(l, w));
  149. printf("C to F (0C): %.1fF\n", C_TO_F(0));
  150. printf("Max of (10, 20): %d\n", MAX2(10, 20));
  151. SWAP(a_swap, b_swap, temp_swap);
  152. printf("After SWAP, a_swap is %d, b_swap is %d\n", a_swap, b_swap);
  153.  
  154. printf("\n--- 2) euroTotal() TEST ---\n");
  155. float total_euros = euroTotal(1, 1, 1, 1, 1, 1, 1, 1);
  156. printf("Total value of one of each coin/euro: %.2f EUR\n", total_euros);
  157.  
  158. printf("\n--- 3) blackjackHandTotal() TESTS ---\n");
  159. printf("Hand (A, A) total: %d\n", blackjackHandTotal('A', 'A'));
  160. printf("Hand (K, 5) total: %d\n", blackjackHandTotal('K', '5'));
  161. printf("Hand (T, J) total: %d\n", blackjackHandTotal('T', 'J'));
  162.  
  163. printf("\n--- 4) isLegal() TESTS ---\n");
  164. printf("'K' is legal: %d\n", isLegal('K')); // 1
  165. printf("'Z' is legal: %d\n", isLegal('Z')); // 0
  166.  
  167. printf("\n--- 5) arrayAvg() TEST ---\n");
  168. int scores[] = {90, 85, 100, 75};
  169. int num_scores = sizeof(scores) / sizeof(scores[0]);
  170. printf("Average of scores: %.2f\n", arrayAvg(scores, num_scores));
  171.  
  172. printf("\n--- 6) revString() TEST ---\n");
  173. char s6[] = "Hello World";
  174. printf("Original string: %s\n", s6);
  175. revString(s6);
  176. printf("Reversed string: %s\n", s6);
  177.  
  178. printf("\n--- 7) checkPalindrome() TESTS ---\n");
  179. printf("'A man, a plan, a canal, Panama' is palindrome: %d\n", checkPalindrome("A man, a plan, a canal, Panama")); // 1
  180. printf("'Hello World' is palindrome: %d\n", checkPalindrome("Hello World")); // 0
  181. printf("'Racecar!' is palindrome: %d\n", checkPalindrome("Racecar!")); // 1
  182.  
  183. return 0;
  184. }
  185.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
--- 1) MACRO TESTS ---
Circle Area (r=5.0): 78.54
Rectangle Area (l=10, w=5): 50
C to F (0C): 32.0F
Max of (10, 20): 20
After SWAP, a_swap is 20, b_swap is 10

--- 2) euroTotal() TEST ---
Total value of one of each coin/euro: 3.88 EUR

--- 3) blackjackHandTotal() TESTS ---
Hand (A, A) total: 12
Hand (K, 5) total: 15
Hand (T, J) total: 20

--- 4) isLegal() TESTS ---
'K' is legal: 1
'Z' is legal: 0

--- 5) arrayAvg() TEST ---
Average of scores: 87.50

--- 6) revString() TEST ---
Original string: Hello World
Reversed string: dlroW olleH

--- 7) checkPalindrome() TESTS ---
'A man, a plan, a canal, Panama' is palindrome: 1
'Hello World' is palindrome: 0
'Racecar!' is palindrome: 1