Download presentation
Presentation is loading. Please wait.
1
C Structures and Commands
Mark Canda CS 265
2
Naming Descriptive globals and short locals. Ex.
Clarity can be achieved though brevity. Ex. for (theElementIndex = 0; theElementIndex < numberOfElements; theElementIndex++) elementArray[theElementIndex] = theElementIndex; Compared to: for ( i = 0; i < nelems; i++) elem[i] = I;
3
Naming Cont. Give all things that are related to one another related names but show their differences and relationship. Use verbs for functions followed by nouns. Make sure your names aren’t misleading.
4
Expressions/Statements
Indenting will make your code much easier to read and organize. Ex: for(n++;n<100;field[n++]=‘\0’); *i = ‘\0’ ; return(‘\n’); Make it into: for (n++; n < 10; n++) field[n] = ‘\0’ ; *i = ‘\0’ ; return ‘\n’ ;
5
Expressions/Statements Cont.
Make sure your expressions are in natural form. Ex: if(!(x < y) || !(x >= z)) Compared to just: if((x >= y) || (x < z)) Parenthesize even when you don’t have to. This will help to keep certain expressions clear. if (x & MASK == BITS)
6
Expressions/Statements Cont.
You don’t have to cram everything into one line. Make sure your code is clear. You don’t have to worry about the size of your code as long as it is clear and gets the job done. Be careful of code that have side effects. Ex: The operator ++ has side effects. 1. It returns a value 2. Modifies a variable
7
Consistency/Idioms Make sure your indentations are consistent and again use braces or parentheses even when they aren’t needed to keep things clear. Use C/C++ idioms to keep things consistent. When dealing with multi-way decisions, else if statements are the recommended way to express them, although switch statements may also be used.
8
Function Macros Avoid using function macros. They cause a lot of problems and causes bugs in your program that you won’t pick up until later. In the case that you do use a function macro, just make sure to parenthesize the argument and macro body. Ex: #define square(x) ((x) * (x))
9
Magic Numbers Magic numbers are the constants, array sizes, character positions, conversion factors, and other literal numeric values that appear in programs. Any number that’s not a 0 or 1 should be given a name. Don’t use macros to define a number, instead use constants. You may use an enum statement or define it with the use of const.
10
Magic Numbers Cont. Use character constants whenever you can instead of using integers. Ex: Using NULL instead of 0 str = 0; should rather be str = NULL; You want to leave integers to literally be just integers and use explicit constants. Use the sizeof operator when determining the size of an object. Ex: char buf[1024] ; fgets(buf, sizeof(buf), stdin);
11
Commenting Don’t state what is obviously happening. Comments are used to clarify the code not recite it. Comment functions and global variables. Use the comments to explain what’s happening in functions. If the code is confusing and causes you to write a lengthy comment, rewrite your code. When your code changes make sure your comments follow and not contradict it.
12
Searching Sequential/Linear Search-searches through each element until it finds the one it wants This type of search is better used for arrays with a few amount of elements. There are different routines to do sequential searches for different data types. Ex: strchr, strstr, find algorithms Binary Search-searches in an orderly way and is much faster than linear search The elements must be sorted in this type of search.
13
Sorting quicksort function-works by partitioning an array into two groups of elements then recursively sorting the two groups This sorting is the fastest way to sort elements because it doesn’t have to compare all the elements to each other. There are several different variations of quicksort.
14
Libraries Functions qsort- can sort any data type; needs a comparison function whenever comparing two values The comparison function uses void* pointers since the values can be any data type. bsearch-can also search any data type and like qsort, besearch needs a pointer to a comparison function
15
O-Notation O-notation-the standard notation to compare the running times and space requirements of algorithms independently of programming language and other factors Notation Name Example O(1) constant array index O(logn) logarithmic binary search O(n) linear string comparison O(nlogn) nlogn quicksort O(n^2) quadratic simple sorting methods O(n^3) cubic matrix multiplication O(2^n) exponential set partitioning
16
Lists Lists are exactly the size they needs to be to hold its contents and can be rearranged by exchanging pointers Lists are beneficial to use when you have data that is susceptible to change unlike arrays whose data are static. You will be able to add items, delete items and find specific items in a list.
17
Trees tree-a hierarchical data structure that stores a set of items in which each item has a value and is pointed to by exactly one other except for the root in-order-executes after visiting the left subtree and before visiting the right subtree post-order-executes on the current node after visiting the children pre-order-executes on the current node then visits the left subtree before the right
18
THE END Questions?
19
Source Kernighan, Brian W., and Rob Pike. The Practice of Programming. Addison-Wesley, Print.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.