Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017

Similar presentations


Presentation on theme: "CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017"— Presentation transcript:

1 CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017

2 Example Program Using a PCF
#include <stdio.h> //My file 1.c #define PI double area (double) ; int main (void) { double radius = 0.0 , result = 0.0 ; printf( "Please enter the radius of a circle in inches: " ); scanf( "%lf", &radius ) ; result = area( radius ) ; printf( "\n\nA radius of %.2lf inches gives the circle an area %.2lf square inches.\n\n" , radius, result) ; printf( "Thank you for using this program to calculate the area of a circle.\n\n" ) ; return (0) ; } double area (double p_radius) double total = 0.0 ; total = ( PI * (p_radius * p_radius) ) ; return total ;

3 Math Library Functions
Math library contains common math functions #include <math.h> Format for calling math functions that return values: variable_name = function_name( parameter(s) ); answer = sqrt( 25.0 ); Multiple arguments: use comma-separated list answer = pow ( 2 , 4 ) All math functions return data type double

4 Data Representation: float vs. double
Computers use a form of scientific notation for floating-point representation Numbers written in scientific notation have three components:

5 Data Representation: float vs. double
Computer representation of a floating-point number consists of three fixed-size fields: This is the standard arrangement of these fields.

6 Data Representation: float vs. double
The one-bit sign field is the sign of the stored value. The size of the exponent field, determines the range of values that can be represented. The size of the significand determines the precision of the representation.

7 Data Representation: float vs. double
The IEEE-754 single precision floating point standard uses an 8-bit exponent and a 23-bit significand (32 total bits… think C data type float) The IEEE-754 double precision standard uses an 11-bit exponent and a 52-bit significand (64 total bits… think C data type double) Next, let’s look at examples of using math library functions

8 #include <stdio.h> // file 2.c in my DogNet directory
#include <math.h> // Preprocessor directive to link to math library int main (void) { double input = 0.0 , result = 0.0 ; printf("\n\n Enter an value to be cubed: "); scanf ("%lf" , &input) ; result = pow(input , 3) ; // Math library function "pow" call printf("\n\n\n The value of %.lf cubed is %.lf.\n\n\n", input , result ); printf("\n\n Enter an value to find the square root: "); scanf ("%lf" , &input) ; result = sqrt(input) ; // Math library function "sqrt" call printf("\n\n\n The value of the square root of %.lf is %.lf.\n\n\n", input , result ); return (0) ; }

9 Math Library Functions
Prototypes of Library Functions: act as guides for implementation not for inclusion in your programs Parameters may be constants, variables, or expressions Compiler directive to access math library (DogNet) Think of –lm switch as "link math" gcc -lm your_file_name.c // result is a.out file OR gcc -lm your_file_name.c -o your_file_name.exe -lm switch

10 // Notice lack of formatting of output… let’s change that.
#include <stdio.h> // file 2.c in my DogNet directory #include <math.h> // Preprocessor directive to link to math library int main (void) { double input = 0.0 , result = 0.0 ; printf("\n\n Enter an value to be cubed: "); scanf ("%lf" , &input) ; result = pow(input , 3) ; // Math library function call printf("\n\n\n The value of %lf cubed is %lf.\n\n\n", input , result ); printf("\n\n Enter an value to find the square root: "); scanf ("%lf" , &input) ; result = sqrt(input) ; // Math library function call printf("\n\n\n The value of the square root of %lf is %lf.\n\n\n", input, result ); return (0) ; } // Notice lack of formatting of output… let’s change that.

11 Function Prototype Thoughts
return_type function name ( parameter(s) ) ; // use ; here Always specify a return type (void if nothing returned) Select a meaningful name Always specify parameters (void if nothing passed to the function) Names/identifiers of parameters in the prototype are acceptable BUT - be aware that names are for your clarity not the compiler’s - parameters are passed by position and not by name - naming parameters in prototypes can confuse some students

12 Function Declaration Thoughts
return_type function name ( parameter(s) ) // do not use ; here { } Specify same return type and name as prototype (void if nothing returned) Specify names and data types for parameters (void if nothing to pass) Names of parameters should be meaningful Remember: positional matching Parameter names are used like variables in function block Local variables can be declared inside function bock

13 Parameter Rules A function can have zero, one, or many parameters.
Data type and position of parameters found in prototype (remember: identifier are not useful here but are acceptable): int final_average (int , int , int ) ; Names, data types, and position found in declaration header: int final_average (int test1 , int test2 , int test3) In a function call, data types do not appear… only values, variables, constants, expressions, or (an)other function call(s) student_average = final_average (exam1, exam2 , exam3) ;

14 Parameter Rules... continued
The parameters in the prototype and declaration, must precisely match the arguments in a function call by data type and position There is no matching by name between arguments and parameters Once again: parameters and arguments are passed (linked, associated, etc…) by position - the first argument in the call argument list is linked/associated with the first parameter in the prototype parameter list and the first parameter in the declaration parameter list - the second argument in the call argument list is linked/associated with the second parameter in the prototype parameter list and the second parameter in the declaration parameter list etc…

15 Parameter Rules... continued
Arguments in the function call must have compatible data types with the parameters to which they are associated in function prototypes/declarations. Again, arguments may be variables, constants, or expressions but the data type and position must be compatible with their associated parameters An argument's name need not match its positionally-matched parameter's name, but it may… remember: it’s about matching the position and data type, it's not about matching the name This matching by position and not name applies to function prototypes, too

16 #include <stdio.h> // file 3.c in my DogNet directory
float averager (int , int , int ) ; //Function "averager" prototype int main (void) { int first = 0, second = 0, third = 0 ; // Not a great way to get input!!! Control?? float avg = 0.0 ; printf("\n Please enter three integers each separated by a space: ") ; scanf( "%d %d %d", &first , &second , &third ) ; avg = averager ( first, second, third ); // Function "averager" call printf ("\n\n% The average of your numbers is: %.2f. \n\n\n", avg); return 0; } float averager (int num1 , int num2 , int num3) //Function "averager" declaration float answer = 0.0 ; answer = (float) (num1 + num2 + num3) / 3.0 ; return answer ;

17 Example Let’s look at a programming example that implements programmer-created functions and passes parameters "by value" (passing "by address" will be introduced in a week or two). The following example is a "hyper-function" approach (it really emphasizes modularization). This is a good exam-style question.

18 Let’s write a C program that implements the following algorithm:
Introduce the program: the program converts degrees Fahrenheit into degrees Centigrade (ultimately accomplished with a programmer-created function named intro_msg ( ) ) Prompt the user to enter degrees in Fahrenheit (ultimately accomplished with programmer-created function named get_input ( ) ) Read the user’s input for degrees in Fahrenheit (ultimately accomplished with programmer-created function named get_input ( ) ) Perform conversion calculation using this formula: degrees Centigrade = 5/9 * (degrees Fahrenheit – 32) (ultimately accomplished with programmer-created function named calc_answer ( ) ) Display results (ultimately accomplished with programmer-created function named display_results ( ) ) Sign-off with user Terminate

19 Let’s Approach This Very Methodically
Let's use the “Urban Inside-Out 1-Step-at-a Time” Method

20 The "Urban Inside-Out 1-Step-at-a-Time Method"
Determine desired output(s) and input(s), then develop your algorithm Place the algorithm as a block of comments at the top of a .c file to act as a guide Focus on the first step of the algorithm Begin coding by translating this step of the algorithm to C source-code (save changes!!) Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) Compile (troubleshoot/fix, save changes, and recompile, if necessary) Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) Comment thoroughly (save changes, compile, and test one last time) Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8 until all steps of the algorithm have been satisfied Terminate

21 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
1. Develop your algorithm Done

22 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
Place the algorithm in your main( ) block of statements as a series of comments to guide your program development You could place all the algorithm steps as comments inside the main( )… this saves time commenting and it really drives the notion of making the algorithm drive your source-code development

23 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
3. Focus on the first step of the algorithm Step 1. Introduce the program: the program converts degrees Fahrenheit into degrees Centigrade (ultimately accomplished with a programmer-created function named intro_msg ( ) )

24 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
Begin coding by translating this step of the algorithm to C source-code (save changes!!) // ALGORITHM STEP 1: INTRODUCE THE PROGRAM: THE PROGRAM CONVERTS DEGREES // FAHRENHEIT INTO DEGREES CENTIGRADE (ULTIMATELY // ACCOMPLISHED WITH A PROGRAMMER-CREATED FUNCTION NAMED // intro_msg ( ) ) printf("\n This program converts degrees Fahrenheit into ") ; printf("\n degrees Centigrade. I hope you find it useful. \n\n ") ;

25 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) #include <stdio.h>

26 Use gcc and pico, as necessary
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" Compile (troubleshoot/fix, save changes, and recompile, if necessary) Use gcc and pico, as necessary

27 ./a.out… then use gcc, pico, and a.out as necessary
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) ./a.out… then use gcc, pico, and a.out as necessary

28 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
8. Comment thoroughly (save changes, compile, and test one last time) Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 3 through 8 until all steps of the algorithm have been satisfied

29 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
// STEP 2: PROMPT THE USER TO ENTER DEGREES IN FAHRENHEIT // (ULTIMATELY ACCOMPLISHED WITH PROGRAMMER-CREATED // FUNCTION NAMED get_input ( ) ) printf("\n Enter the temperature in degrees Fahrenheit: ") ;

30 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
// ALGORITHM STEP 3: READ THE USER'S INPUT FOR DEGREES IN FAHRENHEIT // (ULTIMATELY ACCOMPLISHED WITH PROGRAMMER-CREATED // FUNCTION NAMED get_input ( ) ) scanf("%lf" , &degrees_f ) ; printf("\n TEST TEST input: %lf \n\n " , degrees_f ) ;

31 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
// ALGORITHM STEP 4: PERFORM CONVERSION CALCULATION USING THIS FORMULA: // DEGREES CENTIGRADE = 5/9 * (DEGREES FAHRENHEIT - 32) // (ULTIMATELY ACCOMPLISHED WITH PROGRAMMER-CREATED // FUNCTION NAMED calc_answer ( ) ) degrees_c = 5.0 / 9.0 * ( degrees_f ) ; printf("\n TEST TEST degrees_c: %lf \n\n " , degrees_c ) ;

32 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
// ALGORITHM STEP 5: DISPLAY RESULTS (ULTIMATELY ACCOMPLISHED WITH // PROGRAMMER-CREATED FUNCTION NAMED display_results ( ) ) printf("\n\n %.1lf degrees Fahrenheit " , degrees_f ) ; printf("is equal to %.1lf degrees Centigrade. \n\n\n", degrees_c );

33 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
// ALGORITHM STEP 6: SIGN-OFF WITH USER printf("\n Thanks for using my program. Bye. \n\n\n\n ") ;

34 Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method"
// ALGORITHM STEP 7: TERMINATE return ( 0 ) ;

35 How Do We Handle the "Ultimately" Stuff?
Introduce the program: the program converts degrees Fahrenheit into degrees Centigrade (ultimately accomplished with a programmer-created function named intro_msg ( ) ) Prompt the user to enter degrees in Fahrenheit (ultimately accomplished with programmer-created function named get_input ( ) ) Read the user’s input for degrees in Fahrenheit (ultimately accomplished with programmer-created function named get_input ( ) ) Perform conversion calculation using this formula: degrees Centigrade = 5/9 * (degrees Fahrenheit – 32) (ultimately accomplished with programmer-created function named calc_answer ( ) ) Display results (ultimately accomplished with programmer-created function named display_results ( ) ) Sign-off with user Terminate

36 How Do We Handle the "Ultimately" Stuff?
One algorithm step at a time!! // ALGORITHM STEP 1: INTRODUCE THE PROGRAM: THE PROGRAM CONVERTS DEGREES // FAHRENHEIT INTO DEGREES CENTIGRADE (ULTIMATELY // ACCOMPLISHED WITH A PROGRAMMER-CREATED FUNCTION NAMED // intro_msg ( ) ) // printf("\n This program converts degrees Fahrenheit into ") ; // printf("\n degrees Centigrade. I hope you find it useful. \n\n ") ; intro_msg ( ) ; What else needs to be created to allow this function call to be used?

37 How Do We Handle the "Ultimately" Stuff?
// ALGORITHM STEP 1: INTRODUCE THE PROGRAM: THE PROGRAM CONVERTS DEGREES // FAHRENHEIT INTO DEGREES CENTIGRADE (ULTIMATELY // ACCOMPLISHED WITH A PROGRAMMER-CREATED FUNCTION NAMED // intro_msg ( ) ) // printf("\n This program converts degrees Fahrenheit into ") ; // printf("\n degrees Centigrade. I hope you find it useful. \n\n ") ; intro_msg ( ) ; What else needs to be created to allow this function call to be used? Two things simultaneously: a function prototype and a function declaration

38 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? Two things: a function prototype (below) and a function declaration (next slide). Above the main( ) in the global area: void intro_msg ( void ) ;

39 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? Two things: a function prototype (previous slide) and a function declaration (below). Below the main( ) (after return 0 ; and the closing } of the main ( ) block of statements: void intro_msg (void) { printf("\n This program converts degrees Fahrenheit into ") ; printf("\n degrees Centigrade. I hope you find it useful. \n\n ") ; return ; }

40 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? The link below contains the changes.

41 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? Let's move on to algorithm step 2.

42 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? Let's move on to algorithm step 3.

43 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? Let's move on to algorithm step 4.

44 How Do We Handle the "Ultimately" Stuff?
What else needs to be created to allow this function call to be used? Let's move on to algorithm step 5.

45 How Do We Handle the "Ultimately" Stuff?
The completed program.

46 How Do We Handle the "Ultimately" Stuff?
Compare version "g" with version "m" Both versions do exactly the same thing, however, version "m" uses PCFs

47 GHP #8 Posted later today/tonight
If you understand what we covered today and what was covered in the MPL Chapter 3 exercises, then you'll have no problem with this homework.


Download ppt "CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017"

Similar presentations


Ads by Google