Homework #2:Functions and Arrays By J. H. Wang Mar. 24, 2014
Programming Exercises • Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time and an end time and calculate the difference in minutes between them, The end time will always be within 24 hours of the start time. Use military notation for both the start and end time (e.g., 0000 for midnight and 2359 for one minute before midnight).[… to be continued on the next slide…]
[… continued from the previous slide …] Write a function that takes as input a start time and an end time represented as an int, using military notation. The function should return the difference in minutes as an integer. Write a C++ program that calls your function with times entered by the user. (Hint: Be careful of time intervals that start before midnight and end the following day.)
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 to 1, a to 2, a to 3, and a to 4. However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a, 3 in a, 2 in a, and 1 in a.[… to be continued on the next slide …]
[… 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 …]
[… 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 C++ 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 …]
[… 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.
Use a one-dimensional array to solve the following problem. Write a function to check if a given array of integers contains a duplicate with other numbers in the array. Then, the function should store only the unique values into another array as an array parameter. Also, the number of unique values has to be returned via a call-by-reference parameter. (a) Write a C++ program that allows the user to test the function, and display the output of the array after duplicate elimination. (b) Suppose the integers represent scores between 0 and 100, inclusive. Please design a more efficient function to do the duplicate elimination, and test the function in your program.
Homework Submission • Due: 2 weeks (Apr. 7, 2014) • Submission instructions: • http://mslin.ee.ntut.edu.tw/ • File names: Please name your file according to our homework. For example, <id>_HW1.zip or <id>_quiz2.rar