Guest User

Untitled

a guest
Oct 7th, 2015
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. CSE1222 Programming & Algorithms for Engineers Autumn 2015
  2. Programming Assignment 3: Loops
  3. 1 Setting up the Programming Environment
  4. 1. Create a new directory (folder) called hw3 and move to that directory.
  5. 2. Copy the files from the directory /class/cse1222/9684/hw3 into the current directory
  6. by typing:
  7. cp /class/cse1222/9684/hw3/hour_glass_template.cpp hour_glass.cpp
  8. cp /class/cse1222/9684/hw3/hour_glass_solution.exe .
  9. cp /class/cse1222/9684/hw3/factors_solution.exe .
  10. 2 Work by Yourself
  11. All lab and programming assignments are to be done by yourself. You may discuss labs or
  12. assignments with other students in the class but DO NOT LOOK AT ANYONE’S CODE
  13. OTHER THAN YOUR OWN. Needless to say, you should not share or copy anyone else’s
  14. code.
  15. 3 Program Requirements
  16. Effective commenting and tabbing will affect your grade. The “style” of your program should
  17. follow the style of the sample programs in the course notes. Your program should have the
  18. file name, your name, creation and last modification dates and a brief description of the
  19. program in the comments at the top of the program. The declaration of every variable
  20. should have a comment.
  21. Write a program that prints an hour glass using the character ’#’. The user will enter
  22. the number of ’#’s on the top row and then the number of rows from the top to the middle
  23. row. For instance, an hour glass with 7 ’#’s in the top row and with 3 rows from the top
  24. row to the middle row looks like:
  25. #######
  26. #####
  27. ###
  28. #####
  29. #######
  30. 1
  31. No row is allowed to have less than two ’#’s. You will write code to check for this.
  32. Write a program that prompts for an integer greater than one that represents the low
  33. end of a range and a second integer greater than or equal to the first integer that represents
  34. the high end of a range. The program will print the factors of integers in this range in
  35. descending order.
  36. For example, if the user enters 2 for the low end of the range and 6 for the high end of
  37. the range, then the program will output:
  38. 6: 1, 2, 3, 6
  39. 5: 1, 5
  40. 4: 1, 2, 4
  41. 3: 1, 3
  42. 2: 1, 2
  43. For both problems, write your code incrementally. This means write only enough code
  44. to solve a particular step in the algorithms provided below and then compile and test that
  45. portion thoroughly before writing your solution to the next algorithmic step. Test your code
  46. on small input values first and handle boundary and error cases early in coding your solution.
  47. 1. Program hour_glass.cpp using the starter template provided:
  48. (a) Prompt and read in the number of ’#’s in the top row;
  49. (b) If the number of ’#’s is less than three, print the error message “Size of the top
  50. row must be at least three.”, and prompt again for the number of ’#’s in the top
  51. row. Repeat until the user enters a valid number. (Use a while loop.)
  52. (c) Prompt and read in the number of rows from the top row to the middle row.
  53. (d) If the number of rows is invalid then print ”Invalid number of rows.” and prompt
  54. again for the number rows. An invalid number of rows would be a number less
  55. than 1 or a number that leads to a row in the hour glass with less than two ’#’s.
  56. Repeat until the user enters a valid number of rows. (Use a while loop.)
  57. (e) Display an empty line.
  58. (f) Use for loops to display the hour glass. Your outer for loop will iterate over the
  59. number of rows times. For each row use one nested for loop to display blanks (the
  60. top row contains no blanks) and another nested for loop to display the characters
  61. ’#’.
  62. First, test your code on a top row size of 3 ’#’s and one row. Your program should
  63. display only one total row. Second, test your code on a top row size of 4 ’#’s and two
  64. rows. Continue testing your code for larger input values and combinations.
  65. Test your code on invalid input to ensure that only valid values are used when displaying
  66. the hour glass.
  67. 2
  68. 2. Program factors.cpp (no starter template provided):
  69. (a) Prompt and read in an integer value for the low end of the range. This value
  70. must be greater than one. Print the message ”Number must be greater than 1.”
  71. if an invalid value is input. Repeat this until a valid value is input.
  72. (b) Prompt and read in an integer value for the high end of the range. This value
  73. cannot be lower than the low end value. For example, if the user enters 5 for the
  74. low end value and 2 for the high end value then print the message ”Number must
  75. be greater or equal to 5.”. Repeat this until a valid value is input.
  76. (c) Use nested for loops to display all the factors of each integer from the high end
  77. to the low end in increasing order. The outer for loop will iterate from the high
  78. end to the low end integer and the nested for loop will display all the factors for
  79. a particular integer. An integer x is a factor of y if y mod x equals 0. Note that
  80. 1 and y are always (trivial) factors of y.
  81. 4 Sample Program Interaction
  82. Test your programs against the program hour_glass_solution.exe and factors_solution.exe.
  83. The input and output of your program should match the input and output of the solution.
  84. Test your programs thoroughly with many input values.
  85. 5 Program Submission
  86. Submit your files hour_glass.cpp and factors.cpp in the hw3 drop box on Carmen. DO
  87. NOT submit the file a.out.
  88. If you do not submit your program, you will receive zero credit for the homework.
  89. If your program does not compile and run you will receive zero credit for the homework.
  90. 3
Advertisement
Add Comment
Please, Sign In to add comment