Module 14 Miscellaneous Topics

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming Languages and Paradigms The C Programming Language.
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
 2006 Pearson Education, Inc. All rights reserved Arrays.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2006 Pearson Education, Inc. All rights reserved Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Functions Why we use functions C library functions Creating our own functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Generic Programming in C
Chapter 9: Value-Returning Functions
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Stack and Heap Memory Stack resident variables include:
Lesson #8 Structures Linked Lists Command Line Arguments.
Arrays Chapter 7.
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
Mathematical Functions
5.13 Recursion Recursive functions Functions that call themselves
Functions and an Introduction to Recursion
A bit of C programming Lecture 3 Uli Raich.
C Characters and Strings
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Languages and Paradigms
Number guessing game Pick a random number between 1 and 10
Quiz 11/15/16 – C functions, arrays and strings
C Short Overview Lembit Jürimägi.
CSC113: Computer Programming (Theory = 03, Lab = 01)
This technique is Called “Divide and Conquer”.
Programming Paradigms
Deitel- C:How to Program (5ed)
C Basics.
Programmazione I a.a. 2017/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
7 Arrays.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Chapter 14 - Advanced C Topics
Chapter 6 - Functions Outline 5.1 Introduction
Miscellaneous functions
Assignment Operators Topics Increment and Decrement Operators
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Assignment Operators Topics Increment and Decrement Operators
Arrays Chapter 7.
7 Arrays.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
C Language B. DHIVYA 17PCA140 II MCA.
Assignment Operators Topics Increment and Decrement Operators
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Module 5 Working with Arrays
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Module 14 Miscellaneous Topics

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

@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; } www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a look C::B BaseUnions Unions www.umbctraining.com @Copyright UMBC Training Centers 2012

@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; www.umbctraining.com @Copyright UMBC Training Centers 2012

@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. www.umbctraining.com @Copyright UMBC Training Centers 2012

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” www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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” www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 ); www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a Look C::B CmdLineArgs www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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)); www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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)); www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a Look C::B Memory www.umbctraining.com @Copyright UMBC Training Centers 2012

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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 55.0000000 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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

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) www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Let’s take a Look C::B Random www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

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

@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 www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 www.umbctraining.com @Copyright UMBC Training Centers 2012