C for the MFE class at UC Berkeley Session - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

C for the MFE class at UC Berkeley Session

Description:

{ cout name[i] ' ' phone[i] 'n' ... Enter phone number: 5556666. Enter number of hours worked: 30. Enter wage: 23.40 ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 37
Provided by: Yuli6
Category:
Tags: mfe | berkeley | class | phone | session

less

Transcript and Presenter's Notes

Title: C for the MFE class at UC Berkeley Session


1
C for the MFE class at UC BerkeleySession 3
4-20-2006
  • Yuli Kaplunovsky MFE, MBA, MS-Tax, CFA
  • yuli_at_FinancialSimulations.com
  • (408) 884 5965

2
Example - simple class
include ltiostreamgt using namespace std class
cl int i // private by default public int
get_i() void put_i(int j) int
clget_i() return i void clput_i(int
j) i j int main() cl s
s.put_i(10) cout ltlt s.get_i() ltltendl
return 0
http//www.cs.uregina.ca/Links/class-info/cplusplu
s/CExample.html
3
Example new / delete
include ltiostreamgt include ltcstdlibgt using
namespace std class myclass int
p public myclass(int i) myclass() int
getval() return p myclassmyclass(int
i) cout ltlt "Allocating p\n" p new int
if(!p) cout ltlt "Allocation failure.\n"
exit(1) // exit program if out of memory
p i myclassmyclass() cout ltlt
"Freeing p\n" delete p
void display(myclass ob) cout ltlt ob.getval()
ltlt '\n' int main() myclass a(10)
display(a) return 0
4
Example Input/output
include ltiostreamgt using namespace std int
main() float gallons, liters cout ltlt
"Enter number of gallons " cin gtgt gallons //
Read the inputs from the user liters gallons
3.7854 // convert to liters cout ltlt
"Liters " ltlt liters ltlt endl return 0
5
Example char array
include ltiostreamgt include ltstdlib.hgt using
namespace std int main() char name32 //
big enough to hold 32 characters // prompt for
the name cout ltlt "What's your name?" ltlt
endl gets(name) // read a string from the key
board. cout ltlt "Hello! " ltlt name ltlt "!" ltlt
endl return 0
6
Example 2D array
include ltiostreamgt using namespace std int
main() int sqrs102 1, 1,
2, 4, // The square of 2 is 4,and so on 3,
9, 4, 16, 5, 25, 6, 36, 7,
49, 8, 64, 9, 81, 10, 100
int i, j cout ltlt "Enter a number between 1
and 10 " cin gtgt i // look up i for(j
0 j lt 10 j) if(sqrsj0 i) break
// break from loop if i is found cout ltlt "The
square of " ltlt i ltlt " is " cout ltlt sqrsj1
ltlt endl return 0
7
Example - Pointers
  • include ltstdio.hgt
  • typedef struct
  • int X,Y,Z
  • cord_struct
  • int MyFunc( cord_struct P )
  • return P-gtX P-gtY
  • void main()
  • int J,K,L
  • cord_struct Cord10
  • for ( J 0 J lt 10 J )
  • CordJ.X J2
  • CordJ.Y J3 1

8
Example Array of strings
include ltiostreamgt using namespace std //
function prototyping int menu() // funciton
to display the menu void enter() // function to
enter info void report() // function to print
report // Global variables char name280
// this array holds employee names char
phone220 // their phone numbers float
hours2 // hours worked per week float
wage2 // wage int choice int main()
do choice menu() // get selection
switch(choice) case 0 break case
1 enter() break case 2
report() break default cout ltlt
"Try again.\n\n" while(choice ! 0)
return 0
9
int menu() // Return a user's selection. int
choice cout ltlt "0. Quit\n" cout ltlt "1.
Enter information\n" cout ltlt "2. Report
information\n" cout ltlt "\nChoose one " cin
gtgt choice return choice void enter() //
Enter information. for(int i0 ilt2 i)
cout ltlt "Enter last name " cin gtgt
namei cout ltlt "Enter phone number "
cin gtgt phonei cout ltlt "Enter number of
hours worked " cin gtgt hoursi cout ltlt
"Enter wage " cin gtgt wagei void
report() // Display report. for( int i0 ilt2
i) cout ltlt namei ltlt ' ' ltlt phonei ltlt
'\n' cout ltlt "Pay for the week " ltlt wagei
hoursi ltlt \n
0. Quit 1. Enter information 2. Report
information Choose one 1 Enter last name
Smith Enter phone number 5556666 Enter number of
hours worked 30 Enter wage 23.40 Enter last
name Bush Enter phone number 6668888 Enter
number of hours worked 20 Enter wage 40.00 0.
Quit 1. Enter information 2. Report
information Choose one 2 Smith 5556666 Pay for
the week 702 Bush 6668888 Pay for the week
800 0. Quit 1. Enter information 2. Report
information Choose one 0
10
Reference in a function
// function definition for swap() void swap(int
i, int j) int temp temp i i j
j temp
include ltiostreamgt using namespace std void
swap(int i, int j) // function prototype
swapping two values int main() int NumOne
0 int NumTwo 0 cout ltlt "Please enter two
integers " ltlt endl cout ltlt "Enter value for
NumOne " cin gtgt NumOne cout ltlt "Enter
value for NumTwo " cin gtgt NumTwo cout ltlt
"Before swapping, NumOne is " ltlt NumOne ltlt
endl cout ltlt "Before swapping, NumTwo is " ltlt
NumTwoltlt endl swap(NumOne, NumTwo) cout
ltlt "After swapping, NumOne is " ltlt NumOne ltlt
endl cout ltlt "After swapping, NumTwo is " ltlt
NumTwoltlt endl return 0
Please enter two integers Enter value for
NumOne 10 Enter value for NumTwo 20 Before
swapping, NumOne is 10 Before swapping, NumTwo
is 20 After swapping, NumOne is 20 After
swapping, NumTwo is 10
11
simple inheritance
include ltiostreamgt using namespace std class
base int i, j public void set(int a, int
b) i a j b void show() cout ltlt i ltlt
" " ltlt j ltlt "\n" // inheritance class
derived public base int k public
derived(int x) k x void showk() cout
ltlt k ltlt "\n" void main() derived
ob(3) ob.set(1, 2) // access member of base
ob.show() // access member of base
ob.showk() // uses member of derived class
Output 1 2 3
12
Using protected members
class base protected int i, j // private to
base, but accessible to derived public void
set(int a, int b) i a j b void show()
cout ltlt i ltlt " " ltlt j ltlt "\n" class
derived public base int k public //
derived may access base's i and j void setk()
k ij void showk() cout ltlt k ltlt "\n"
void main() derived ob ob.set(2,
3) // OK, known to derived ob.show() //
OK, known to derived ob.setk() ob.showk()
Output 2 3 6
13
Protected inheritance
class base int i protected int
j public int k void seti(int a) i a
int geti() return i // Inherit base
as protected. class derived protected base
public void setj(int a) j a // j is
protected here void setk(int a) k a // k
is also protected int getj() return j
int getk() return k
int main() derived ob / This next line
is illegal because seti() is a protected
member of derived, which makes it
inaccessible outside of derived. / //
ob.seti(10) // cout ltlt ob.geti() // illegal
-- geti() is protected // ob.k 10 // also
illegal because k is protected // these next
statements are OK ob.setk(10) cout ltlt
ob.getk() ltlt ' ' ob.setj(12) cout ltlt
ob.getj() ltlt ' ' return 0
Output 10 12
14
Multiple inheritance
class base1 protected int x public void
showx() cout ltlt x ltlt "\n" class base2
protected int y public void showy()
cout ltlt y ltlt "\n" // Inherit multiple base
classes. class derived public base1, public
base2 public void set(int i, int j) x i
y j
int main() derived ob ob.set(10, 20) //
provided by derived ob.showx() // from
base1 ob.showy() // from base2 return
0
Output 10 20
15
Calling base class's constructor in derived
class 
class base1 protected int i public
base1(int x) i x cout ltlt "Constructing
base1\n" base1() cout ltlt "Destructing
base2\n" class base2 protected int
k public base2(int x) k x cout ltlt
"Constructing base2\n" base2() cout ltlt
"Destructing base2\n" class derived
public base1, public base2 int j public
derived(int x, int y, int z) base1(y), base2(z)
j x cout ltlt "Constructing derived\n"
derived() cout ltlt "Destructing derived\n"
void show() cout ltlt i ltlt " " ltlt j ltlt " " ltlt k
ltlt "\n"
int main() derived ob(3, 4, 5) ob.show()
// displays 4 3 5 return 0
Constructing base1 Constructing
base2 Constructing derived 4 3 5 Destructing
derived Destructing base2 Destructing base2
16
Calling base class's constructor in derived
class 
include ltiostreamgt include ltcstringgt using
namespace std class B_class char
author80 public void put_author(char s)
strcpy(author, s) void show_author() cout
ltlt author ltlt "\n" class D_class public
B_class char title80 public void
put_title(char num) strcpy(title, num)
void show_title() cout ltlt "Title "
cout ltlt title ltlt "\n"
void main() B_class p B_class B_ob
D_class dp D_class D_ob p B_ob //
address of base // Access B_class via
pointer. p-gtput_author("Tom Clancy") //
Access D_class via base pointer. p D_ob
p-gtput_author("William Shakespeare") // Show
that each author went into proper object.
B_ob.show_author() D_ob.show_author() cout
ltlt "\n" dp D_ob dp-gtput_title("The
Tempest") p-gtshow_author() // either p or dp
can be used here. dp-gtshow_title( )
Output Tom Clancy William Shakespeare William
Shakespeare Title The Tempest
Since put_title() and show_title() are not
part of the base class, they are not accessible
via the base pointer p and must be accessed
either directly, or, as shown here, through a
pointer to the derived type.
17
Virtual functions
class triangle public figure public
void show_area() cout ltlt "Triangle with
height " cout ltlt x ltlt " and base " ltlt y
cout ltlt " has an area of " cout ltlt x
0.5 y ltlt ".\n" class square public
figure public void show_area()
cout ltlt "Square with dimensions " cout ltlt
x ltlt "x" ltlt y cout ltlt " has an area of "
cout ltlt x y ltlt ".\n" class
circle public figure public void
show_area() cout ltlt "Circle with radius
" cout ltlt x cout ltlt " has an area
of " cout ltlt 3.14 x x ltlt ".\n"

class figure protected double x, y public
void set_dim(double i, double j0) x i
y j virtual void show_area()
cout ltlt "No area computation defined " cout
ltlt "for this class.\n"
18
int main() figure p // create a pointer to
base type triangle t // create objects of
derived types square s circle c p t
p-gtset_dim(10.0, 5.0) p-gtshow_area() p
s p-gtset_dim(10.0, 5.0) p-gtshow_area()
p c p-gtset_dim(9.0) p-gtshow_area()
return 0
Triangle with height 10 and base 5 has an area of
25. Square with dimensions 10x5 has an area of
50. Circle with radius 9 has an area of 254.34.
19
Financial applications - Normal Distribution
  • //
  • //
  • double N_func( double X ) // The cumulative
    normal distribution
  • double L, K, w
  • double const a1 0.31938153, a2
    -0.356563782, a3 1.781477937
  • double const a4 -1.821255978, a5
    1.330274429
  • L fabs(X)
  • K 1.0 / (1.0 0.2316419 L)
  • w 1.0 - 1.0 / sqrt(2 Pi) exp(-L L / 2)
    (a1 K a2 K K a3 pow(K,3) a4
    pow(K,4) a5
  • pow(K,5))
  • if (X lt 0 )
  • w 1.0 - w
  • return w

20
Black Scholes (there is a mistake)
  • double BS( double S, double K, double Sig,
  • double t, double r )
  • double d1, d2
  • double t_sqrt sqrt(t)
  • d1 (log(S/K) r t) / (Sig t_sqrt )
  • 0.5 Sig t_sqrt
  • d2 d1 - (Sigt_sqrt)
  • return S N_func(d1) - K exp( -r t )
    N_func(d2)

21
fopen, fprintf, fclose
  • Use pointer to predetermined structureFILE F
  • To create / open file F fopen( file_name,
    type )examplesF fopen(file1.dat, w
    )F_Read fopen(file2.dat,r )
  • ALWAYS need to close the file before program
    terminatesfclose( F )
  • One way to write to a file very similar to
    printf()fprintf( F, test\n )

22
File write example 1
A new file by the name file1.dat is created,
and its content is ABCDE Second Line 0, 1, 2,
3, 4, 5, 6, 7, 8, 9,
  • include ltstdio.hgt
  • void main()
  • FILE F
  • int I
  • F fopen("file1.dat", "w" )
  • fprintf( F, "ABCDE\n" )
  • fprintf( F, "Second Line\n" )
  • for ( I 0 I lt 10 I )
  • fprintf( F, "d, ", I)
  • fclose(F)

23
File read example 2
The content of the file num10.dat 10 20 30 40
99 32 1 999 -22 4423
include ltstdio.hgt void main() FILE F
int I, J F fopen("num10.dat", "rt" )
printf("Reading from the file\n") for ( I
0 I lt 10 I ) fscanf( F, "d" ,
J ) printf( "d, ", J )
fclose(F)
  • Output (on the monitor)
  • Reading from the file
  • 10, 20, 30, 40, 99, 32, 1, 999, -22, 4423,

24
File read example 3
  • The content of the file num10.dat
  • 1,10.2
  • 3, 20
  • 5, 30.33
  • 1,2
  • 22 , 333
  • 45,46
  • 9 40.11
  • 3,-993.3333
  • 99,33.2

include ltstdio.hgt void main() FILE F
int I, J char ST200 float FL
double D F fopen("num20.dat", "rt" )
printf("Reading from the file\n") for ( I
0 I lt 8 I ) fgets( ST,
sizeof(ST)-1, F ) sscanf( ST, "d,f" ,
J, FL ) D FL printf( "d,g
\n", J, D ) fclose(F)
  • Output (on the monitor)
  • Reading from the file
  • 1,10.2
  • 3,20
  • 5,30.33
  • 1,2
  • 22,2
  • 45,46
  • 9,46
  • 3,-993.333

25
malloc / free
  • include ltstdlib.hgt
  • void main()
  • double D
  • int I
  • D (double )malloc( sizeof(D) 100 )
  • for ( I 0 I lt 100 I )
  • DI (double)I 1.2
  • free(D)

26
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include ltmath.hgt
  • typedef struct double X,Y CORD_typedef
  • int Generate_Sin_Noise( int N, char FileName )
  • FILE F
  • int I
  • CORD_typedef C
  • F fopen(FileName, "w" )
  • if ( F NULL )
  • return -1 // An Error
  • C (CORD_typedef ) malloc(
    sizeof(CORD_typedef) N )
  • if ( C NULL )

Write file example
void main() if ( Generate_Sin_Noise( 200,
"TestSin.dat" ) ! 0 )
printf("Error\n") exit(-1)
printf("Done \n")
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
(No Transcript)
32
(No Transcript)
33
From excel to C
Output 0 192606, -12.31, 0.88 1 192607,
-19.3, -6.24 2 192608, -13.04, -2.83 889
200007, -3.55, 1.72 890 200008, -5.91,
-5.17 891 200009, -10.62, -7.07 892 200010,
-5.03, 1.58 893 200011, -5.7251, 0.245714
  • include ltstdio.hgt
  • void main()
  • FILE F
  • char ST300
  • int ItemsNum,I
  • struct
  • int Month
  • double P1,P2
  • Item1000
  • float F1,F2
  • F fopen("Book1.txt", "rt" )
  • if ( F NULL ) printf("Error opening
    file\n") return
  • fgets(ST, sizeof(ST), F) // We know that the
    first line is not used
  • ItemsNum 0

34
(No Transcript)
35
(No Transcript)
36
Exercise / Homework This was the basic
foundation of C Spending ONLY 2.5 hours in
class is NOT enough to master the foundation The
easiest way to memorize it and get more familiar
with it is to surprisingly simple - COPY all
the samples and run them one by one on your PC.
Write a Comment
User Comments (0)
About PowerShow.com