Advertisement
M4ritimeSeeker

PASS Week 3: Data types, Variables, and Input/Output

Sep 14th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. MYSTERY PROGRAM
  2.  
  3. /*
  4. * What is happening line-by-line in this program? And why does it work?
  5. * Particularly, what are the format specifiers in both printf statements doing?
  6. */
  7. #include <stdio.h>
  8.  
  9. int main(void)
  10. {
  11. int aNum = 32;
  12. char aChar = 'A';
  13. int aNumChar = aNum + aChar;
  14. printf("The numerical value of 'A' is %d\n", aChar);
  15. printf("10 + 'A' = %d and the character is '%c'\n", aNumChar, aNumChar);
  16. return 0;
  17. }//end main
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. PRACTICE PROBLEM 1
  32.  
  33. Write a program that prompts the user for an integer input.
  34. Then, print the integer input as a character. (DO NOT CHANGE THE ORIGINAL VARIABLE'S DATA TYPE)
  35. Then, on the next line, print it as a decimal integer.
  36. Lastly, on the next line, print it as a float.
  37. Now, looking at the ASCII chart, enter in decimal numbers that represent ASCII characters in the chart. Did you get the
  38. output you expected? https://www.asciitable.com/
  39.  
  40. /*** SKELETON CODE ***/
  41.  
  42. #include <stdio.h>
  43. int main(void)
  44. {
  45. int numInput = 0; //DON'T CHANGE THIS VARIABLE DECLARATION
  46.  
  47. // YOU START PROGRAMMING HERE
  48.  
  49. return 0;
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. PRACTICE PROBLEM 2
  70. Write a program that prints an output that resembles the following with all the correct
  71. spaces and characters (Also there are NO variables needed. Hard code this):
  72.  
  73. NO PARKING
  74. 2:00 - 6:00 a.m.
  75.  
  76. PERMITS ENFORCED
  77. 8:00 - 6:00 p.m.
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. PRACTICE PROBLEM 3
  96.  
  97. Write a program that uses DEFINED constants for each of the vowels: A, E, I, O, U.
  98. Then use MEMORY constants (The ones that use "const") inside the main function to hold the even values: 0, 2, 4, 6, 8.
  99.  
  100. Now provide 3 print statements to print a first line with the vowels, the second with the even
  101. values and then finally the third line should use literals to print the odd numbers 1-9.
  102. Match your format to the output below.
  103.  
  104. The objective here is to distinguish between a defined constants and memory constants and to practice using constants and
  105. variables.
  106.  
  107.  
  108. OUTPUT:
  109. A E I O U
  110. 0 2 4 6 8
  111. 1 3 5 7 9
  112.  
  113.  
  114.  
  115.  
  116. PRACTICE PROBLEM 3A
  117.  
  118. What is the difference between a defined constant and a global variable?
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. Problem #4: Using the sizeof() operator
  146.  
  147. Compose a program and in the local declarations area of the main function, declare and initialize the
  148. following items:
  149.  
  150. int var1 = 10;
  151. float var2 = 10.121;
  152. double var3 = 20.255;
  153. char var4 = ‘A’;
  154.  
  155. Now, in the local statements area use the printf() function and the sizeof() operator to output the
  156. number of bytes of memory each variable declared contains. Addtionally, in the same printf()
  157. statement also output the value of the variable using the correct conversion modifier (i.e. %d, %c, etc).
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. CHALLENGE PROBLEM
  177.  
  178. The scanf and printf functions don't just print and scan, they also return values. Notice we have not made use of them thus far.
  179. Return values from functions are optionally used. We will cover functions in Module 5. The
  180. scanf function will return the number of values that are successfully read or scanned in.
  181. This can be used to detect a failed data read. The printf function will return the number of
  182. characters processed to standard output.
  183.  
  184. Compose a program that declares 4 integer variables initialized to ‘0’. 3 of these will hold
  185. inputs and the 4th will hold the return value from the printf function call described below.
  186. Your program should use the printf function to show the number of conversions for a scanf
  187. call reading in the 3 integer values and assign the return value to the 4th variable. See the
  188. example below for using a function call as input to the printf() function call.
  189.  
  190. Helper code:
  191. X = printf(“Number read %d:”, scanf(“%d%d%d”, &a, &b, &c));
  192.  
  193. You can use simple variable names for this example program. Here is your algorithm.
  194. 1.) Prompt the user to input 3 numbers.
  195. 2.) Scan the input values.
  196. 3.) Output the number of items read/converted/scanned.
  197. 4.) Output the values read and the number of characters output in step 3.
  198.  
  199. Test the program:
  200. 1. Run the program by entering in 3 non decimal values and observe the results.
  201. 2. Run the program but enter in a decimal value for the first value. Observe the result.
  202. 3. Discuss the output and count the number of characters processed by the printf() statement.
  203. Are they correct and why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement