Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.

Similar presentations


Presentation on theme: "Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012."— Presentation transcript:

1 Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012

2 Programming Exercises 1.Write a C++ program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. There should be at least three functions: one for input, one to do the conversion, and one for output. Record the A.M./P.M. information as a value of type char, ‘A’ for A.M. and ‘P’ for P.M. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is A.M. or P.M. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.

3 2.An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a[0], 3 in a[1], 2 in a[2], and 1 in a[3]. [… to be continued on the next slide …]

4 [… continued from the previous slide …] In this exercise you will write a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of two integers. You program will read the digits as values of type char so that the number 1234 is read as the four characters ‘1’, ‘2’, ‘3’, and ‘4’. After they are read into the program, the characters are changed to values of type int. [… to be continued on the next slide …]

5 [… continued from the previous slide …] The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elements in the array after the array is filled with data from the keyboard. Your program will perform the addition by implementing the usual paper-and-pencil addition algorithm. The result of the addition is stored in an array of size 20 and the result is then written to the screen. [… to be continued on the next slide …]

6 [… continued from the previous slide …] If the result is an integer with more than the maximum number of digits (that is, 20), then your program should issue a message saying that it has encountered “integer overflow.” You should be able to change the maximum length of integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additions until the user says the program should end.

7 3.The birthday paradox is that there is a surprisingly high probability that two people in the same room happen to share the same birthday. By birthday, we mean the same day of the year (ignoring leap years), but not the exact birthday including the birth year or time of day. Write a C++ program that approximates the probability that two people in the same room have the same birthday, for 2 to 50 people in the room. […to be continued on the next slide…]

8 [… continued from the previous slide …] The program should use simulation to approximate the answer. Over many trials (say, 5,000), randomly assign birthdays to everyone in the room. Count up the number of times at least two people have the same birthday, and then divide by the number of trials to get an estimated probability that two people share the same birthday for a given room size. […to be continued on the next slide…]

9 [… continued from the previous slide …] Your output should look like the following. It won’t be exactly the same due to the random numbers: For 2 people, the probability of two birthdays is about 0.002 For 3 people, the probability of two birthdays is about 0.0082 For 4 people, the probability of two birthdays is about 0.0163 … For 49 people, the probability of two birthdays is about 0.9654 For 50 people, the probability of two birthdays is about 0.969

10 Homework Submission Due: 2 weeks (Apr. 3, 2012) Submission instructions: –http://140.124.183.39/oop/http://140.124.183.39/oop/ – File names : Please name your file according to our homework. For example, HW1.zip or quiz2.rar

11 Questions or Comments?


Download ppt "Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012."

Similar presentations


Ads by Google