* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Ex 1 - gmitWEB
Large numbers wikipedia , lookup
Abuse of notation wikipedia , lookup
Big O notation wikipedia , lookup
Functional decomposition wikipedia , lookup
Continuous function wikipedia , lookup
Non-standard calculus wikipedia , lookup
Proofs of Fermat's little theorem wikipedia , lookup
Dirac delta function wikipedia , lookup
Elementary mathematics wikipedia , lookup
Function of several real variables wikipedia , lookup
GMIT Programming and Coding Functions Exercise #1 Write a function named toSeconds which takes three integer parameters, a number of hours, number of minutes, and number of seconds and returns the total number of seconds. The prototype should look like. int toSeconds(int hours, int minutes, int seconds); As always, use good indentation and meaningful identifier names. For example, cout << toSeconds(0, 2, 15); would print 135, the total number of seconds in 0 hours, 2 minutes and 15 seconds. Exercise #2 Given this main program: int main() { float temperature; while (cin >> temperature) { printTempOpinion(temperature); } return 0; } Write the function printTempOpinion which prints "Cold" on cout if the temperature is below 70, "OK" if the temperature is in the range 70-80, and "Hot" if the temperature is above 80. Exercise #3 Write a function named toMeters which takes two float parameters, a number of feet and a number of inches and returns the floating point number of equivalent meters. Assume there are 2.54 centimeters (0.0254 meters) in one inch. Write only the function. Sample Usage cout << toMeters(5, 11); This would print 1.8034, the number of meters in 5 feet, 11 inches. Exercise #4 Write a function named average3 which returns the average of its three floating-point parameters. As always, use good indentation. You do not need to write the main program. Write only the function. For example, cout << average3(7, 2, 6); This would print 5.0, which is (7+2+6)/3. GMIT Programming and Coding Functions Exercise #5 Write a function named promptYN which takes one string parameter. It prints the string (which is a question the caller supplies) on cout, then prompts the user to enter Y or N. It then reads one character from cin. If the character is a 'Y', the function returns true, if the character is a 'N', it returns false, if the characer is neither of the above, your function should prompt the user again for a 'Y' or 'N' and read another character, continuing with this until a Y or N is entered. The prototype should be the following. bool promptYN(string question); Sample Usage For example, while (promptYN("Do you want to continue")) . . . would continue executing the loop as long as the user answered Y. Exercise #6 Write a function integerpower ( base, exponent ) that returns the value of: base exponent For example, integerpower ( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero integer and that base is an integer. The function integerpower should use for or while loops to control the calculation. Do not use any math library functions. Write a program that calls the function and prints a table of powers (squared and cubed) for the intergers 1 to 10. Number Squared Cubed 1 1 1 2 4 8 3 9 27 ... Exercise #7 Define a function hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. Use this function in a program to determine the length of the hypotenuse for each of the triangles shown below. The function should take two double arguments and return the hypotenuse as a double. Triangle Side 1 Side 2 1 3.0 4.0 2 3 5.0 8.0 12.0 15.0 GMIT Programming and Coding Functions Exercise #8 Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return true if the second is a multiple of the first, false otherwise. Use this function in a program that inputs a series of pairs of integers. Exercise #9 Write a program that inputs a series of integers and passes them one at a time to function even, which uses the modulus operator to determine whether an integer is even. The function should take an integer argument and return true if the integer is even and false otherwise. Exercise #10 Write a function that displays at the left margin of the screen a solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays the following: **** **** **** **** Exercise #11 Modify the function created in Exercise 10 to form the square out of whatever character is contained in character parameter fillCharacter. Thus, if side is 5 and fillCharacter is "#," then this function should print the following: ##### ##### ##### ##### ##### GMIT Programming and Coding Functions Exercise #12 Write program segments that accomplish each of the following: a) Calculate the integer part of the quotient when integer a is divided by integer b. b) Calculate the integer remainder when integer a is divided by integer b. c) Use the program pieces developed in (a) and (b) to write a function that inputs an integer between 1 and 32767 and prints it as a series of digits, each pair of which is separated by two spaces. For example, the integer 4562 should print as follows: 4 5 6 2 Exercise #13 Implement the following integer functions: a) Function Celsius returns the Celsius equivalent of a Fahrenheit temperature. b) Function Fahrenheit returns the Fahrenheit equivalent of a Celsius temperature. c) Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable. Exercise #14 Write a program that inputs three double-precision, floating-point numbers and passes them to a function that returns the smallest number. Exercise #15 An integer is said to be prime if it is divisible by only I and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. a) Write a function that determines whether a number is prime. b) Use this function in a program that determines and prints all the prime numbers between 2 and 10,000. How many of these numbers do you really have to test before being sure that you have found all the primes?