Download presentation
Presentation is loading. Please wait.
Published byCollin Park Modified over 6 years ago
1
Intermediate Programming using C++ Functions part 2
Armen Keshishian
2
Sending Data into a Function
When a function is called, the program may send values into the function. Example 1 (void function with input): write a program that receives a digit and shows its corresponding number in English using functions. Example 2 (value return function): write a program that receives two integers and shows the max number. Example 3: write a program that receives four integers and show the max number. Example 4: write a program that receives an integer and detects if it is a prime number.
3
More Example Write a program that receives two integers and then displays the menu below: 1) + 2) – 3) * 4) / 5) % 6) Avg 7) Max 8) Min 9) Exit Based on users input the program calls a function to do the calculation. After each execution, it clears the console window and shows this menu again until user inserts 9 for exit. If inserted number is out of range, it shows an appropriate message and lets the user try again.
4
Homework physicsHelper
Write a program that shows the menu below 1) Falling Distance Calculator 2) Kinetic Energy Calculator 3) Temperature Converter 4) Exit And asks user to select an item, based on selected number it displays: For 1: Displays: ”Insert the time: “ Uses a function to calculate the distance of falling object using this formula: 𝑑= 1 2 𝑔 𝑡 2 For 2: Displays: “Insert the mass: ” Displays: “Insert the velocity: “ Uses a function to calculate Kinetic energy by this formula: 𝐾𝐸= 1 2 𝑚 𝑣 2 For 3: Displays: “Insert the temperature in Fahrenheit: “ Uses a function to calculate the temperature in Celsius by this formula: 𝐶= (𝐹 −32) After each execution, it clears the console window and shows the menu again until user inserts 4 for exit. If inserted number is out of range, it shows an appropriate message and lets the user try again.
5
Extra credit (2%) Homework beltArea:
Write a program that calculates the area of a belt. Input parameters are as follow: float LBR: Left Big Radius float RBR: Right Big Radius float TH: Trapezius’s Height float Scale
6
beltArea: more details
7
Default values Optional vs. Required parameters
Default values should be in Prototypes Modifying an existing function Return_Data_Type functionName(parameter1_Data_Type param1,…, Default_parameter_Data_Type default_param_1 = default_value1,…);
8
Example: Write a function that receives year of birth and has a default value of current year, and returns user’s age at the given year. int yourAge(int yearOfBirth, int atYear = 2016); int main() { int yob = 1988; int yourAgeNow = yourAge(yob); int yourAgeAt2020 = yourAge(yob, 2020); int yourAgeAt2000 = yourAge(yob, 2000); cout << "your age right now is: " << yourAgeNow << endl; cout << "your age at 2020 will be: " << yourAgeAt2020 << endl; cout << "your age at 2000 was: " << yourAgeAt2000 << endl; system("pause"); return 0; } int yourAge(int yearOfBirth, int atYear) { return atYear - yearOfBirth;
9
Example: Shopping menu
Write a program that asks user’s age and based on that shows the menu of available items to buy. User can choose to not to insert his/her age. In that case program should show the same items for users younger than 18. age < 18, shopping menu: shoes, T-shirts, glasses 18 <= age < 21, shopping menu: all items above + beer 21 <= age, shopping menu: all items above + cigarette
10
void showMenu(int contentType = 1);
int main() { int age = 0; while (age >= 0) { char answer = 'n'; cout << "Do you want to insert your age?(y/n) "; cin >> answer; if (answer == 'y') { cout << "Please insert your age: "; cin >> age; if (age < 18) showMenu(); else if (age < 21) showMenu(2); else showMenu(3); } else showMenu(); system("pause"); return 0; void showMenu(int contentType) { cout << "1- Shoes" << endl; cout << "2- T-shirts" << endl; cout << "3- glasses" << endl; if (contentType > 1) cout << "4- Beer" << endl; if (contentType == 3) cout << "5- Cigarette" << endl; }
11
Example: modifying an existing function
Write a function for digit counter homework (week1). Modify the function to be able to count the number of digits of a negative number too without changing existing usages of the function.
12
int countDigits(int number, bool ignoreSign = false);
int main(){ int number = 0; cout << "Old Funcionality...Enter an integer: "; cin >> number; cout << "number of digits: " << countDigits(number) << endl; cout << " " << endl; cout << "Now you can count number of digits of a negative number! " << endl; cout << "Enter any number: "; cout << "number of digits: " << countDigits(number, true) << endl; system("pause"); return 0;} int countDigits(int number, bool ignoreSign){ int counter = 0; if (ignoreSign && number < 0) number *= -1; while (number > 0) { counter++; number /= 10; } return counter;
13
Homework: Present Value
How much money do you need to deposit today to make have specific amount after 10 years? You can use the following formula, which is known as the present value formula, to find out: 𝑃= 𝐹 (1+𝑟) 𝑛 Then terms in the formula are as follows: P is the present value, or the amount that you need to deposit today. F is the future value that you want in the account r is the annual interest rate n is the number of years that you plan to let the money sit in the account Write a program that has a function named presentValue that preforms this calculation.
14
Power function Rectangle Area
Write a function that raises a number to any given positive integer power. The function should receive a floating point as the base number and an integer as a power. By default function should square the number. Rectangle Area Write a function that calculates the area of a rectangle. The function should receive two floating numbers for length and width, if width is not provided the function should consider the rectangle as a square and calculate the area of the square.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.