C Structures and Commands

Slides:



Advertisements
Similar presentations
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
BST Data Structure A BST node contains: A BST contains
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Searching and Sorting Searching algorithms with simple arrays
Algorithm Analysis 1.
Dynamic Storage Allocation
The Machine Model Memory
Chapter 12 – Data Structures
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
GC211Data Structure Lecture2 Sara Alhajjam.
C Programming Tutorial – Part I
UNIT III TREES.
Data Structures Interview / VIVA Questions and Answers
C Short Overview Lembit Jürimägi.
External Methods Chapter 15 (continued)
structures and their relationships." - Linus Torvalds
Week 6 Discussion Word Cloud.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Building Java Programs
CS 2308 Exam I Review.
Lecture 8b: Strings BJ Furman 15OCT2012.
Algorithm design and Analysis
CS 2308 Exam I Review.
Topics Introduction to File Input and Output
7 Arrays.
Find in a linked list? first last 7  4  3  8 NULL
The Elements of Programming Style
Growing Arrays in C Russell Stephens.
Data Structures Review Session
Trees Lecture 9 CS2110 – Fall 2009.
CS 201 Fundamental Structures of Computer Science
A Robust Data Structure
Binary Trees (and Big “O” notation)
CMSC 202 Trees.
Sub-Quadratic Sorting Algorithms
Building Java Programs
Binary Trees, Binary Search Trees
Introduction to Data Structures
7 Arrays.
Homework Reading Programming Assignments Finish K&R Chapter 1
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Interface Principles Omar Kabeer.
An Introduction to Programming though C++
Variables in C Topics Naming Variables Declaring Variables
Topics Introduction to File Input and Output
Binary Trees, Binary Search Trees
Searching and Sorting Hint at Asymptotic Complexity
structures and their relationships." - Linus Torvalds
SPL – PS1 Introduction to C++.
SPL – PS2 C++ Memory Handling.
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

C Structures and Commands Mark Canda CS 265

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;

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.

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

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)

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

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.

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

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.

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

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.

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.

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.

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

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

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.

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

THE END Questions?

Source Kernighan, Brian W., and Rob Pike. The Practice of Programming. Addison-Wesley, 1999. Print.