Study Guides (248,333)
Canada (121,489)
CS 137 (4)
Midterm

Programming Midterm Notes.docx

7 Pages
284 Views
Unlock Document

Department
Computer Science
Course
CS 137
Professor
Andrew Morton
Semester
Fall

Description
Programming Midterm Notes October 19, 2012 Algorithms GCD int gcd (int a, int b) { int r= a%b; while (r!=0) { a=b; b=r; r=a%b; } return b; } Horner’s Method double horner (double f[], int n, double x) { assert (n>0); // f[0]=2.0, f[1]=9.0, f[2]=4.0, f[3]=3.n=4 double h = f[n-1]; for (int i=n-2; i>=0; i--) h=h*x + f[i]; return h; } Bisection double f (double x) { return x-cos(x); } double bisect (double a, double b, double epsilon, int maxIters) { assert (f(a)<0.0 && f(b)>0.0 && epsilon > 0.0); double m; // midpoint for (int i=0; i0) b=m; else a=m; } return m; } Primes bool isPrime (int num) { int divisor = 2; if (num<=1) return false; else { while (num/divisor >= divisor) { if (num%divisor)==0) return false; divisor++; } } return true; } Syntax Variables Type Size Range unsigned char 1 byte [0, +255] = [0, +2 -1] 8-1 8-1 char 1 byte [-128, +127] = [-2 , +2 -1] unsigned short int 2 bytes [0, +65535] = [0, +2 -1] short int 2 bytes [-32768, +32767] = [-216-,+216--1] 32 unsigned int 4 bytes [0,+4294967295] = [0, +2 -32-1 32-1 int 4 bytes [-2147483648, +2147483647] = [-2 ,+2 -1] unsigned long int 4/8 long int 4/8 64- unsigned long long int 8 bytes 64-1+264-1 long long int 8 bytes [-2 , +2 -1] Type Storage Precision Exponent float 4 bytes ~ 7 digits ± 38 double* 8 bytes ~ 16 digits ± 308 long double 12 bytes ~ 34 digits ± 4931 Precedence Precedence Operator Associativity 1 Post-inc/dec (++, --) Left 2 Pre-inc/dec (++,--_ Right Unary +, unary - Right 3 * / % Left 4 + - Left 5 = *= /= %= += -= Right Scientific Notation double x = -2.6302e30; printf(“%d\n”, sizeof(double)); // 8 floating point printf(“%.2e\n”, x) // -2.63e+30 scientific notation with 2 decimal points printf(“%g\n”, x); // -2.6302e+30 shortest of %f or %e Arrays  Initialization o int b[5] = {1, 2, 3};  b[0] = 1
More Less

Related notes for CS 137

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit