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.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Strings.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Strings & Text File Input CIS Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Overview creating your own functions calling your own functions.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
1 Chapter 4 Program Input and the Software Design Process.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Functions Why we use functions C library functions Creating our own functions.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Program Input and the Software Design Process ROBERT REAVES.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Chapter 4 Program Input and the Software Design Process.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
CS221 C++ Basics. C++ Data Types structured array struct union class address pointer reference simple integral char short int long bool floating float.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Program Input and the Software Design Process
C++ Arrays.
Lecture 2B Expressions Richard Gesick
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
CS150 Introduction to Computer Science 1
10.1 Character Testing.
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 3 Input output.
Chapter 4 Program Input and the Software Design Process
Engineering Problem Solving with C++, Etter
CS1201: Programming Language 2
Fundamental Programming
Using string type variables
(Dreaded) Quiz 2 Next Monday.
Main() { int fact; fact = Factorial(4); } main fact.
Strings Skill Area 313 Part C
Presentation transcript:

Functions & Strings CIS 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

int Factorial(int n) { if (n == 0) { return (1); } else { return (n * Factorial(n-1)); }

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

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 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” firstNamelastName

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”? firstNamelastName

String Operations

Character Manipulation #include tolower(value) toupper(value) isalpha(value) isdigit(value) String -> Integer: –atoi(string.c_str()); String -> Float: –atof(string.c_str());

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