cs 108 computing fundamentals notes for tuesday, september 15, 2015

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

Upload: randall-lane

Post on 18-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

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

CS 108 Computing FundamentalsNotes for Tuesday, September 15, 2015

Page 2: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 3: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 4: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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)

Page 5: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 6: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 7: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 8: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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?

Page 9: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 10: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 11: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Data Type of an Expression

# include <stdio.h> // My file 1.c… let’s also talk about mismatchedint 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) ;

}

Page 12: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Data Type of an Expression

# include <stdio.h> // My file 1a.c… notice the ( ) which means the expressionint 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) ;

}

Page 13: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Data Type of an Expression# include <stdio.h> // 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) ;

}

Page 14: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Data Type of an Expressions# include <stdio.h> // 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) ;

}

Page 15: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Data Type of an Expression# include <stdio.h> // 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) ;

}

Page 16: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Operators Associativity Execution Order

( ) Left to Right First

+ - (unary) Right to Left

* / % Left to Right

+ - Left to Right Last

Page 17: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 18: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 19: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Arithmetic Expressions# include <stdio.h> // 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) ;

}

Page 20: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Arithmetic Expressions# include <stdio.h> // 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

Page 21: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Playing With Format Specifiers

• Chapter 2 does a really good job explaining how to format

the output of numbers

Page 22: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Playing With Format Specifiers

# include <stdio.h> // 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) ;

}

Page 23: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Playing With Format Specifiers# include <stdio.h> // 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) ;

}

Page 24: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Playing With Format Specifiers# include <stdio.h> // 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) ;

}

Page 25: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Playing With Format Specifiers# include <stdio.h> // 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) ;

}

Page 26: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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?

Page 27: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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( )

Page 28: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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)

Page 29: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Why are functions useful?

• They facilitate the use of structured programming

- Top-down design

- Code reusability

- Information hiding

- Abstraction

Page 30: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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 possibleThe construction of a program should embody top-down design

Page 31: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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 programRequires integration of smaller programs (programs, sub-

programs or modules) into a mosaic that satisfies the larger/overarching goal

Page 32: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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/expertiseSaving time and resourcesMaking a programmer’s life easier

Page 33: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 34: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 35: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 36: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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> <function name> ([<parameters>]) ;• 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 ) ;

Page 37: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Programmer-created Function Declaration

• Always located after main( ) function

• Contains all the source code for that function

<return type> <function name> ( [<parameters>] )

{ C programming code ;

return <value of return type>;

}

• 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

Page 38: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Programmer-created Function Declaration

• Example:

int add_two_integers ( int first , int second )

{

return (first + second) ;

}

Page 39: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

Programmer-created Function Call

• Located inside main( ) function or inside another function

<variable of return type> = <function name> ([<arguments>]) ; or<function name> ([<arguments>]) ;

• Examples:

sum = add_two_integers (12 , 34) ;

display_greeting ( );

programmer-created functions calls

arguments list

Page 40: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

• 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

Page 41: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

The next slide puts everything together.

Page 42: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

# include <stdio.h> // 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

Page 43: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 44: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

# include <stdio.h> // 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

Page 45: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

/* An example demonstrating local variables and side-effects… my file 9.c */# include <stdio.h> // 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 ;}

Page 46: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

/* An example demonstrating local variables, scoping, and side-effects… 9.c */# include <stdio.h> // 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

22557222

Page 47: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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;

Page 48: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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

Page 49: CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015

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