Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 14 Miscellaneous Topics

Similar presentations


Presentation on theme: "Module 14 Miscellaneous Topics"— Presentation transcript:

1 Module 14 Miscellaneous Topics

2 @Copyright UMBC Training Centers 2012
The goto Statement Rarely used Violates principles of structured programming Causes immediate jump to label . . . myLabel: goto myLabel; @Copyright UMBC Training Centers 2012

3 @Copyright UMBC Training Centers 2012
The null statement A solitary semi-colon // cute, but not advisable while ((*to++ = *from++) != ‘\0’); NOT a Compiler Error for( int i = 0; i < size; i++); { for-loop body; } @Copyright UMBC Training Centers 2012

4 @Copyright UMBC Training Centers 2012
Union Concept Used to store different data types in the same memory area union mixed { char c; int i; float f; }; The members of a union are typically mutually exclusive The size of the union is the size of the largest member @Copyright UMBC Training Centers 2012

5 @Copyright UMBC Training Centers 2012
Union Syntax Use dot notation, same as with structs union mixed x; x.c = ‘K’; x.i = 42; // x.i is the ONLY value in x @Copyright UMBC Training Centers 2012

6 @Copyright UMBC Training Centers 2012
Let’s take a look C::B BaseUnions Unions @Copyright UMBC Training Centers 2012

7 @Copyright UMBC Training Centers 2012
Comma Operator Bottom of the precedence char Separates expressions in a single statement For-loops for(i = 0, j = 100; i < 10; ++i, --j) Create a single statement to avoid braces Not common, not recommended while( i < 100 ) sum += grades[ i ], ++i; @Copyright UMBC Training Centers 2012

8 @Copyright UMBC Training Centers 2012
Type Qualifiers register Suggest to the compiler that a variable will be heavily used and should be stored in a register rather than in main memory for faster acccess volatile The inverse of const Tell the compiler that a variable will be changed by some code. Prevents the compiler from performing improper optimizations restrict Tells the compiler that a pointer is the only reference to its pointee. Allows the compiler to perform some optimization. @Copyright UMBC Training Centers 2012

9 Command Line Arguments
Data can be passed to a program at the time it is executed rather than have the program prompt the user and wait for input myProgram 64 myFile.dat These data items are referred to as “command line arguments” @Copyright UMBC Training Centers 2012

10 @Copyright UMBC Training Centers 2012
Accessing the CLA Arguments to main are used to access the command line arguments int main( int argc, char *argv[ ]) argc is the number of command line arguments, including the program name argv is an array of strings representing the command line arguments Numeric arguments must be converted @Copyright UMBC Training Centers 2012

11 @Copyright UMBC Training Centers 2012
argc and argv At the command line myProgram 64 myFile.dat In main argc = 3 argv[0] = “myProgram” argv[1] = “64” argv[2] = “myFile.dat” @Copyright UMBC Training Centers 2012

12 @Copyright UMBC Training Centers 2012
Handling arguments Verify that the correct number of command line arguments have been entered if (3) if (argc < 2 || argc > 5) Convert numeric arguments from string to value int atoi(const char *string ); long atol( const char *string ); float atof( const char *string ); @Copyright UMBC Training Centers 2012

13 @Copyright UMBC Training Centers 2012
Let’s take a Look C::B CmdLineArgs @Copyright UMBC Training Centers 2012

14 @Copyright UMBC Training Centers 2012
Library Functions Memory functions Math functions Requires math library to be linked Automatic with Code::Blocks and other IDEs “-lm” switch on Linux command line Random numbers Sorting and Searching @Copyright UMBC Training Centers 2012

15 @Copyright UMBC Training Centers 2012
Initializing Memory memset (stdlib.h) memset(void *p, int value, size_t count); Initializes count bytes of memory to value, starting at memory location p Only useful for char arrays unless value is 0 Useful for initializing arrays to 0 @Copyright UMBC Training Centers 2012

16 @Copyright UMBC Training Centers 2012
memset Example typedef struct person { char first[20]; char last[20]; char middle; } PERSON; PERSON people[100]; memset(people, 0, 100 * sizeof(PERSON)); @Copyright UMBC Training Centers 2012

17 @Copyright UMBC Training Centers 2012
Copying Memory memcpy and memmove memcpy (void *to, void *from, size_t count); memmove(void *to, void *from, size_t count); memmove handles overlapping blocks of memory but memcpy does not, so memmove is preferred Useful for copying arrays @Copyright UMBC Training Centers 2012

18 @Copyright UMBC Training Centers 2012
memmove Example PERSON people[100]; PERSON students[100]; /* store data into the people array */ /* copy the data into the student array */ memmove(students, people, 100 * sizeof(PERSON)); @Copyright UMBC Training Centers 2012

19 @Copyright UMBC Training Centers 2012
Let’s take a Look C::B Memory @Copyright UMBC Training Centers 2012

20 Sqrt, powers, logarithms
double sqrt(double x); returns the square root of x Undefined result if x is negative double pow(double x, double y); Returns x to the y power Undefined result if x = 0 and y is not positive x is negative and y is not an integer double log10(double x); Returns the base 10 logarithm of x Undefined result if x is not positive @Copyright UMBC Training Centers 2012

21 @Copyright UMBC Training Centers 2012
Floor and Ceiling double floor(double x); Returns the largest integer(-ish) not larger than x Used to “round” x down to previous integer floor(55.76) is 55  really floor(-66.8) is -67 double ceil(double x); Returns the smallest integer (-ish) not less than x Used to “round” x up to the next integer ceil(55.76) is 56 ceil(-66.8) is -66 @Copyright UMBC Training Centers 2012

22 @Copyright UMBC Training Centers 2012
Absolute Value double fabs(double x); long labs(long x); int abs(int x); All functions return the absolute value of x labs and abs are in stdlib.h @Copyright UMBC Training Centers 2012

23 @Copyright UMBC Training Centers 2012
Random Numbers Pseudo-random number generator (RNG) stdlib.h Repeatable sequence of “random numbers” void srand( unsigned int seed ) Initializes the RNG; use it only once Determines the sequence of “random” numbers int rand( ) Returns a random integer from 0 to RAND_MAX @Copyright UMBC Training Centers 2012

24 Scaling Random Numbers
From 1 to N int x = rand( ) % N + 1; // from 1 to N From LOW to HIGH int x = rand() % (HIGH - LOW + 1) + LOW; // (100 – 200) @Copyright UMBC Training Centers 2012

25 @Copyright UMBC Training Centers 2012
Let’s take a Look C::B Random @Copyright UMBC Training Centers 2012

26 @Copyright UMBC Training Centers 2012
Sorting and Searching Linear search Use a loop to examine each element of an array until you find the one you’re looking for (or not) Binary Search Look in the middle, cut the array in half Look in the middle of one of the halves Look in the middle of one of the halves of the halves etc., etc., etc. Requires array to be sorted Quicksort Almost always the best choice Poor choice if array is already sorted @Copyright UMBC Training Centers 2012

27 @Copyright UMBC Training Centers 2012
Quicksort in C Implemented as qsort (stdlib.h) qsort parameters void pointer to array Number of elements in the array Size of each element in the array Pointer to a function that compares elements Parameters are const void pointers to two elements Returns < 0, 0, > 0 @Copyright UMBC Training Centers 2012

28 @Copyright UMBC Training Centers 2012
Binary Search in C Implemented as bsearch (stdlib.h) bsearch parameters void pointer to the key value for which to search void pointer to the array Number of elements in the array The size of each element in the array A pointer to a function that compares elements Parameters are const void pointers to two elements Returns < 0, 0, > 0 Returns Pointer to element if found NULL if not found @Copyright UMBC Training Centers 2012

29 @Copyright UMBC Training Centers 2012
Let’s take a Look C::B Sortints SortBusses Change line 63 to call compareNrPAssengers SearchBusses @Copyright UMBC Training Centers 2012

30 @Copyright UMBC Training Centers 2012
Exercises Ex1-Keno -- Random Numbers Ex2-PeopleSort – using qsort for people structs Ex3-FortuneCookies – random numbers Non-duplicate Powerball numbers seemed hard for them Ex4-Craps – rand numbers, structs, CLA @Copyright UMBC Training Centers 2012

31 @Copyright UMBC Training Centers 2012
@Copyright UMBC Training Centers 2012


Download ppt "Module 14 Miscellaneous Topics"

Similar presentations


Ads by Google