Lecture 25 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation

lecture 25
SMART_READER_LITE
LIVE PREVIEW

Lecture 25 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation

Lecture 25 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture25/*.* Reminder: Homework 6 due on Monday. Questions? Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 1 Outline Template


slide-1
SLIDE 1

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 1

Lecture 25

 Log into Linux. Copy files on csserver from

/home/hwang/cs215/lecture25/*.*

 Reminder: Homework 6 due on Monday.  Questions?

slide-2
SLIDE 2

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 2

Outline

 Template functions

slide-3
SLIDE 3

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 3

Linear Search

 Consider a linear search function for an array of

integers over a range [first, last) that returns the index of the search target, if it is in the array, or last, if the search target is not in the array.

 Analysis

Objects Type Movement Name array of values int [ ] received arr index of lower bound int received first index of upper bound int received target search target int received target index of target int returned position

slide-4
SLIDE 4

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 4

Linear Search

 Design

  • 1. For position from first to last-1 do

1.1. If arr[position] equals the target then 1.1.1. Return position

  • 2. Return last

 Code

int LinearSearch (const int arr[], int first, int last, int target) { for (int position=first; position < last; position++) if (arr[position] == target) // found target return position; return last; }

slide-5
SLIDE 5

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 5

Linear Search

 Suppose we want to use this function to search

an array of strings? doubles? Dates?

 Would have to change the type of arr and

  • target. Nothing changes in the design. The
  • nly characteristic of the element type needed

is equality operator==.

 Could use a value_type typedef

typedef int value_type;

int LinearSearch (const value_type arr[], int first, int last, value_type target) { ... }

slide-6
SLIDE 6

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 6

Linear Search

 But what if we want to use this function for both

integers and strings in the same program?

 C++ allows function overloading, so we could

write two functions, one with int and one with string as the element type.

 Then if we want to search an array of Dates, we

need to add another copy with just the two type

  • changes. Duplicate code should be avoided.

 What we need is a type parameter (vs. a data

parameter). C++ allows type parameters by using a template.

slide-7
SLIDE 7

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 7

Template Functions

 Template function syntax is:

template <typename T> int LinearSearch (const T arr[], int first, int last, const T & target) { ... }

 Note: the textbook uses class rather than

  • typename. Use typename as it is the

standard way to do this.

 Note: since we do not know if T is an object

type, we pass the received target using a const reference parameter

slide-8
SLIDE 8

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 8

Template Functions

 Template functions are instantiated wherever

the function is called. This means an actual type is plugged in for T, and the code for that version of the template function is generated.

 The type that is plugged into the template is

determined by the type of the arguments.

 See files template-examples.cpp,

templates.h, and Makefile.inclass25

slide-9
SLIDE 9

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 9

Template Header Files

 Since the compiler must have the full

implementation to instantiate a version of a template, the header file contains the entire template (not just a prototype), and there is no corresponding .cpp source file.

slide-10
SLIDE 10

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 10

In-class Exercises

 In templates.h, write a template function

PrintArray that receives an array of values and the number of values in the array. This function should display each element to the screen on a separate line.

 Add code to the end of template-

examples.cpp to use PrintArray to print out the three arrays (intList, realList, and strList).

slide-11
SLIDE 11

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 11

In-class Exercises

 In templates.h, write a template function

FindIndexOfMinimum that receives an array

  • f values and the number of values in the array.

The function is to return the index of the minimum value in the array (as determined by

  • perator<).

 Add code to the end of templates-

examples.cpp to use FindIndexOfMinimum to display the minimum value in each array.

slide-12
SLIDE 12

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 12

In-class Exercises

 In templates.h, write a template function Sort

that receives and passes back an array of values, and receives the number of values in the array. This function is to sort the array using a sorting algorithm of your choice.

 Add code at the end of template-

examples.cpp to use Sort to sort the three arrays and then display them again using PrintArray.

slide-13
SLIDE 13

Friday, October 22 CS 215 Fundamentals of Programming II - Lecture 25 13

Submitting In-class Exercises

 When you have completed the in-class

exercises, make sure you have put your name in each file, then tar together files templates.h and template-examples.cpp

 Submit this exercise to the submission system

no later than class time on Monday (11am). The system only will compile the files; it will not run the program.