{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs notes

# cs notes - cs notes Recursion – function calling itself...

This preview shows pages 1–5. 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 is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cs notes Recursion – function calling itself Int gcd (int a , int b) { if(b==9); return a; else gcd(b,a%b); } Sieve of Eratosthenes: a method of finding all the prime numbers up to a specified number n up to 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-strike out 1-start at 2-strike out multiples of 2 (other than 2)-find next highest number not struck out-strike out that number's multiples -repeat-strike out multiples of 2 (not including 2) CS137 oct 5 create storage for array int a[5] = {-10,15, 7, -3. 2 }; a0[-10] a1[15] a2 int b[10]={1,2,3} 0[1] 2[2] 3[3] 4[0] 5[0]… int d[1000] = {0}; (all elements are 0) int d[1000]; //uninitialized array [?]0 … [?]999 Arguments to functions-In C, arguments are passed by value void weird(int n) { n=17; } int main(void) int n=6; weird(n); printf(“%d\n”,n) return 0; } prints 6. Array arguments void one23 (int a, int n) { int i; for(i=0;i<n;i++) a[i]=i+I; } int main(void){ int a[1000]; one23(a,1000) printf(a[772]) prints 773 so array is passed by reference. sizeof: sizeof(type) sizeof(variable) these expressions return type or variable (usually in bytes) int i=7; int a[10]={0}; sizeof(int)=4; sizeof(i)=4; sizeof(char)=1 sizeof(a)=40 a[ x ] printf sizeof(a) =8 printf n = 10 printf(“%ld\n”,(longint)a); =140733275212096 an in array in c is really a pointer to an int int * a a is ^ pointer to an int void strange (int*a, int n) a[ x ] n [10] int main(void) { int a[5]={0,1,2,3,4} z is position int *b=a; b[x] printf(“%d\n”, a[2]);=2 b[2]=100; print (a[2]))=100 Scientific notations-2.6307 x 10^30 inc C: float x; single precision floating point 32 bits double ; double precision floating point 64 bits. #include <stdio.h> int main(void) { double x=-2.6302e30; printf(%d//n”,sizeof(float)); =>4 printf(“%d\n”,sizeof(double));=>5 printf(%f\n”,x); =>-26302000000000001035938475983749.00000000 printf(%e\n,x); =>-2.630e30 printf(“%g\n”,x);=>2.630e+30 IEEE floating point standard 63 (sign, 1bit) 53 double signs 63-52:expoinent (with sign, 11 bits) fraction (51-0), 52 bits. value (O-1) sign x fraction x 2(exponent) (sort of) Floating point provides an approximate representation for real numbers (ie, no irrational numbers); Error in floating point numbers r-real number you’re trying to approximate eg Pi p – floating point representation absolute error |r-p| eg |Pi-3.14| = 0.00159265358979 relative error eg |r-p|/|r| eg |pi-3.14|/|pi| = 0.0005670 Relative error can be large even when error is small. avoid:-subtracting nearly equal values-dividing very small numbers-multiplying by very large numbers-tests for equality BAD if(x==7){ } GOOD (if x-y<0.0001||y-x<0.0001){ } but test against integer constants can be okay if (x=-0), if (x==1.0) #include <stdio.h> int main (void){ double x=5.0/6.0; double y=1.0/2.0 double z=1.0/3.0 if (x-y==z) print yes else print no 5/6-1/2 = 1/3 (logical answer) printf (5g\n”,(x-y) –z); => 5.55||5.55112e-17 (real answer)printf (5g\n”,(x-y) –z); => 5....
View Full Document

{[ snackBarMessage ]}

### Page1 / 41

cs notes - cs notes Recursion – function calling itself...

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

View Full Document
Ask a homework question - tutors are online