M4ritimeSeeker

PASS Week 4: Operators, Operands, and Precedence

Sep 20th, 2021 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. PROBLEM #1 (Precedence)
  2.  
  3. Given the expressions below, add parentheses to determine the order in which operations will be carried out in the
  4. expressions. Code these in a code editor and see if your predictions hold true!
  5. Use this link to see the C precedence chart:
  6. https://en.cppreference.com/w/c/language/operator_precedence
  7.  
  8. 1. 3 * 8 / 4 % 4 * 5
  9. 2. a += b *= c ‐= 5
  10. 3. true && false || false
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. PROBLEM #2 (Modulo)
  26.  
  27. Compose a program that prompts the user for an integer value and stores it in a variable.
  28. Then, use modulo to determine if the number is even or odd, printing the result to standard output.
  29. (If the remainder is 0, it is even. If the remainder is anything BUT 0, it is odd).
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. Problem #3 (More Modulo)
  46.  
  47. Compose a program using modulo to isolate the rightmost digit (least significant digit) of an integer.
  48. We want to use modulo to get rid of ALL digits but the value in the ones place.
  49. (Hint if we remove the tens place, we also remove anything above it, leaving the ones place value.)
  50.  
  51.  
  52. /** SKELETON CODE **/
  53.  
  54. #include <stdio.h>
  55.  
  56. int main(void)
  57. {
  58. //Local Declarations
  59. int intNum = 0;
  60. int lsDigit = 0;
  61.  
  62. //Local Statements
  63.  
  64. return 0;
  65. }//end main
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. PROBLEM #4 (Division & Yet More Modulo)
  79.  
  80. Compose a program that uses both division AND modulo to determine the
  81. quotient and remainder of two integer values provided by the user at runtime.
  82. Then print those two values.
  83.  
  84. /** SKELETON CODE **/
  85.  
  86. #include <stdio.h>
  87.  
  88. int main(void)
  89. {
  90. //Local declarations
  91. int intNum1 = 0;
  92. int intNum2 = 0;
  93. int intCalc = 0;
  94.  
  95. //Local Statements
  96. printf("Supply two integral values: ");
  97. //Scanf here
  98.  
  99. //CALCULATE QUOTIENT HERE
  100.  
  101. //CALCULATE ANY REMAINDER HERE
  102.  
  103. return 0;
  104. }//end main
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. PROBLEM #5 (Shifty Business)
  123.  
  124. Compose a program that declares a defined constant value of 9876543201 called ALONGNBR.
  125. Now you will manipulate the value to accomplish shifting to the right,
  126. the left and to return the rightmost values, printing them to output.
  127.  
  128. You will make a printf() statement that will use the division operator to shift the defined number to the right 4
  129. places, effectively truncating the "3201" values.
  130.  
  131. You will then make a second printf() statement that should use the
  132. modulo operator to return the 2 rightmost values of the original number.
  133.  
  134. Lastly you will make a printf() statement that should use the
  135. multiplication operator to shift the original number two places to the left.
  136. When you print out the last statement your format modifier should use %lld for a
  137. long long integer.
  138.  
  139.  
  140. SAMPLE OUTPUT:
  141.  
  142. Shift 4 places to the left: 987654
  143. Return 2 rightmost numbers: 1
  144. Shift 2 places to the right: 987654320100
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. PROBLEM #6 (Tired of Modulo yet?)
  164.  
  165. Compose a program that will take a floating point number as input (float or double works, just keep it consistent)
  166. and will round the number to the hundredth place of accuracy, rounding up. Sample output below.
  167.  
  168. So if the user enters in the value 2.345,
  169. the program will round the number to 2.35 and output it to the screen.
  170. YOU WILL NEED TO USE SHIFTING (i.e. similar to problem #5) TO DO THIS.
  171.  
  172. Hint: The solution involves moving the decimal point to the right,
  173. performing a conversion (cast) to int, then shifting the number
  174. back to the right and converting it back to a floating point data type.
  175.  
  176.  
  177. SAMPLE OUTPUT:
  178.  
  179. Enter in a floating point value with at least 3 decimal places.
  180. 2.325
  181. The number is now: 2.330000
  182.  
  183.  
  184.  
  185.  
  186.  
  187. NOTE: Printing a value to screen can round the output as its presented, but the actual value in
  188. memory that is being used for math operations is not rounded, which can cause
  189. errors hidden by the rounded output.
Add Comment
Please, Sign In to add comment