Transcript Slide 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.
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.
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
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
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.
Recursion
Recursion
int Factorial(int n)
{
if (n == 0)
{
return (1);
}
else
{
return (n * Factorial(n-1));
}
}
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;
}
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;
}
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
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);
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;
}
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.
Strings
• #include <string>
• Character sequence enclosed in double
quotes
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”;
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 = “”;
String Input
string message;
cin >> message;
cout << message;
This may have problems….
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.
String Input Using >>
string firstName;
string lastName;
cin >> firstName >>
lastName;
• Input stream:
Joe Hernandez 23
Results:
“Joe”
“Hernandez”
firstName
lastName
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);
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
String Input: getline
string firstName;
string lastName;
getline (cin, firstName);
getline (cin, lastName);
• Input stream:
Joe Hernandez 23
Results:
“Joe Hernandez 23”
firstName
?
lastName
String Operations
Character Manipulation
•
•
•
•
•
#include <cctype>
tolower(value)
toupper(value)
isalpha(value)
isdigit(value)
• String -> Integer:
– atoi(string.c_str());
• String -> Float:
– atof(string.c_str());
Example code:
Located in:
/classfiles/samples/strings/
stringpractice.cpp
stringinput.cpp
stringoperations.cpp
Located in:
/class-files/samples/files
ask.cpp
infile.cpp
mixed.cpp
int.dat
parts.dat