Advertisement
M4ritimeSeeker

PASS Week 9/8

Sep 8th, 2020 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. WARM-UP PROGRAM
  2.  
  3. /*
  4. * This is a simple example of how characters are treated similarly to
  5. * integer values and demonstrate you can use math operations on them.
  6. * By: Larry Snedden
  7. */
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. int aNum = 32;
  13. char aChar = 'A';
  14. int aNumChar = aNum + aChar;
  15. printf("The numerical value of 'A' is %d\n", aChar);
  16. printf("The value of 10 + 'A' = %d and the character is '%c'\n", aNumChar, aNumChar);
  17. return 0;
  18. }//end main
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. PRACTICE PROBLEM 1
  28.  
  29. Write a program that prompts the user for an integer input and then prints the integer
  30. input first as a character and then as a decimal integer and then as a float. Now looking at
  31. the ASCII chart enter in numbers that represent ASCII values in the chart. Did you get the
  32. output you expected? https://www.asciitable.com/
  33.  
  34. /*** SKELETON CODE ***/
  35.  
  36. #include <stdio.h>
  37. int main(void)
  38. {
  39. int numInput = 0; //DON'T CHANGE THIS VARIABLE DECLARATION
  40.  
  41. // YOU START PROGRAMMING HERE
  42.  
  43. return 0;
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. PRACTICE PROBLEM 2
  56.  
  57. Write a program using only printf statements that will print a right arrow. There should be
  58. 5 rows and 7 columns and the characters should be an ‘X’ (you can experiment). Use new
  59. lines ‘\n’ to provide empty lines where needed to improve appearance. Remember that a ‘ ‘
  60. is a character with a fixed width just like ‘X’.
  61. Output:
  62. X
  63. XXXXXX
  64. XXXXXXX
  65. XXXXXX
  66. X
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. PRACTICE PROBLEM 3
  86.  
  87. Write a program that uses defined constants for each of the vowels: A, E, I, O, U. now use
  88. memory constants inside the main function to hold the even values: 0, 2, 4, 6, 8. Now
  89. provide 3 print statements to print a first line with the vowels, the second with the even
  90. values and then finally the third line should use literals to print the odd numbers under the
  91. positionally equivalent even numbers as shown. The objective here is to distinguish
  92. between a defined constants and memory constants and to practice using constants and
  93. variables. It should also be noted that a defined constant is not the same thing as a global
  94. variable which is a big “no no” in programming.
  95.  
  96. Output:
  97. A E I O U
  98. 0 2 4 6 8
  99. 1 3 5 7 9
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. Problem #4: Using the sizeof() operator
  119.  
  120. Compose a program and in the local declarations area of the main function declare and initialize the
  121. following items:
  122.  
  123. int var1 = 10;
  124. float var2 = 10.121;
  125. double var3 = 20.255;
  126. char var4 = ‘A’;
  127.  
  128. Now, in the local statements area use the printf() function and the sizeof() operator to output the
  129. number of bytes of memory each variable declared contains. Addtionally, in the same printf()
  130. statement also output the value of the variable using the correct conversion modifier (i.e. %d, %c, etc) as well as
  131. using a minimum width modifier of 5.2. Observe the output for alignment and rounding.
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. CHALLENGE PROBLEM
  151.  
  152. The scanf and printf functions return values. Notice we have not made use of them thus far.
  153. Return values from functions are optionally used. We will cover functions in Module 5. The
  154. scanf function will return the number of values that are successfully read or scanned in.
  155. This can be used to detect a failed data read. The printf function will return the number of
  156. characters processed to standard output.
  157.  
  158. Compose a program that declares 4 integer variables initialized to ‘0’. 3 of these will hold
  159. inputs and the 4th will hold the return value from the printf function call described below.
  160. Your program should use the printf function to show the number of conversions for a scanf
  161. call reading in the 3 integer values and assign the return value to the 4th variable. See the
  162. example below for using a function call as input to the printf() function call.
  163.  
  164. Helper code:
  165. X = printf(“Number read %d:”, scanf(“%d%d%d”, &a, &b, &c));
  166.  
  167. You can use simple variable names for this example program. Here is your algorithm.
  168. 1.) Prompt the user to input 3 numbers.
  169. 2.) Store the input values.
  170. 3.) Output the number of items read/converted/scanned.
  171. 4.) Output the values read and the number of characters output in step 3.
  172.  
  173. Test the program:
  174. 1. Run the program by entering in 3 non decimal values and observe the results.
  175. 2. Run the program but enter in a decimal value for the first value. Observe the result.
  176. 3. Discuss the output and count the number of characters processed by the printf() statement.
  177. Are they correct and why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement