CS - 137 notes

# CS - 137 notes - CS-137: Algorithm-find the length of the...

This preview shows pages 1–13. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS-137:ProgrammingPrinciplesEuclid Algorithm-find the length of the longest stick that can measure both distance exactlyEuclud->repeated substraction-> instead, we use division806mod338=130338mod130=78…52mod26=0Input and output (chap 3)output : printf(format string, argument);input: scanf("%d", &a);Computer Memory"char" : 8 bits = 1byte : -128 to 127"int" : 32bits = 4 bytes: -2 147 483 648 to 2 147 483 647Expression:+-*/%Assignmenta=a+b;Compound Assignmenta+=2;it is the same as a=a+2;Increasement / Decresementa++ or ++aa-- or --atable of precedence:-when in doubt, use bracketsLogical/Boolean(true/false) Expressionsfalse is 0, true is non-zeroRelational operations : < > <= =>Equality operations:== !=Logical Operatorslogical NOT: !logical OR: ||logical AND: &&ex: true or false:(100>700)||(2<=7) T(100>700)&&(2<=7) F!(100>700)&&(2<=7) TDemorgan's Law!(P&&Q)==!P||!Q!(P||Q)==!P&&!Qif statementif else statementRule:elseassociated with the closest elseless if before itWhile Statement (6.2)or FunctionsAssertions(a useful trick)assert (expression);-if true => does nothing-if false=> aborts program with error message Rule : declaration before useSquare must be declared before cubebefore quarticbefore quinticCube must be declared before quarticbefore quintictesting: define main and supporting functions in a separate file. main.cFunction Declaration-external view of the function-includes : name + parameter types + return typeeg: int square(int foo);Function Definition-internal (what it does) view of the function-includes: declaration-there can be only one definitionvoid-as a return type => returns nothing-as parameter type => takes no argumentsSeparate compilation>gcc powers.c main.cpowers.c powers.omain.c main.o>./a.outRecursion-a function calling itselfeg: Find the sum of the first "n" natural numbers.1 , 3 , 6 , 10 , 15 , …+2 +3 +4 +5 +6 …eg: GCDhow it works?gcd(338,806)gcd(806, 338)gcd(338, 130)gcd(130, 78)gcd(78, 52)gcd(52,26)gcd(26,0)Array of integersint a=6; int a[5] = {10, -7, 3, 8, 42}10a[0]-7a[1]3a[2]8a[3]42a[4]Indexing the arrayprintf("%d",n), a[2]); 3Assignmenta[3]=100; Add up elements- array of size nForloop(section 6.3)it is equivalent toeg:Sieve of Eratosthenes:(find all prime numbers up to specified number n)-start at 2-strike out multiples of 2 (other than 2)-find next hight number not struke out-strike out multiples of that number (not the number)-repeateg: up to 20int a[4] = {100 , 200, 300 , 400 }48…x100a[0]200a[1]300a[2]400a[3]…..………….……a[4]int a[5] = { 12, 13, 14 , 15}clear (a, 5);aCreates storage for arrayint a[5]= {-10, 15, 7, -3, 2} initialized arraya-10157-32int b[10]={1,2,3} remaining elements are zerob123…int c[1000]={0} all elements are zerocint d[1000];...
View Full Document

## This note was uploaded on 12/10/2011 for the course CS 137 taught by Professor Clarke during the Fall '10 term at Waterloo.

### Page1 / 41

CS - 137 notes - CS-137: Algorithm-find the length of the...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online