functions & strings cis 230 08-feb-06. quiz 1.write a function prototype for the function...
DESCRIPTION
Function: Return Type Data type hierarchy: long double double float unsigned long int long int unsigned int int unsigned short int short int unsigned char short char Promotion occurs in this directionTRANSCRIPT
![Page 1: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/1.jpg)
Functions & Strings
CIS 23008-Feb-06
![Page 2: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/2.jpg)
Quiz1. Write a function Prototype for the function
findSmallest that takes three integers and returns an integer.
2. Write a function call for findSmallest storing the value in a variable named smallest. Use the values num1, num2, and num3 as arguments.
3. Write a function header for the function findSmallest that takes three integers, x, y, and z, and returns an integer.
![Page 3: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/3.jpg)
Function: Return Type• Data type hierarchy:• long double• double• float• unsigned long int• long int• unsigned int• int• unsigned short int• short int• unsigned char• short• char
Promotion occurs in this direction
![Page 4: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/4.jpg)
Recursion
• Recursion is the ability of a function to call itself. • Consider the mathematical function n! (factorial)
– n! = n * (n-1) … * 2 * 1– Not mathematically precise because we use an
ellipsis (…). • Consider the following formal definition
– n! = 1, if n = 0– n! = n * (n-1)!, if n > 0
• The factorial function is defined in terms of itself
![Page 5: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/5.jpg)
Recursion
• C++ function mirrors the mathematical factorial definition
– If the value of n is 0, the value 1 is returned.– Otherwise, the product of n and Factorial(n-1)
is returned.
![Page 6: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/6.jpg)
Recursion
![Page 7: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/7.jpg)
Recursionint Factorial(int n) {
if (n == 0) {
return (1);}else {
return (n * Factorial(n-1));}
}
![Page 8: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/8.jpg)
Recursion#include <iostream>using namespace std;int Factorial(int); int main() {
cout << "Enter a positive integer: ";int n;cin >> n;cout << n << "! = " << Factorial(n) << endl;return 0;
}
![Page 9: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/9.jpg)
Recursion vs. Iteration
• Write a factorial function using iteration:int factorial (int n){ int result = 1; for (int i = n; i >= 1; i--) result *= i; result = result * I; return result;}
![Page 10: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/10.jpg)
Parameters by Reference
• When you want more than one item to change.
• Changes (uses) the original variables• No need to return• Use & before the parameter variable
declaration
![Page 11: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/11.jpg)
Parameters by Reference• Switch two values:void swap (float &first, float &second){ float temp; temp = first; first = second; second = temp;}
• In main:if ( a > b ) swap (a, b);
![Page 12: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/12.jpg)
Parameters by Reference• & (Reference character) must go in the prototype and
the definition.void getData(int, int &);
void getData(int a, int & b){
int one, two;a = a + 1;b = 2;cout << "Enter two integers: ";cin >> one >> two;
}
![Page 13: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/13.jpg)
Reference Practice
• Write a function header for a function named time that has an integer parameter named seconds and three integer reference parameters named hours, min, and sec.
![Page 14: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/14.jpg)
Strings
• #include <string>• Character sequence enclosed in double
quotes
![Page 15: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/15.jpg)
Declaring & Initializing Strings• string objectName = value;
– string str1 = “Good Morning”;– string str2 = str1;– string str3 = str1 + str2;
• string objectName(stringValue);– string str4(“Hot”);– string str5(str4 + “ Dog”);
• string objectName(str, n);– string str6(“Good Morning”);– string str7(str6, 5); //str7 = “Morning”;
![Page 16: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/16.jpg)
Declaring & Initializing Strings
• string objectName(str, n, p);– string str8(“Good Morning”);– string str9(str8, 5, 2); //str9 = “Mo”;
• string objectName(n, char);– string str10(5, ‘*’); //str10 = “*****”;
• string objectName;– string message; //message = “”;
![Page 17: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/17.jpg)
String Input
string message;cin >> message;cout << message;
This may have problems….
![Page 18: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/18.jpg)
Extraction Operator >>
• >> skips any leading whitespace characters
• >> stops at (before) the first trailing whitespace character
• trailing whitespace character is left in stream, and will become “next” leading whitespace character.
![Page 19: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/19.jpg)
String Input Using >>
string firstName;string lastName;cin >> firstName >>
lastName;
• Input stream:Joe Hernandez 23
Results:“Joe” “Hernandez”firstName lastName
![Page 20: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/20.jpg)
getline() Function
• >> cannot be used to input a string with blanks in it.
• Use getline(inFileStream, str) • First argument is an input stream variable
(cin), second is the string variable.
string message;getline(cin, message);
![Page 21: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/21.jpg)
getline(inFileStream, str)
• getline does not skip leading whitespace characters
• getline reads all characters (including blanks) into the string.
• getline stops when it reaches ‘\n’• ‘\n’ is not stored in the string variable
![Page 22: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/22.jpg)
String Input: getline
string firstName;string lastName;getline (cin, firstName);getline (cin, lastName);
• Input stream:Joe Hernandez 23
Results:“Joe Hernandez 23” ?firstName lastName
![Page 23: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/23.jpg)
String Operations
![Page 24: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/24.jpg)
Character Manipulation
• #include <cctype>• tolower(value)• toupper(value)• isalpha(value)• isdigit(value)
• String -> Integer:– atoi(string.c_str());
• String -> Float:– atof(string.c_str());
![Page 25: Functions & Strings CIS 230 08-Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b4e7f8b9ab0599a67f9/html5/thumbnails/25.jpg)
Example code:
Located in:/class-files/samples/
strings/stringpractice.cppstringinput.cppstringoperations.cpp
Located in:/class-files/samples/filesask.cppinfile.cppmixed.cppint.datparts.dat