Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015.

Similar presentations


Presentation on theme: "CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015."— Presentation transcript:

1 CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

2 GHP #4 (1) Missing 10 submissions If you did not submit, you need to e-mail me an schedule an appointment for help either today (10 AM to 3 PM) or tomorrow (10 AM to 4 PM) I will grade GHP #4 later today and tomorrow

3 GHP #4 (2) Several people sent me messages as part of their GHP submission telling me that they did not format their output because I didn't cover that in class.  Anything that is covered in the textbook chapters that have been assigned to read/study is absolutely appropriate for you to use/include/employ… in fact, I expect it.  I will not cover in class every single detail of every chapter that I assign  However, you will be responsible for anything that is in the chapters I assign in the reading/studying

4 Arithmetic Expressions The = is the assignment operator The expression to the right of the assignment operator may include one or more arithmetic operators The most commonly used arithmetic operators are: – addition ( + ) operator (for real or integer numbers) – subtraction ( - ) operator (for real or integer numbers) – multiplication ( * ) operator (for real or integer numbers) – division ( / ) operator (for real or integer numbers) – modulo ( % ) operator (for integer numbers)

5 Data Type of an Expression Each variable is of a single data type... however, each expression is also of a single data type (expressions resolve to a single value and that value is of a specific data type) The data type of an expression depends on the types of its operands (data type conversion) Hierarchy of data types: int is lowest, float next, double is highest If operators are *, /, +, or – then the type of the expression will be: – integer (int), if all operands are integer – float, if at least one operand is float and there is no double) – double, if at least one operand is double

6 Data Type of an Expression What is the data type of each expressing below? int first = 7, last = 11; float second = 2.5; double third = 6.0; first * last last + second third / second first - third first * last - second / third

7 Data Type of an Expression What is the data type of each expressing below? int first = 7, last = 11; float second = 2.5; double third = 6.0; first * last this expression's data type is int last + second this expression's data type is float third / second this expression's data type is double first - third this expression's data type is double first * last - second / third this expression's data type is double

8 Data Type of an Expression Division operation normally produces a real result on paper However, only the integer part of the result will be considered if the two operands in an expression are of type int (even if the variable on left hand side of the assignment operator is float). float batting_avg = 0.0 ; int at_bats = 500, hits = 200 ; batting_avg = hits / at_bats ; Results?

9 Data Type of an Expression Division operation normally produces a real result on paper However, if the operands are of data type int then the result of the division operation is an int (even if the variable on left hand side of the assignment operator is a float or double ) float batting_avg = 0.0 ; int at_bats = 500, hits = 200; batting_avg = hits / at_bats; The result will be 0.000 stored in batting_avg when we are really expecting.400… the data type of the expression is int… in integer division, the “fractional” component is truncated to produce the integer value 0, which is then stored in batting_avg as a float

10 Data Type of an Expression The data type of the expression can be converted to any other data type before resolving the expression to a single value Use the name of the data type between parentheses just to the right of the assignment operator (=) This is know as explicit casting (cast operator converts the first operand's data type to another data type) float batting_avg = 0.0 ; int at_bats = 500, hits = 200; batting_avg = (float) hits / at_bats ; Now the value stored in batting_avg will be.400 because the data type of the expression is float

11 Data Type of an Expression # include // My file 1.c… let’s also talk about mismatched int main(void) // format specifiers (float) hits / at_bats { int at_bats = 500, hits = 200 ; float batting_avg = 0.0 ; batting_avg = hits / at_bats ; // integer division produces an integer printf("\n\n The uncasted integer batting average is %f \n\n", batting_avg) ; batting_avg = (float) hits / at_bats ; // an example of "casting" printf("\n\n The casted batting average is %f \n\n", batting_avg) ; return (0) ; }

12 Data Type of an Expression # include // My file 1a.c… notice the ( ) which means the expression int main(void) // is resolved before casting (float) ( hits / at_bats ) { int at_bats = 500, hits = 200 ; float batting_avg = 0.0 ; batting_avg = hits / at_bats ; // integer division produces an integer printf("\n\n The uncasted integer batting average is %f \n\n", batting_avg) ; batting_avg = (float) ( hits / at_bats ) ; // an example of "casting" and ( ) printf("\n\n The casted batting average is %f \n\n", batting_avg) ; return (0) ; }

13 Data Type of an Expression # include // My file 2.c (float) first / second int main(void) { int first = 13, second = 5, remainder = 0, int_quotient = 0 ; float real_quotient = 0.0 ; remainder = first % second ; printf("\n\n The remainder is: %d", remainder) ; int_quotient = first / second ; printf("\n\n The int_quotient is: %d", int_quotient) ; real_quotient = first / second ; printf("\n\n The first real_quotient is: %f", real_quotient) ; real_quotient = (float) first / second ; printf("\n\n The second real_quotient is is %f \n\n", real_quotient) ; return (0) ; }

14 Data Type of an Expressions # include // My file 2a.c … (float) ( first / second ) int main(void) { int first = 13, second = 5, remainder = 0, int_quotient = 0 ; float real_quotient = 0.0 ; remainder = first % second ; printf("\n\n The remainder is: %d", remainder) ; int_quotient = first / second ; printf("\n\n The int_quotient is: %d", int_quotient) ; real_quotient = first / second ; printf("\n\n The first real_quotient is: %f", real_quotient) ; real_quotient = (float) ( first / second ) ; printf("\n\n The second real_quotient is is %f \n\n", real_quotient) ; return (0) ; }

15 Data Type of an Expression # include // My file 2b.c … let's play… watch the video to see int main(void) // what we do in class { float first = 13.0 ; float second = 3.3 ; printf("\n\n Result : %f \n\n\n", 300.9 ) ; return (0) ; }

16 Operator Precedence Rules Arithmetic expressions inside parentheses are executed first (left to right) Unary operators (negative and positive signs) are executed before multiplications, divisions and modulo operations Additions and subtractions are executed last OperatorsAssociativityExecution Order ( )Left to RightFirst + - (unary)Right to Left * / %Left to Right + -Left to RightLast

17 Operator Precedence Practice ans = 2.0 + 4 * 4 ; ans = 15.0 / (9 - 7) ; ans = 22.0 + 15 % 3 ; ans = 2.0 * 3 / 4 + 5 ; ans = 10.0 - 9 + 8 *7 ; ans = 3.0 + 6 * (3 + 6) ; ans = 3.0 * 6 + 3 % 6 ; ans = 3.0 * (6 % (6 - 3)) ; » Assume that ans is of data type float

18 Operator Precedence Practice ans = 2.0 + 4 * 4; ans = 18.0 2.0 + (4*4) = 18.0 ans = 15.0 / (9 – 7); ans = 7.5 15.0 / 2 = 7.5 ans = 22.0 + 15 % 3; ans = 22.0 22.0 + (15 % 3) =22.0 ans = 2.0 * 3 / 4 + 5; ans = 6.5 ((2.0 *3)/4) + 5 = 6.5 ans = 10.0 – 9 + 8 * 7; ans = 57.0 10.0 – 9 + (8 * 7) = 57.0 ans = 3.0 + 6 * (3 + 6); ans = 57.0 3.0 + (6 * (3+6)) = 57.0 ans = 3.0 * 6 + 3 % 6; ans = 21.0 (3.0*6) + (3 % 6) = 21.0 ans = 3.0 *(6 % (6 – 3)); ans = 0.0 (3.0 * (6 % (6 – 3)) =0.0 » Assume that ans is of data type float

19 Arithmetic Expressions # include // My file 3.c int main(void) { int first = 12, second = 7, remainder = 0, quotient_i = 0 ; float quotient_f = 0.0; remainder = first % second ; quotient_i = first / second ; quotient_f = (float) first / second ; printf("\n\n The remainder is %d \n\n", remainder) ; printf("\n\n The integer division quotient is %d \n\n", quotient_i) ; printf("\n\n The casted division quotient is %f \n\n", quotient_f) ; return (0) ; }

20 Arithmetic Expressions # include // My file 3.c int main(void) { int first = 12, second = 7, remainder = 0, quotient_i = 0; float quotient_f = 0.0; remainder = first % second ; quotient_i = first / second ; quotient_f = (float) first / second ; printf("\n\n The remainder is %d \n\n", remainder) ; printf("\n\n The integer division quotient is %d \n\n", quotient_i) ; printf("\n\n The casted division quotient is %f \n\n", quotient_f) ; return (0) ; } remainder = 5 quotient_i = 1 quotient_f = 1.714286

21 Playing With Format Specifiers Chapter 2 does a really good job explaining how to format the output of numbers

22 Playing With Format Specifiers # include // My file 3.c int main(void) { int first = 12, second = 7, remainder = 0, quotient_i = 0; float quotient_f = 0.0; remainder = first % second ; quotient_i = first / second ; quotient_f = (float) first / second ; printf("\n\n The remainder is %d \n\n", remainder) ; printf("\n\n The integer division quotient is %d \n\n", quotient_i) ; printf("\n\n The casted division quotient is %f \n\n", quotient_f) ; return (0) ; }

23 Playing With Format Specifiers # include // My file 4.c int main(void) { int first = 12, second = 7, remainder = 0, quotient_i = 0; float quotient_f = 0.0; remainder = first % second ; quotient_i = first / second ; quotient_f = (float) first / second ; printf("\n\n The remainder is %0d", remainder) ; printf("\n\n The remainder is %1d", remainder) ; printf("\n\n The remainder is %2d", remainder) ; printf("\n\n The remainder is %3d", remainder) ; printf("\n\n The remainder is %01d", remainder) ; printf("\n\n The remainder is %02d", remainder) ; printf("\n\n The remainder is %03d \n\n", remainder) ; return (0) ; }

24 Playing With Format Specifiers # include // My file 5.c int main(void) { int first = 12, second = 7, remainder = 0, quotient_i = 0; float quotient_f = 0.0; remainder = first % second ; quotient_i = first / second ; quotient_f = (float) first / second ; printf("\n\n The casted division quotient is %7.0f ", quotient_f) ; printf("\n\n The casted division quotient is %7.1f ", quotient_f) ; printf("\n\n The casted division quotient is %7.2f ", quotient_f) ; printf("\n\n The casted division quotient is %7.3f ", quotient_f) ; printf("\n\n The casted division quotient is %7.4f ", quotient_f) ; printf("\n\n The casted division quotient is %7.5f \n\n", quotient_f) ; return (0) ; }

25 Playing With Format Specifiers # include // My file 6.c int main(void) { int first = 12, second = 7, remainder = 0, quotient_i = 0; float quotient_f = 0.0; remainder = first % second ; quotient_i = first / second ; quotient_f = (float) first / second ; printf("\n\n The casted division quotient is %-7.0f ", quotient_f) ; printf("\n\n The casted division quotient is %-7.1f ", quotient_f) ; printf("\n\n The casted division quotient is %-7.2f ", quotient_f) ; printf("\n\n The casted division quotient is %-7.3f ", quotient_f) ; printf("\n\n The casted division quotient is %-7.4f ", quotient_f) ; printf("\n\n The casted division quotient is %-7.5f \n\n", quotient_f) ; return (0) ; }

26 Functions – A Little Review and A Little New What is a function? Why are functions useful? What are the characteristics of a function? What is a programmer-created function? How do we create programmer-created functions?

27 What is a function? Function can be thought of as a module or a subprogram  A named block that is designed to perform a specific task within a program Examples of functions with which we’ve worked:  main( ), printf ( ), and scanf( )

28 Functions Are Very Flexible Tools Many useful functions are found in libraries  See Table 3.1 and Appendix B in the textbook Some functions are general while others are very specific in nature and use Some functions have parameters and some do not Some functions return a single value and some do not Some functions have side-effect(s) and some do not http://encyclopedia.thefreedictionary.com/side- effect+%28computer+science%29 Function libraries are not panaceas: we need ability to create our own functions (programmer-created functions)

29 Why are functions useful? They facilitate the use of structured programming - Top-down design - Code reusability - Information hiding - Abstraction

30 Structured Programming Structured Programming is a problem-solving strategy and a programming methodology that has the following two goals:  The flow of control in a program should be as simple as possible  The construction of a program should embody top-down design

31 Top-down Design Top-down design, also referred to as stepwise refinement, it consists of repeatedly decomposing a problem into smaller problems.  Smaller problems solved via smaller solutions (programs, sub-programs or modules)  Each piece is more manageable than the original program  Requires integration of smaller programs (programs, sub- programs or modules) into a mosaic that satisfies the larger/overarching goal

32 Code Reusability Code reusability addresses the ease with which software programs/modules/functions) can be used in other programs. Goals include:  Allowing programs to take advantage of other programmers’ knowledge/expertise  Saving time and resources  Making a programmer’s life easier

33 Information Hiding Information hiding is a concept in which the data structure and the implementation of its operations are not known by the user. C language examples with which we are familiar:  printf( ) and scanf( ) Practical examples of a “black box” might include:  picture-taking machine at an amusement park  car engine or transmission

34 Function Characteristics Functions are composed of three elements/pieces: - Prototype - Declaration - Call Functions found in libraries will have their prototypes and declarations inside the library… therefore, we do not include prototypes and declarations for library functions in the source-code that we write

35 Function Characteristics When we write functions in our C code these functions will be known as programmer-created functions In our programs we must provide all three components of a function for all programmer-created functions - Prototype - Declaration - Call Library functions vs. programmer-created functions

36 Programmer-created Function Prototype Always located before main( ) function… provides a "hint" to compiler of what is to come that isn't standard C or in a library General syntax: ([ ]) ; Return type may be void or a single data type only Parameters may be void or parameters may be numerous and of various (mixed) data types Parameters may be named but are not usefully named in the function prototype… only data type(s) are required in prototypes Prototype examples: int add_two_integers ( int, int ) ; same as int add_two_integers ( int perch, int trout ) ;

37 Programmer-created Function Declaration Always located after main( ) function Contains all the source code for that function ( [ ] ) { C programming code ; return ; } Parameters (aka formal parameter or formal arguments) must be named in the function declaration  Parameters are memory objects Local variables (variables inside the function) may be used

38 Programmer-created Function Declaration Example: int add_two_integers ( int first, int second ) { return (first + second) ; }

39 Programmer-created Function Call Located inside main( ) function or inside another function = ([ ]) ; or ([ ]) ; Examples: sum = add_two_integers (12, 34) ; display_greeting ( ); programmer-created functions calls arguments list

40 Another example (the whole picture in segments): int cuber ( int ) ; // Function Prototype ******************************************************* int cuber ( int num1 ) // Function Declaration Begins { int answer; answer = num1 * num1 * num1 ; return answer; } // Function Declaration Ends ******************************************************* n = cuber(5) ; // Function Call

41 The next slide puts everything together.

42 # include // file 7.c in my DogNet home directory int cuber ( int ) ; // Programmer-created function cuber prototype int main (void) { int input = 0, result = 0 ; // Local variables for main( ) declared printf("\n\n Enter an integer value to be cubed: "); scanf ("%d", &input) ; result = cuber ( input ) ; // Programmer-defined function cuber call printf("\n\n\n The value of %d cubed is %d.\n\n\n", input, result ); return (0); } int cuber ( int num1 ) // Programmer-created function cuber declaration start { int answer; // Programmer-created function cuber local variable answer = num1 * num1 * num1 ; return answer; } // Programmer-created function cuber declaration end

43 Another example on the next slide… this example demonstrates a side-effect and an empty parameters list.

44 # include // My file 8.c void display_message (void) ; // PCF Prototype int main ( void ) { display_message ( ) ; // PCF Call return (0); } void display_message (void) // PCF Definition… shows one type of // “side effect” { printf(" \n\n What do you want for nothing? \n") ; printf(" Rubber Biscuit! \n\n\n " ) ; return ; } never place void in a function call

45 /* An example demonstrating local variables and side-effects … my file 9.c */ # include // make sure to use a symbol table/memory map void function_1 (void); // PCF Prototype int main (void ) { int num_1 = 22; // num_1 used as variable in main() printf("%d \n", num_1); function_1( ); // PCF Call printf("%d \n", num_1); return (0); } void function_1 (void) // PCF Declaration { int num_1 = 55; // num_1 used as variable in function_1 printf("%d\n", num_1); num_1= num_1 + 17 ; printf("%d\n", num_1) ; return ; }

46 /* An example demonstrating local variables, scoping, and side-effects … 9.c */ # include // make sure to use a symbol table/memory map void function_1 (void); // PCF Prototype int main (void ) { int num_1 = 22; // num_1 used as variable in main() printf("%d \n", num_1); function_1( ); // PCF Call printf("%d \n", num_1); return (0); } void function_1 (void) // PCF Declaration { int num_1 = 55; // num_1 used as variable in function_1 printf("%d\n", num_1); num_1= num_1 + 17 ; printf("%d\n", num_1) ; return ; } Output 22 55 72 22

47 The Return Statement When a return statement is executed, program control is immediately passed back to the calling environment/function. If an expression follows the keyword return, the value of the expression is returned to the calling environment as well. A return statement has one of the following two forms: return; return expression;

48 The Return Statement return; return answer ; // Assuming answer is a variable return first + second ; // Assuming first and second are variables return (a+b+c); // Assuming a, b, and c are variables

49 Pico Shortcuts (one more time) ctrl-w followed by ctrl-t produces a prompt that asks you on which line you like the cursor to be positioned ctrl-c produces the number of the line on which the cursor is currently positioned ctrl-y moves the cursor up one page ctrl-v moves the cursor down one page ctrl-k deletes an entire line


Download ppt "CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015."

Similar presentations


Ads by Google