Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
switch (expression) {
case const-expr: statements
case const-expr: statements
…
default: statements
}
/* Calculate Pi according to various methods */ #include <stdio.h> #include <math.h> int main() { <scan option> switch(opt) { case 1: <scan num> pi = pi1(num); break; case 2: <scan epsilon> pi = pi2(epsilon); break; case 3: <scan epsilon> pi = pi3(epsilon); break; defualt: <issue error message> break; } <print pi> return 0; } |
int is_alpha(char ch) { return (('a'<=ch && ch<='z')||('A'<=ch && ch<='Z')); } |
int is_digit(char ch) { return ('0' <= ch && ch <= '9'); } |
/* Calculate e according to (1 + 1/n)^n */ #include <stdio.h> double calc_e(int alst); int main() { int last; printf("Enter number of iterations: "); scanf("%d", &last); printf("%lf\n", calc_e(last)); return 0; } double calc_e(int last) { int i; double e = 1, mul = 1 + 1 / (double)last; for (i = 1; i <= last; i++) e *= mul; return e; } |
/* Calculate the root of a number */ #include <stdio.h> double my_sqrt(double num) { double root, root_n = 1.0; do { root = root_n; root_n = (root + num / root) / 2; } while(root_n != root); return root; } int main() { double num; scanf("%lf", &num); printf("%lf\n", my_sqrt(num)); return 0; } |
#include <stdio.h> int invoke() { static int counter = 0; return ++counter; } int main() { int i; for (i = 0; i < 10; i++) (void) invoke(); printf("%d\n", invoke()); return 0; } |