630 likes | 788 Views
ecs30 Summer 2014: Programming and Problem Solving # 03: Chapters 3-7. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Review. Programs: kms and hello Program, Programming Languag , and Compiler
E N D
ecs30 Summer 2014:Programming and Problem Solving#03: 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
Review • Programs: kms and hello • Program, Programming Languag, and Compiler • X, &X, *X in C • Binary representation • Arithmetic Expressions • Mix-types Assignments • Function Arguments and Return Type/Value ecs30 Winter 2012 Lecture #01
Use “Casting” int m, n; double p, x, y; m = 3; n = 2; p = 2.0; x = m / p; y = (double) m / (double) n; printf(“x = %lf, y = %lf\n”, x,y); ecs30 winter 2012 Lecture #04
Evaluation Expressions(section 2.5 and 4.2) • Parenthesis rule • Operator precedence rule • function calls, unary {+,-,&,*,!}, binary {*,/,%}, binary {+,-}, logic {<,<=,>=,>},… assignment {=} • Associativity rule • Unary: right to left (e.g.,(-*x) (*-x)) • Binary: left to right (e.g., (x / y * z)) ecs30 winter 2012 Lecture #04
Abstraction ecs30 winter 2012 Lecture #04
Call by value Call by reference Call by name Figure 3-4: #define PI 3.1415926 #define Area 3.14 * R_big * R_big // gcc -E /* get the radius of bigger circle R_big */ /* get the radius of bigger circle R_inner*/ A = pi * r * r; A = 3.1415926 * r * r; ecs30 winter 2012 Lecture #04
Reusability/Sharing, Readability/Debugging, Modularity Abstraction Single return value for now; We will address other ways later (in Chapter 6). Function vs. Macro ecs30 WInter 2012 Lecture #05
trouble.c (hw#2)(try to control the input) #include <stdio.h> int main(void) { int x; printf("%x\n", &x); printf("%d\n", (int) &x); scanf("%d", &x); printf("[04] %d\n", (int) *((int *) x)); } 32 bits versus 64 bits sizeof uname -m What would be the valid input values for x such that the program won’t crash? ecs30 winter 2012 Lecture #04
Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 winter 2012 Lecture #04
double Find_area(doubler) { double area; area = r * r * PI; return area; } ecs30 winter 2012 Lecture #04
Inputs and Outputs (parameters and return value) ecs30 winter 2012 Lecture #04
Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 winter 2012 Lecture #04
Problem Solving ecs30 Winter 2012 Lecture #01
Problem Solving ecs30 Winter 2012 Lecture #01
Problem Solving Thinking Digitally!!! Variables Rules Inputs and Outputs ecs30 Winter 2012 Lecture #01
Variables: 16 tiles T00, T01, T02, T03 T10, T11, T12, T13 T20, T21, T22, T23 T30, T31, T32, T33 ecs30 Winter 2012 Lecture #01
Rules The rule of Shifting Up, Down, Left, Right Shift until no more empty tiles and no collapse The rule of Collapse If a tile hits another tile with equal value during a shift action The rule of “Game Over” ecs30 Winter 2012 Lecture #01
How about Inputs? ecs30 Winter 2012 Lecture #01
How about Inputs? New Tile Generation User Input Action ecs30 Winter 2012 Lecture #01
Problem Solving Thinking Digitally!!! Variables Rules Inputs and Outputs ecs30 Winter 2012 Lecture #01
void printRightShift(int x0, int x1, int x2, int x3); 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
void printRightShift(int x0, int x1, int x2, int x3) { … printf(…); return; } 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
intCompareShiftCollapse(int x, int y); ecs30 Winter 2012 Lecture #01
void printRightShift(int x0, int x1, int x2, int x3) { … CompareShiftCollapse(x0, x1); CompareShiftCollapse(x1, x2); CompareShiftCollapse(x2, x3); printf(…); return; } ecs30 Winter 2012 Lecture #01
4 4 4 4 8 8 4 ecs30 Winter 2012 Lecture #01
Problem Solving 210 29 28 27 23 24 25 26 22 24 22 21 ecs30 Winter 2012 Lecture #01 ecs30 Winter 2012 Lecture #01 35
#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
decision F T if (condition) { <statement> <statement> (optional) } else { <statement> <statement> (optional) } <other statements> ecs30 WInter 2012 Lecture #07
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 x, y) { double temp; if (x > y) { temp = y; y = x; x = 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
An interesting difference • Input/output for functions • A design concept in problem solving • What should be input to a function/module? • Call by Value/Referene/Name • Parameters and Return values for functions • An implementation concept in C (or a programming language feature of C) • We can use this feature to implement different ways of inputs and outputs! • C is call by value!(but…) Advanced ecs30 WInter 2012 Lecture #05
Execution Sequence/Control Flow 3 1 2 ecs30 WInter 2012 Lecture #05
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 ecs30 WInter 2012 Lecture #05