Unformatted text preview: Engineering 3 – Class 7
Today:
• a note about future assignments • more on logical operators • using builtin and preexisting functions Note: Brendon’s office hours: Thursday from 1112 pm, Rm. 1117 Assignment Headers
Please include a short header at the beginning of your programs, eg:
/*Name: Brendon Hall Account Name: brendon Program: timestable.c Description: This program does …… */ #include <stdio.h> …………… More Logical Operators
Used to create compound conditional statements Logical AND operator  &&
expression1 expression2 expression1 && expression2 0 0 0 0 1 0 1 0 0 1 1 1 More Logical Operators
Logical AND operator  &&
expression1 expression2 expression1 && expression2 0 0 0 0 1 0 1 0 0 1 1 1 Negation  !
expression1 !expression2 0 1 1 0 More Logical Operators
x = 2, y = 5 x<5 true! !(y >= 10) true! !(x<5) && !(y>=7) false! !(y != 3)  (x > 3) false! y=4 true! Program Modules in C – Functions
• All C programs are made up of components (modules) called functions. Each program contains a number of functions, one of which is called main. • C contains libraries of prewritten functions, and also allows users to write their own functions. • When using library functions, a statement telling C to use the library must be included in the header
– Example: #include <math.h> • Many mathematical functions are available in the C math library. Math Library Functions
• Math library functions
– Perform common mathematical calculations (sin, exponent, absolute value, etc.) – Include in header: #include <math.h> • Format for calling functions
– FunctionName( argument1 , argument2, … ); – Arguments may be constants, variables, or expressions – i = sqrt( 25.0 );
• sqrt is a function that accepts 1 argument of data type double • sqrt calculates the square root of the argument (in this case 25.0) • All math functions return data type double • Compiling programs with math library functions
– Compile command must include the option –lm – Example: gcc –lm prog1.c – Example: gcc –lm –o prog1.out prog1.c Math Library Functions
From Fig. 5.2 in book – commonly used math library functions:
Function sqrt(x) exp(x) log(x) log10(x) fabs(x) ceil(x) floor(x) Description square root of x exponential function ex natural log of x [ ln(x) ] log (base 10) of x [ log10(x) ] absolute value of x rounds x to the smallest integer not less than x rounds x to the largest integer not greater than x Example sqrt( 900.0 ) is 30.0 sqrt( 4.0 ) is 2.0 exp( 1.0 ) is 2.718282 exp( 2.0 ) is 7.389056 log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( 1.0 ) is 0.0 log10( 100.0 ) is 2.0 fabs( 3.5 ) is 3.5 fabs( 2.3 ) is 2.3 ceil( 9.2 ) is 10.0 ceil( 7.8 ) is 7.0 floor( 9.2 ) is 9.0 floor( 7.8 ) is 8.0 Math Library Functions
From Fig. 5.2 (continued):
Function pow(x,y) fmod(x,y) sin(x) cos(x) tan(x) Description x raised to power y ( xy ) Example pow( 2, 5 ) is 32.0 pow( 9, .5 ) is 3.0 fmod( 12.3, 2.1 ) is 1.8 fmod( 2.35, 1.02 ) is 0.31 sin( 0.0 ) is 0.0 sin( 3.14159/2 ) is 1.0 cos( 0.0 ) is 1.0 cos( 3.14159/2 ) is 0.0 tan( 0.0 ) is 0.0 tan( 3.14159/4 ) is 1.0 remainder of x/y as a floating point number trigonometric sine of x ( x in radians ) trigonometric cosine of x ( x in radians ) trigonometric tangent of x ( x in radians ) Example Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* using math library functions */ #include <stdio.h> #include <math.h> int main() { float pi = 3.14159; double i = sin( 3.14159 ); double j = sin( pi/2 ); float k = cos( pi/2 ); float l = tan( pi ); double m = 2.3e5; printf( printf( printf( printf( printf( "\ni "\nj "\nk "\nl "\nm is is is is is %f", i %f", j %f", k %f", l %f\n", ); ); ); ); m ); return 0; /* indicate that program ended successfully */ } /* end function main */ Example Program Output
Output for program on previous page:
% a.out i j k l m % is is is is is 0.000003 1.000000 0.000001 0.000003 230000.000000 Math Function Example
Processes like burning coal result in the emission of sulfur dioxide into the atmosphere. Sulfuric acid is formed as a result and the resulting acid rain can damage the environment. The acidity of rain and lakes is measured using pH = − log10 (C )
where C is the concentration of hydronium ions in the water. A pH value of 7 indicates a neutral value, while levels below 7 indicate an acid and above 7 an alkaline or base. Math Function Example
Algorithm: • input hydronium ion concentration • calculate pH • display results Hand calculation…consider input C = .0001 (mol/litre)
pH = − log10 (.0001) = 4 Example Program: Compound Interest
An investor plans to invest an amount of money p in an account with interest r (compounded annually). Write a program that asks the user to enter the initial investment amount (also called the principle) and the interest rate and then outputs a table showing the amount in the account at the end of each of the first 10 years. The formula for compound interest is: a = p (1 + r)n where p is the principle r is the annual interest rate n is the number of years a is the amount in the account after n years The function pow(x,y) calculates xy Sample output for this program is shown on the next page. Program Output: Compound Interest
% a.out Enter the principle amount: 1200.00 Enter the annual interest rate: .04 Year 1 2 3 4 5 6 7 8 9 10 % Amount in account 1248.00 1297.92 1349.84 1403.83 1459.98 1518.38 1579.12 1642.28 1707.97 1776.29 Example Program: Compound Interest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* calculating compound interest */ #include <stdio.h> #include <math.h> int main() { double amount; double principle; double rate; int year; printf( "\nEnter the principle amount: " ); scanf( "%lf", &principle ); printf( "Enter the annual interest rate: " ); scanf( "%lf", &rate ); Example Program: Compound Interest
printf( "\n%4s%21s\n", "Year", "Amount in account" ); 18 19 for ( year = 1 ; year <= 10 ; year++ ) { 20 amount = principle * pow( 1.0 + rate, year ); 21 printf( "%4d%21.2f\n", year, amount ); 22 } 23 24 return 0; /* indicate that program ended successfully */ 25 26 27 } /* end function main */ Temperature Conversion
For some people (like Canadians) it is difficult to get used to using some US units of measurement. For example, they always have to convert temperature readings from ˚F to units that make much more sense, like ˚C. Consider a library, defined in “conv_lib.h” that has functions that performs conversions. Use the function FtoC() to print a conversion chart that displays the converted temperature over a specified range and interval. Develop an appropriate algorithm, and implement the code. Name your program conversion.c and compile with: gcc conv_lib.o conversion.c Temperature Conversion
For some people (like Canadians) it is difficult to get used to using some US units of measurement. For example, they always have to convert temperature readings from ˚F to units that make much more sense, like ˚C. Consider a library, defined in “conv_lib.h” that has functions that performs conversions. Use the function FtoC() to print a conversion chart that displays the converted temperature over a specified range and interval. Writing Custom Functions
• In addition to functions which are defined in libraries, C allows you to write your own customized functions. • When you use a customized function, you must include the following in your program:
– Declare the function at the beginning (or include a custom header file where the function is defined) – Call the function in the main body of your program – Define the function Function Declaration
• Included in your header • Declares the output type and the input parameters for the function • Declaration format:
returnvaluetype functionname( parameter list ); • Examples:
– int cube( int x ); – float maxtest( float a, float b, float c ); – char primetest( int num ); Function Definition
• Function definition is done after the main body of your program • Function definition format:
returnvaluetype functionname( parameter list ); { declarations and statements } • Function body (block)
– Variables can be defined inside blocks – Functions cannot be defined inside other functions Example Program: Custom Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /* Creating and using custom functions */ #include <stdio.h> int cube( int y ); int main() { int i; /* function prototype */ /* counter */ /* calculate and print the cube of integers between 1 and 10 */ for ( i = 1 ; i <= 10 ; i++ ) { printf( "%d ", cube( i ) ); } /* end for */ printf( "\n\n" ); return 0; /* indicate successful termination */ } /* end main */ /* define cube function to return the cube of an integer */ int cube( int y ) { return y*y*y; /* returns cube of y as an int */ } /* end function cube */ Example Program Output
Output for program on previous page:
% a.out 1 8 27 64 125 216 343 512 729 1000 % ...
View
Full Document
 Summer '08
 BENYAAKOV
 math library functions

Click to edit the document details