1 / 17

NKU CSC 601 Fall 2002 K. Kirby

The Road to GenericLand. NKU CSC 601 Fall 2002 K. Kirby. // Sick Factorial #include <iostream> using namespace std ; #define N 12 template< int n > struct Fac { enum { RET= n * Fac<n-1>::RET } ; } ; template<> struct Fac<0> { enum { RET=1 } ; } ; void main() {

dena
Download Presentation

NKU CSC 601 Fall 2002 K. Kirby

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Road to GenericLand NKU CSC 601 Fall 2002 K. Kirby

  2. // Sick Factorial #include <iostream> using namespace std ; #define N 12 template< int n > struct Fac { enum { RET= n * Fac<n-1>::RET } ; } ; template<> struct Fac<0> { enum { RET=1 } ; } ; void main() { cout << N << "! = " << Fac<N>::RET << endl ; } // output: 12! = 479001600

  3. CSC 101 201 301 401 501 601 Homework Problem Take a sequence of numbers and return the adjacent change with the largest magnitude Ex: { 6, 12, 4, 3, 9, 11 } 6 -8 -1 6 2 Ans:-8 NKU CSC 601 Fall 2002 K. Kirby

  4. // #1. double maxjump1( const double ar[], int n ) { assert( n > 1 ) ; double maxjump= 0; for ( int i=1 ; i < n ; ++i ) { double jump= ar[i] - ar[i-1] ; if ( fabs(jump) > fabs(maxjump) ) maxjump= jump ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  5. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; cout << maxjump1( xarr, 6 ) << endl ; NKU CSC 601 Fall 2002 K. Kirby

  6. // #2. double maxjump2( const vector<double>& ar ) { int n= ar.size() ; assert( n > 1 ) ; double maxjump= 0 ; for ( int i=1 ; i < n ; ++i ) { double jump= ar[i] - ar[i-1] ; if ( fabs(jump) > fabs(maxjump) ) maxjump= jump ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  7. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; vector<double> xvec( xarr, xarr+6 ) ; cout << maxjump2( xvec ) << endl ; NKU CSC 601 Fall 2002 K. Kirby

  8. // #3. template< typename TNum > TNum ab( TNum x ) { return x < 0 ? -x : x ; } template< typename TNum > TNum maxjump3( const vector<TNum>& v ) { int n= v.size() ; assert( n > 1 ) ; TNum maxjump= 0 ; for ( int i=1 ; i < n ; ++i ) { TNum jump= v[i] - v[i-1] ; if ( ab(jump) > ab(maxjump) ) maxjump= jump ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  9. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; vector<double> xvec( xarr, xarr+6 ) ; int iarr[]= { 4, 6, -10, 100, 3, 9 } ; vector<int> ivec( iarr, iarr+6 ) ; cout << maxjump3( xvec ) << endl ; cout << maxjump3( ivec ) << endl ; NKU CSC 601 Fall 2002 K. Kirby

  10. // #4. template< typename TNumCont > typename TNumCont::value_type maxjump4( const TNumCont& v ) { int n= v.size() ; assert( n > 1 ) ; typename TNumCont::value_type maxjump= 0 ; for ( int i=1 ; i < n ; ++i ) { TNumCont::value_type jump= v[i] - v[i-1] ; if ( ab(jump) > ab(maxjump) ) maxjump= jump ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  11. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; vector<double> xvec( xarr, xarr+6 ) ; deque<double> xdeq( xarr, xarr+6 ) ; int iarr[]= { 4, 6, -10, 100, 3, 9 } ; vector<int> ivec( iarr, iarr+6 ) ; deque<int> ideq( iarr, iarr+6 ) ; cout << maxjump4( xvec ) << endl ; cout << maxjump4( ivec ) << endl ; cout << maxjump4( xdeq ) << endl ; cout << maxjump4( ideq ) << endl ; NKU CSC 601 Fall 2002 K. Kirby

  12. // #5. template< typename ItRanNum > typename ItRanNum::value_type maxjump5( ItRanNum first, ItRanNum last ) { typedef typename ItRanNum::value_type TNum ; int n= last - first ; assert( n > 1 ) ; TNum maxjump= 0 ; for ( int i=1 ; i < n ; ++i ) { TNum jump= first[i] - first[i-1] ; if ( ab(jump) > ab(maxjump) ) maxjump= jump ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  13. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; vector<double> xvec( xarr, xarr+6 ) ; deque<double> xdeq( xarr, xarr+6 ) ; int iarr[]= { 4, 6, -10, 100, 3, 9 } ; vector<int> ivec( iarr, iarr+6 ) ; deque<int> ideq( iarr, iarr+6 ) ; cout << maxjump5( xvec.begin(), xvec.end() ) << endl ; cout << maxjump5( ivec.begin(), ivec.end() ) << endl ; cout << maxjump5( xdeq.begin(), xdeq.end() ) << endl ; cout << maxjump5( ideq.begin(), ideq.end() ) << endl ; NKU CSC 601 Fall 2002 K. Kirby

  14. // #6. template< typename ItNum > typename ItNum::value_type maxjump6( ItNum first, ItNum last ) { typedef typename ItNum::value_type TNum ; TNum maxjump= 0 ; ItNum it= first ; TNum cur= *it++ ; while ( it != last ) { TNum jump= *it - cur ; if ( ab(jump) > ab(maxjump) ) maxjump= jump ; cur= *it++ ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  15. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; vector<double> xvec( xarr, xarr+6 ) ; deque<double> xdeq( xarr, xarr+6 ) ; list<double> xlis( xarr, xarr+6 ) ; int iarr[]= { 4, 6, -10, 100, 3, 9 } ; vector<int> ivec( iarr, iarr+6 ) ; deque<int> ideq( iarr, iarr+6 ) ; list<int> ilis( iarr, iarr+6 ) ; cout << maxjump6( xvec.begin(), xvec.end() ) << endl ; cout << maxjump6( ivec.begin(), ivec.end() ) << endl ; cout << maxjump6( xdeq.begin(), xdeq.end() ) << endl ; cout << maxjump6( ideq.begin(), ideq.end() ) << endl ; cout << maxjump6( xlis.begin(), xlis.end() ) << endl ; cout << maxjump6( ilis.begin(), ilis.end() ) << endl ; NKU CSC 601 Fall 2002 K. Kirby

  16. // #7. template< typename ItNum > typename iterator_traits<ItNum>::value_type maxjump7( ItNum first, ItNum last ) { typedef typename iterator_traits<ItNum>::value_type TNum ; TNum maxjump= 0 ; ItNum it= first ; TNum cur= *it++ ; while ( it != last ) { TNum jump= *it - cur ; if ( ab(jump) > ab(maxjump) ) maxjump= jump ; cur= *it++ ; } return maxjump ; } NKU CSC 601 Fall 2002 K. Kirby

  17. double xarr[]= { 12.3, 16.5, 10.5, 15.3, 8.8, 4.3 } ; vector<double> xvec( xarr, xarr+6 ) ; deque<double> xdeq( xarr, xarr+6 ) ; list<double> xlis( xarr, xarr+6 ) ; int iarr[]= { 4, 6, -10, 100, 3, 9 } ; vector<int> ivec( iarr, iarr+6 ) ; deque<int> ideq( iarr, iarr+6 ) ; list<int> ilis( iarr, iarr+6 ) ; cout << maxjump( xvec.begin(), xvec.end() ) << endl ; cout << maxjump( ivec.begin(), ivec.end() ) << endl ; cout << maxjump( xdeq.begin(), xdeq.end() ) << endl ; cout << maxjump( ideq.begin(), ideq.end() ) << endl ; cout << maxjump( xlis.begin(), xlis.end() ) << endl ; cout << maxjump( ilis.begin(), ilis.end() ) << endl ; cout << maxjump( xarr, xarr+6 ) ; cout << maxjump( iarr, iarr+6 ) ; NKU CSC 601 Fall 2002 K. Kirby

More Related