M4ritimeSeeker

PASS Week 7: Nested Loops

Feb 28th, 2022 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. PROBLEM #1: Making Coordinates
  2.  
  3. Construct a program that uses two nested for loops to produce 10 lines of 10
  4. integers that shows the row and column position (think like the game Battleship).
  5. The first loop represents the ROWS and the nested loop represents each COLUMN in a row.
  6. The first number is the row and the second is the column position.
  7. Your output should look something like this:
  8.  
  9. [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9]
  10. [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9]
  11. [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9]
  12. [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9]
  13. [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,7] [4,8] [4,9]
  14. [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9]
  15. [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9]
  16. [7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9]
  17. [8,0] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9]
  18. [9,0] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9]
  19.  
  20. NOTE: You could start looping at 1 instead of 0. People don’t usually refer to column 1 as
  21. column zero!
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. PROBLEM #2: Printing Asterisks
  39.  
  40. Create a program using nested loops that creates the output shown.
  41. That is 20 columns of the ‘*’ character and 4 rows. You may prompt the user to enter values for the
  42. height and width to create other shapes.
  43.  
  44.  
  45. SAMPLE OUTPUT:
  46.  
  47. Enter in the number of rows/height you want: 4
  48. Enter in the number of columns/width you want: 20
  49.  
  50. ********************
  51. ********************
  52. ********************
  53. ********************
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. ADDITIONAL PROBLEM #2.5: Correcting an Incorrect Program
  79.  
  80. The program below is supposed to print this output:
  81.  
  82. *
  83. * *
  84. * * *
  85. * * * *
  86. * * * * *
  87.  
  88. However, the program has a LOGIC error preventing it from printing correctly.
  89. Find and fix the errors. THERE IS ONLY 1 ERROR.
  90.  
  91.  
  92. BROKEN CODE:
  93.  
  94. #include<stdio.h>
  95.  
  96. int main()
  97. {
  98. // outer for loop to represent rows
  99. for(int i = 1; i <= 5; i++)
  100. {
  101. // inner for loop to represent columns
  102. for(int j = i; j <= i;j++)
  103. {
  104. printf("* ");
  105. }
  106.  
  107. // new line
  108. printf("\n");
  109. }
  110.  
  111. return 0;
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. PROBLEM #3: A Weird * * Problem
  125.  
  126. Create a program using nested loops that creates the output shown. Hint: Make use of
  127. if/else statements and nested loops based on conditions. You are printing spaces, newlines and two
  128. different characters, so you may want to write down an algorithm before coding this.
  129. You may prompt the user to enter values for the height and width to create other shapes.
  130.  
  131. SAMPLE OUTPUT:
  132.  
  133. Enter in the number of rows you want: 7
  134. Enter in the number of columns you want: 13
  135.  
  136. =============
  137. * *
  138. * *
  139. * *
  140. * *
  141. * *
  142. =============
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. PROBLEM #4 Using Break & Continue
  164.  
  165. THIS PROBLEM HAS NO NESTED LOOPS
  166.  
  167. Using break and continue, create a program with an loop that will count up from 0.
  168. YOUR LOOP MUST BE while(1)
  169. The numerical value of each loop iteration should be checked to see if it is an even number.
  170. If it is odd, print to screen that is odd, and if it is even increment
  171. a counter to keep track of the number of even numbers. When the loop reaches it’s 50th
  172. iteration, the loop should stop using the break statement. You may not use a test case in the
  173. loop test to determine when to stop. You must use a counter in the body of the loop to
  174. keep track of the number of loops. NOTE: This is just an exercise in the use of break and
  175. continue.
  176.  
  177. SAMPLE OUTPUT:
  178.  
  179. 1 is odd.
  180. 3 is odd.
  181. 5 is odd.
  182. 7 is odd.
  183. 9 is odd.
  184. 11 is odd.
  185. 13 is odd.
  186. 15 is odd.
  187. 17 is odd.
  188. 19 is odd.
  189. 21 is odd.
  190. 23 is odd.
  191. 25 is odd.
  192. 27 is odd.
  193. 29 is odd.
  194. 31 is odd.
  195. 33 is odd.
  196. 35 is odd.
  197. 37 is odd.
  198. 39 is odd.
  199. 41 is odd.
  200. 43 is odd.
  201. 45 is odd.
  202. 47 is odd.
  203. 49 is odd.
  204. The number of evens in 50 numbers is 25
Add Comment
Please, Sign In to add comment