410 likes | 575 Views
ecs30 Winter 2012: Programming and Problem Solving # 06: Chapters 2~ 5. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Reusability/Sharing, Readability/Debugging, Modularity. Abstraction. Function vs. Macro.
E N D
ecs30 Winter 2012:Programming and Problem Solving#06: Chapters 2~5 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 WInter 2012 Lecture #06
Reusability/Sharing, Readability/Debugging, Modularity Abstraction Function vs. Macro ecs30 WInter 2012 Lecture #06
Function • intmain (void) { … } • float myfunc (intx) { …; return f;} <return type> <function name> (input parameters) {<body>} • { <declarations> <statements> } ecs30 WInter 2012 Lecture #06
Inputs and Outputs (parameters and return value) They are actually different!! ecs30 WInter 2012 Lecture #06
Call by Value, Reference, Name Pointer/Address of Memory/Variable Pointer to Function ~ Control of “where is the function code”! ecs30 WInter 2012 Lecture #06
double r_in, r_out, the_area; get_input_paramenters(&r_in, &r_out); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(double *in_ptr, *out_ptr) { scanf(“%lf”, in_ptr); scanf(“%lf”, out_ptr); return 0; } ecs30 WInter 2012 Lecture #06
No, they don’t share. Calling means “copying the values!” ecs30 WInter 2012 Lecture #06
5.0 x = x * 2; Caller No, they don’t share. Calling means “copying the values!” Callee ecs30 WInter 2012 Lecture #06
num_1 X num_2 n Functions in C are“call by values”for input parameters! I.e., we copy the values fromcallertocalleewhencalling a function! (and, NOT copying them back when returns!) ecs30 WInter 2012 Lecture #06
intx; (int *) xp = &x; x = 222; x, &x, *xp; ecs30 WInter 2012 Lecture #06
intx; int * xp = &x; x = 222; x, &x, *xp; /*???*/ ecs30 WInter 2012 Lecture #06
int* xp; <type> * <variable name>; I declare a variable xpsuch that xpis an address containing a integer-type object. address xp &xp xp T(int) = 222 x (*xp) == 222; ecs30 WInter 2012 Lecture #06
int* xp; <type> * <variable name>; I declare a variable xpsuch that xpis an address containing a integer-type object. 34ef5f12 xp &xp xp or &x == 0x34ef5f12 T(int) = 222 x x == 222 *xp == 222 ecs30 WInter 2012 Lecture #06
T = (int *) * T* x; <type> * <variable name>; I declare a variable x such that x is an address containing a T-type object. xp = &x; 34ef5f12 xp &xp &x == 0x34ef5f12 T(int) = 222 x x == 222 *xp == 222 ecs30 WInter 2012 Lecture #06
Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 WInter 2012 Lecture #06
#include <stdio.h> #include <stdlib.h> #define PI 3.1415926 intmain(void) { double inner, outer, area; /* inputs of radius */ printf("pleaseinout the inner:"); scanf("%lf", &inner); printf("pleaseinout the outer:"); scanf("%lf", &outer); Compare_and_swap(&inner, &outer); /* calculate the area */ area = outer * outer * PI - inner * inner * PI; /* print the result */ printf("area = %f\n", area); return 0; } ecs30 WInter 2012 Lecture #06
What is the “condition”? ecs30 WInter 2012 Lecture #06
OR, AND, XOR X Y (X || Y) (X && Y) (X Y) T T T T F T F T F T F T T F T F F F F F ecs30 WInter 2012 Lecture #06
Brainstorming… What is this program segment doing? Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06
void Compare_and_swap(double *xp, *yp) { double temp; if (*xp > *yp) { temp = *yp; *yp = *xp; *xp = temp; } } ecs30 WInter 2012 Lecture #06
if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06
printf and fprintf printf("Name: %s\n", yourname); fprintf(stdout, "Name: %s\n", yourname); %./a.out > out.xyz fprintf(stderr, "Name: %s\n", yourname); ecs30 WInter 2012 Lecture #06
“conditional” flow control ecs30 WInter 2012 Lecture #06
Conditional Swapping Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06
if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06
T/F if (x == 0) {// TRUE} else {//FALSE} • What should be “x”? (In what ranges of x, the condition will be true?) ecs30 WInter 2012 Lecture #06
T/F if (x == 1) {// TRUE} else {//FALSE} • What should be “x”? (In what ranges of x, the condition will be true?) • ONLY (x == 1) (0 for 32 bits) 00000000 00000000 00000000 00000001 ecs30 WInter 2012 Lecture #06
T/F if (x == 1) {// TRUE} else {//FALSE} • What should be “x”? if (x) {// TRUE} else {//FALSE} 00000010 00010000 00000000 00000000 !(00000000 00000000 00000000 00000000) if (x = 0) {// TRUE} else {//FALSE} (00000000 00000000 00000000 00000000) Always false! ecs30 WInter 2012 Lecture #06
Logical Negation if (!x) {// TRUE} else {//FALSE} • What should be the value of x? if (x == 0) {// TRUE} else {//FALSE} ecs30 WInter 2012 Lecture #06
Logical/Bitwise • Logical: (if statements) • X = {false or true} {zero or otherwise} • Bitwise: we operate on every bit! 00000000 00000000 00000000 00000001 & 10101010 00000000 00000000 10101011 00000000 00000000 00000000 00000001 ecs30 WInter 2012 Lecture #06
Special Symbols(Appendix C) Logical • ||, &&, >, <, >=, <=, ==, != • ! Bitwise • |, &, ^, <<, >>, ~ ecs30 WInter 2012 Lecture #06
if and ? printf(“%d\n”, (x>y) ? x : y); if (x > y) { printf(“%d\n”, x); } else { printf(“%d\n”, y); } ecs30 WInter 2012 Lecture #06
Brainstorming… ecs30 WInter 2012 Lecture #06
Brainstorming… if ((x>= min) && (x <= max)) { // TRUE } ecs30 WInter 2012 Lecture #06
Brainstorming… ecs30 WInter 2012 Lecture #06
Brainstorming… if ((x < z) || (x > y)) { // TRUE } ecs30 WInter 2012 Lecture #06
char class; switch(class) { case ‘B’: case ‘b’: printf(“Battleship\n”); break; case ‘C’: case ‘C’: printf(“Cruiser\n”); break; default: printf(“Unknown class %c\n”, class); } ecs30 WInter 2012 Lecture #06
int class; switch(class) { case 0: case 1: printf(“Battleship\n”); break; case 2: printf(“Cruiser\n”); break; default: printf(“Unknown class %d\n”, class); } ecs30 WInter 2012 Lecture #06