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

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
Chapter 6: Functions.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
CS 108 Computing Fundamentals Notes for Thursday September 10, 2015.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CS 108 Computing Fundamentals Notes for Thursday, September 17, 2015.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Variables, Operators, and Expressions
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
Computer Science 210 Computer Organization
Dr. Shady Yehia Elmashad
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
© 2016 Pearson Education, Ltd. All rights reserved.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Functions, Part 2 of 2 Topics Functions That Return a Value
ICS103 Programming in C Lecture 3: Introduction to C (2)
CO1401 Programming Design and Implementation
CS 108 Computing Fundamentals Notes for Tuesday, September 19, 2017
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
Functions.
User-Defined Functions
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
A function with one argument
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Introduction to Problem Solving and Programming
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
C++ for Engineers and Scientists Second Edition
Introduction to C Programming
CPS125.
Presentation transcript:

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

Example Program Using a PCF #include <stdio.h> //My file 1.c #define PI 3.14159 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 ;

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

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

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.

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.

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

#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) ; }

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

// 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.

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

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

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

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…

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

#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 ;

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.

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

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

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

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

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 http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14a.txt

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

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 ") ; http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14b1.txt

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> http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14b2.txt

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

./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

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

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: ") ; http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14c.txt

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 ) ; http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14d.txt

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 - 32 ) ; printf("\n TEST TEST degrees_c: %lf \n\n " , degrees_c ) ; http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14e.txt

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 ); http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14f.txt

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 ") ; http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14g.txt

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

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

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?

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

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

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 ; }

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. http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14h.txt

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. http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14i.txt

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. http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14j.txt

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. http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14k.txt

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. http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14L.txt

How Do We Handle the "Ultimately" Stuff? The completed program. http://web.cs.sunyit.edu/~urbanc/cs_108_sep_14m.txt

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 http://web.cs.sunyit.edu/~urbanc/cs_108_feb_01g.txt http://web.cs.sunyit.edu/~urbanc/cs_108_feb_01m.txt

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.