Unformatted text preview:

Header FilesRandom Number Generation using the standard library #include <stdlib.h>Sample programFunctions with Empty Parameter ListsDefault ArgumentsSample program using call-by-reference:#include <stdio.h>void swap(int *i, int *j);int main(void){int num1, num2;num1 = 100;num2 = 800;printf(“num: %d num2: %dn”,num1, num2);swap(&num1, &num2);printf(“num: %d num2: %dn”,num1, num2);return 0;}void swap(int *i, int *j){int temp;temp = *i;*i = *j;*j = temp;}Storage classesScope Rules#include <stdio.h>long factorial( long );int main(){int i;for ( i = 1; i <= 10; i++ )printf( "%2d! = %ldn", i, factorial( i ) );return 0;}long factorial( long number ){if ( number <= 1 )return 1;elsereturn ( number * factorial( number - 1 ) );}Recursion vs InterationIS 12 1 IS12 – Introduction to ProgrammingDate: February 19, 2003IS 12 2Preprocessor Directives - #defineThe preprocessor directives include #define and the #include. The #define directive is used to create constants. When the #define is used any occurrence of the identifer will bereplaced before the program is compiled. Example:#define PI 3.14159Wherever this is referred to in a program, it will place 3.14159 in place of PI. Another example:#define AGELIMIT 21.if (employeeAge < AGELIMIT) /*logic here when true */AGELIMIT will be replaced with 21 in the statement.Use upperclase letters for constants so as to distinquish from other types of variables. Defining and using constants at the beginning of the program will facilitate easier program maintenance. Defining the constant prior to main will allow for the constant to be used by all logic thereafter in the program.Another type of preprocessor directive is the macro. A macro is defined with a #define preprocessor directive statement. For example:#define PI 3.14159#define CIRCLE_AREA(X) ( (PI) * (x) * (x) )Whenever we call this as in area = CIRCLE_AREA(10);the value of 10 is substitued for x in the replacement define preprocessor directive. This expands to area = ( ( 3.14159) * (10) * (10) );IS 12 3Preprocessor Directive - #includeThe #include statement is a merge-type of operation. Right before the program is compiled, the #include command is replaced with the file content specified in the #include statement. The #include has two formats#include <filename>#include “filename”See chapter seven as an example of code usage. When including the filename, be aware that some operating sytems, such as unix, are case-sensitive. To use a built-in library function, you must include the .h (header file) in the include statement. Header files are text files that contain C code. An example of this is the#include <stdio.h>This preprocessor directive allows us to use functions printf and scanf in our programs. Header FilesVarious standard libraries are available that contain function prototypes and function definitions. To create a library file, type it and save it with a .h extension. The use the #include preprocessor directive to include the header file in your program. FunctionsThe focus of functions or modules or subprograms is to facilitate large programming activities such as design, implementation, operation, and maintenance. Program modules are also referred to as functions. Languages such as Pascal call functions procedures so be aware of the different terminology that represents the same thing in programming. There are two ways to work with functions. These include standard functions that are already defined and are available from the appropriate library (like stdio.h), as well as functions written by the programmer.Functions defined in the standard library are readily available to the programmer. To use these standard functions, the programmer must know what the purpose of the function is, as well as what the function needs in terms of data (input) and what type of results it will return (information). These include functions to perform operations such asmathematical capabilities, string manipulations, character manipulations, input/output, error checking, etc Common standard function libraries include: stdio.h (printf and scanffunctions), stdlib.h (contains the rand function for random number generation), string.h (functions for string processing), math.h (functions for math library), ctype.h (functions for character manipulations), errno.h (error reporting function conditions), and time.h (contains functions for time and date). Thus, we must include the appropriateIS 12 4preprocesor directive in the program if we want to use any of the functions found in the standard libraries.Random Number Generation using the standard library #include <stdlib.h>The rand function generates a integer value between 0 and 32767. rand() % 6 generates integer values in the range of 0 to 5. One is added to shift the numbers being generated..unsigned is used which is short for unsigned int. The range can be from 0 to 65,535. Generalize random formula with n = a + rand() % b; a is the shifting value (first value in the range of consecutive integers) b is the scaling factor (width of the desired range of consecutive integers) Sample program using the rand() function#include <stdio.h>#include <stdlib.h>int main(){ int i; for ( i = 1; i <= 20; i++ ) { printf( "%10d", 1 + ( rand() % 6 ) ); if ( i % 5 == 0 ) printf( "\n" ); } return 0;}Functions that are written by the programmer are referred to as programmer-defined functions. As we break a program into modules or functions, we need to have each function perform a specific task. This is the philosophy of problem solving and top downdesign. Using functions helps in the divide-and-conquer programming development Structure charts are often used to show the breakdown of all of the modules.Cohesive functions or subprograms - can be defined as the degree to which the function or subprogram performs a single task. Functions also lend themselves to software reusability. This allows previously defined code to be reused by other programs.IS 12 5A function is invoked by a function call. The function call indicates the name of the function and provides arguments (another name for arguments is parameter or variable) that the called function needs to complete the task. calling function arguments called function return Using functions eliminates duplication of code. Placing code in a function and calling it from various locations is easier than duplicating the code in each area. Function definitions -


View Full Document

Pitt INFSCI 0012 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?