Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus."— Presentation transcript:

1 CSC 107 – Programming For Science

2 Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus  All material is important & you are responsible for it  This is one reason why using book & post all solutions  PPTX slides posted onto Angel for each lecture  For unclear reasons, IE ignores ending & treats as ZIP  Could rename ZIP to PPTX & use with PowerPoint  Or, for many reasons, just use Firefox to download file

3 The Lecture’s Goal

4 Functions  C++ actually tries being useful on occasion  Defines built-in functions for use in any program  How functions work similar in both C++ & algebra  Consider black-box that takes value & returns another  (Will discuss other types of functions later)  Functions must be declared before using it  Just like variables, must give computer some warning  For built-in functions, done with #include s

5 Mathematical Functions  Add #include at top of file  Should go with #include and others  Order does not matter, can list however you want  All these functions return a value  Will NOT change argument’s value

6 Mathematical Functions  Function result is tossed unless you take action  Use as part of an expression program is computing  Result of the function can be assigned to variable  Could be ignored, but why bother calling function?

7 Using These Functions  abs(x) returns absolute value of number  Result’s type matches type of value passed in int i1 = abs(-1); double d1 = abs(-56.54); double d2 = abs(i1); int i2 = abs(i1 + 1 * d2); double d3 = d2 * abs(d1); i2 = 46 * abs(i1); d3 = abs(abs(d2));

8 Other Functions  Decimal data only returned by trig. functions sin( x ), cos( x ), tan( x ), asin( x ), atan( x )…  These will still match argument’s type  Radians used when evaluating the angles (2π = 360˚)  Exponent functions also return decimal data log10( x ), sqrt( x ), log( x ), exp( x )…  Return type matches argument’s type for these also  Decimals needed because results could be decimal  pow( x, y ) computes x y  x ’s (decimal) type is type of returned value

9 Errors In Functions  Some values may cause errors in function  Nothing will be output, but result appears funny if printed  If assigned to variable, variable could continue to be used  Using “funny” value results in funny value for all equations acos( x ), asin( x ) x must be in range [-1, 1] sqrt( x ) x must be number ≥ 0 exp( x ) ε x must be in range of x ’s type pow( x, y ) x y must fit in x ’s type; y ≥ 0

10 Rounding Functions  Safely convert decimal numbers into integers  floor( x ) returns x rounded down to nearest integer floor(2.01) returns 2.0 floor(78.999999) returns 78.0 floor(-0.0001) returns -1.0 floor(floor(-65.561)) returns -66.0  ceil( x ) takes x & returns number after rounding up ceil(2.01) returns 3.0 ceil(78.999999) returns 79.0 ceil(-0.0001) returns 0.0 ceil(ceil(-65.561)) returns -65.0

11 What Planet Are [They] From?  Both floor( x ) & ceil( x ) return decimals  Why? would be a good question to ask

12 What Planet Are [They] From?  Both floor( x ) & ceil( x ) return decimals  Why? would be a good question to ask

13 Using Characters  Also get in on built-in function loving in C++, too  Must have #include in includes to use  Info about char possible with some functions  Uses int s – 0 returned for false & others mean true isalpha( x ) – checks if x is letter isdigit( x ) – checks if x is number isalnum( x ) – checks if x is letter or number ispunct( x ) – checks if x is punctuation mark isspace( x ) – checks if x is space, tab, or enter iscntrl( x ) – checks if x is “control character”

14 Data Types  Assignments are legal only if always safe  C++ defines ordering of legal assignments long double double float long int short char Legal to assign to higher type

15 Switching Characters  Built-in function can make things pretty, also  #include needed for these, too  Switch between upper & lowercase letters easily  Arguments that are not letters will be returned as is  Return x as UPPERCASE LETTER with toupper( x )  tolower( x ) will return x as lowercase letter

16 What Planet Are [They] From?  Both toupper( x ) & tolower( x ) return int  Why? would be a good question to ask

17 What Planet Are [They] From?  Both toupper( x ) & tolower( x ) return int  Why? would be a good question to ask

18 Typecasting Requires typecast to work

19 Typecasting int i1 = static_cast (-1.0); char c1 = static_cast (abs(48.3)); float f1 = static_cast (48.3); i1 = static_cast (f1 * 2); c1 = i1 + 3; c1 = static_cast (i1 + 256); i1 = static_cast (i1 + 256); f1 = static_cast (f1 + 256); i1 = (int)(49.0 * 2);

20 Normal Rounding  C++ does not have function for typical rounding  floor( x + 0.5) works with numbers > -0.5  ceil( x + 0.5) works with numbers < -0.5  We will revisit this problem later…

21 Your Turn  Get in groups of 3 & work on following activity

22 For Next Lecture  Read sections 7.1 for Friday  What is the boolean data type?  How do the boolean functions work?  What does this have to do with binary numbers?  Week #3 weekly assignment due Tuesday  Problems available on Angel  If problem takes more than 10 minutes, TALK TO ME!


Download ppt "CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus."

Similar presentations


Ads by Google