Download presentation
1
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring” the function. float square (float x); In a function declaration, or prototype, specifying an identifier (parameter name) is optional. Thus, the following example is legal in a function declaration. float square (float); Writing the function body is “defining” the function float square (float x){ return x * x; }
2
Factorial Declare the prototype and define a function named factorial.
In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 0! is a special case that is explicitly defined to be 1. Here is the pseudo-code for one possible solution. Define variable f of type integer and initialize to 1 Prompt user for a number n Using a repetition statement calculate the factorial Display the message “factorial = ” followed by the factorial.
3
Declare the prototype and define a function named sum.
Sum of Numbers Declare the prototype and define a function named sum. In the main program, include a loop asking the user for a positive integer values. Use a negative number as a sentinel value. After each input call your function named sum, which returns the current total of the numbers entered. Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2nd Edition, by Tony Gaddis
4
Distance Traveled The distance a vehicle travels can be calculated as follows: distance = speed * time For example, if a train travels 40 mph for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles-per-hour) and how many hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled 40 80 120 Input Validation: Do not accept a negative number for speed and do not accept any value less than one for time traveled. Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2nd Edition, by Tony Gaddis
5
Pennies For Pay Write a program that calculates how much a person would earn over a period of time if his salary is one penny the first day, two pennies the second day, and continued to double each day. The program should ask the user for the number of days. Display a table showing how much the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Input Validation: Do not accept a number less than one for the number of days worked Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2nd Edition, by Tony Gaddis
6
Fibonacci Number Consider the following sequence of numbers:
1, 1, 2, 3, 5, 8, 13, 21, 34, …. Given the first two numbers of the sequence (say a1 and a2), the nth number an, n >= 3, of this sequence is given by an = an-1 + an-2 Thus a3 = a2 + a1 = 1 + 1 = 2, a4 = a3 + a2 = 1 + 2 = 3, and so on… Such a sequence is called a Fibonacchi sequence. Write a program that given any first two numbers, using this algorithm, determine the nth number an, n <= 3, of the sequence. Sample Run Enter the first two Fibonacci numbers -> The first two Fibonacci numbers are 12 and 16 Enter the desired Fibonacci number to be determined -> 10 The 10th Fibonacci number is 796. Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik
7
More Programming Exercises
An integer is divisible by 9 if the sum of its digits is divisible by 9. Write a program that prompts the user to input an integer. The program should then output the number and a message string stating whether the number is divisible by 9. It does so by first adding the digits and then checking whether the sum of the digits is divisible by 9. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as ; output the individual digits of 8030 as ; output the individual digits of as ; output the individual digits of 4000 as ; and output the individual digits of-2345 as Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be Your program must also output 5000 as 0005 and 980 as 098. Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is divisible by 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.