Intermediate Programming using C++ Functions part 2

Slides:



Advertisements
Similar presentations
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Advertisements

Computer Science 1620 Loops.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
CONTROLLING PROGRAM FLOW
Fundamental Programming: Fundamental Programming Introduction to C++
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Previously Repetition Structures While, Do-While, For.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Midterm: Question 1 (35%) (30 minutes) Write an assembly program to draw a rectangle. – (5%) Show a message to ask for information of the rectangle – (5%)
Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Basic concepts of C++ Presented by Prof. Satyajit De
TK1913 C++ Programming Basic Elements of C++.
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
Repetitive Structures
Dr. Shady Yehia Elmashad
MT262A Review.
REPETITION CONTROL STRUCTURE
while Repetition Structure
C++ Programming: CS150 For.
Computing Fundamentals
Array An “average.cpp” program
Chapter 4 Loops DDC 2133 Programming II.
CO1401 Programming Design and Implementation
Arrays Part-1 Armen Keshishian.
Chapter 5. Looping.
Multi-dimensional Array
Chapter 2.2 Control Structures (Iteration)
Dr. Shady Yehia Elmashad
Control Statement Examples
Dr. Shady Yehia Elmashad
While Loops.
Repetition Statements
TOPIC 4: REPETITION CONTROL STRUCTURE
Lab 1 Introduction to C++.
Compound Assignment Operators in C++
Today in CS161 Week #3 Learn about… Writing our First Program
Additional Control Structures
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
Today in CS161 Week #5 **weekly visit D2L** Practicing…
Chapter 7 Conditional Statements
Chapter 2.2 Control Structures (Iteration)
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
3. Decision Structures Rocky K. C. Chang 19 September 2018
National University of Computer & Emerging Sciences
Life is Full of Alternatives
Objectives You should be able to describe: The while Statement
Fundamental Programming
Today in CS161 Week #5 **go to Blackboard ** Practicing…
Summary of what we learned yesterday
Life is Full of Alternatives
Selection Control Structure
Programming Fundamental
Presentation transcript:

Intermediate Programming using C++ Functions part 2 Armen Keshishian

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.

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.

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: 𝐶= 5 9 (𝐹 −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.

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

beltArea: more details

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

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;

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

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

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.

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;

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.

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.