Download presentation
Presentation is loading. Please wait.
Published byTobias Harvey Modified over 9 years ago
1
Functions & Strings CIS 230 08-Feb-06
2
Quiz 1.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.
3
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
4
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
5
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.
6
Recursion
7
int Factorial(int n) { if (n == 0) { return (1); } else { return (n * Factorial(n-1)); }
8
Recursion #include using namespace std; int Factorial(int); int main() { cout << "Enter a positive integer: "; int n; cin >> n; cout << n << "! = " << Factorial(n) << endl; return 0; }
9
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; }
10
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
11
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);
12
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; }
13
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.
14
Strings #include Character sequence enclosed in double quotes
15
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”;
16
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 = “”;
17
String Input string message; cin >> message; cout << message; This may have problems….
18
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.
19
String Input Using >> string firstName; string lastName; cin >> firstName >> lastName; Input stream: Joe Hernandez 23 Results: “Joe”“Hernandez” firstNamelastName
20
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);
21
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
22
String Input: getline string firstName; string lastName; getline (cin, firstName); getline (cin, lastName); Input stream: Joe Hernandez 23 Results: “Joe Hernandez 23”? firstNamelastName
23
String Operations
24
Character Manipulation #include tolower(value) toupper(value) isalpha(value) isdigit(value) String -> Integer: –atoi(string.c_str()); String -> Float: –atof(string.c_str());
25
Example code: Located in: /class- files/samples/strings/ stringpractice.cpp stringinput.cpp stringoperations.cpp Located in: /class-files/samples/files ask.cpp infile.cpp mixed.cpp int.dat parts.dat
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.