330 likes | 480 Views
ecs30 Summer 2014: Programming and Problem Solving # 04: Chapters 3-7. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Abstraction. Problem Solving. Thinking Digitally!!!. Variables Rules Inputs and Outputs.
E N D
ecs30 Summer 2014:Programming and Problem Solving#04: Chapters 3-7 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 #01
Abstraction ecs30 winter 2012 Lecture #04
Problem Solving Thinking Digitally!!! Variables Rules Inputs and Outputs ecs30 Winter 2012 Lecture #01
void printRightShift(int x0, int x1, int x2, int x3); 4 8 4 8 ecs30 Winter 2012 Lecture #01
void printRightShift(int x0, int x1, int x2, int x3); 8 8 16 ecs30 Winter 2012 Lecture #01
4 8 ecs30 Winter 2012 Lecture #01
4 8 ecs30 Winter 2012 Lecture #01
4 8 ecs30 Winter 2012 Lecture #01
4 8 ecs30 Winter 2012 Lecture #01
4 8 ecs30 Winter 2012 Lecture #01
X n ecs30 WInter 2012 Lecture #05
X n >>> x = x * 2; ecs30 WInter 2012 Lecture #05
How about num_1 and num_2? num_1 X num_2 n Caller Callee When you make a function call, do they “SHARE” the memory boxes? In other words, 12 or 24 bytes allocated for these four variables? ecs30 WInter 2012 Lecture #05
No, they don’t share. Calling means “copying the values!” ecs30 WInter 2012 Lecture #05
5.0 x = x * 2; Caller No, they don’t share. Calling means “copying the values!” Callee ecs30 WInter 2012 Lecture #05
num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 Lecture #11
num_1 X num_2 n Call by Reference &num_1 &x &num_2 &n num_1 num_2 ecs30 Winter 2012 Lecture #11
Call by References &num_1 &x &num_2 &n num_1 scale(&num_1, &num_2); double scale(double *xp, int *np) {… (*xp) = (*xp) * (*np); } num_2 ecs30 Winter 2012 Lecture #11
Problem Solving 210 29 28 27 23 24 25 26 22 24 22 21 ecs30 Winter 2012 Lecture #01 ecs30 Winter 2012 Lecture #01 19
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 #08
#include <stdio.h> #include <stdlib.h> int main(void) { intx = 0; fprintf(stderr, "%d\n", (int) (&x)); printf("enterx\n"); scanf("%d", &x); printf("[04] %d\n", (int) (*((int *) x))); return 0; } ecs30 WInter 2012 Lecture #08
#include <stdio.h> #include <stdlib.h> int main(void) { intx = 0; fprintf(stderr, "%d\n", (int) (&x)); printf("enterx\n"); scanf("%d", &x); printf("[04] %d\n", (int) (*((int *) x))); return 0; } Xp == &x *xp == x ecs30 WInter 2012 Lecture #08
0x00000222 ???? (X == 0x00000222) 0x00000222 0xbfcd1340 ecs30 WInter 2012 Lecture #08
0x00000222 ???? (X == 0xbfcd1340) 0xbfcd1340 0xbfcd1340 ecs30 WInter 2012 Lecture #08
0x00000222 ???? (X == 0xbfcd1340) 0xbfcd0000 0xbfcd1340 ecs30 WInter 2012 Lecture #08
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
decision F T if (condition) { <statement> <statement> (optional) } else { <statement> <statement> (optional) } <other statements> ecs30 WInter 2012 Lecture #07
#include <stdio.h> int main(void) { int i,j; i = 2; j = 0; printf("i = %d, j = %d\n", i,j); j =i++;//(1) j = i;(2) i = i+1; printf("i = %d, j = %d\n", i,j); i = 2; j = 0; printf("i = %d, j = %d\n", i,j); j =++i;//(1) i = i+1;(2) j =i; printf("i = %d, j = %d\n", i,j); } ecs30 WInter 2012 Lecture #09~10 (Roozbeh)
for(i=0; i<7; i++) printf("i = [%3d]\n", i); for(i=0; i<7; ++i) printf("i = [%3d]\n", i); for(i=0; i++< 7;) printf("i = [%3d]\n", i); for(i=0; (i++)<7;) printf("i = [%3d]\n", i); // (1) (i<7)(2) i=i+1; for(i=0; ++i< 7;) printf("i = [%3d]\n", i); for(i=0; (++i)<7;) printf("i = [%3d]\n", i); // (1) i=i+1; (2) (i<7) Print 7 times versus 6 times! ecs30 WInter 2012 Lecture #09~10 (Roozbeh)
for(i=0; i<7; i++) printf("i = [%3d]\n", i); for(i=0; i<7; ++i) printf("i = [%3d]\n", i); for(i=0; i++< 7;) printf("i = [%3d]\n", i); for(i=0; (i++)<7;) printf("i = [%3d]\n", i); // (1) (i<7)(2) i=i+1; for(i=0; ++i< 7;) printf("i = [%3d]\n", i); for(i=0; (++i)<7;) printf("i = [%3d]\n", i); // (1) i=i+1; (2) (i<7) ecs30 WInter 2012 Lecture #09~10 (Roozbeh)
for (i = n; i > 1; --i) product *= i; for (i = 2; i <= n; ++i) product *= i; ecs30 WInter 2012 Lecture #09~10 (Roozbeh)