1 / 7

11.3 Function Prototypes

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);

Download Presentation

11.3 Function Prototypes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 1Prompt user for a number nUsing a repetition statement calculate the factorialDisplay the message “factorial = ” followed by the factorial.

  3. 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 lessthan 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 -> 12 16 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 3 4 5 6; output the individual digits of 8030 as 8 0 3 0; output the individual digits of 2345526 as 2 3 4 5 5 2 6; output the individual digits of 4000 as 4 0 0 0; and output the individual digits of-2345 as 2 3 4 5 . • 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 54321.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

More Related