CountStars

Week 1, CountStars print stars Step 3: Repetitions Stars Example #include <stdio.h> int printTriangle(int size) { //start with starCount being 0 int starCount = 0; //count from 0 (inclusive) to size (exclusive), for each number i that you count for (int i=0; i< size; i++) { //count from 0 (inclusive) to i (inclusive), for each number j that you count for (int j=0; j<=i; j++) { //print a "*" printf("*"); //increment starCount starCount++; } //when you finish counting on j, //print a newline ("\\n") printf("\\n"); } //when you finish counting on i, //your answer is starCount return starCount; } int main(void) { int numStars; printf("Here is a triangle with height 4\n"); numStars = printTriangle(4); printf("That triangle had %d total stars\n", numStars); //now print "Here is a triangle with height 7\n" //then call printTriangle, passing in 7, and assign the result to numStars //finally, print "That triangle had %d total stars\n", such that the %d //prints the value of numStars return 0; } Tips: ...

2023-11-21 · 2 min · Atom.X

Writing, Running, and Fixing Code in C

Original Course link: Writing, Running, and Fixing Code in C Assignment GitHub - mvirgo/c-programming: Code worked on for Duke’s Intro to Programming in C course UNIX basic cat // output file to terminal emacs // open file in editor Emacs (be attention for the command, use lowercase, not uppercase) close a terminal tab, ctrl + shift + w close the entire terminal including all tabs, ctrl + shift + q githttp://git-scm.com/book/en/v2/ ...

2023-11-21 · 2 min · Atom.X

Overflow

Week 3 How to calculate binary, decimal, hexadecimal 3 parts of fields compound a float number. the most significant bit (MSB) 最高有效位 exponent 指数 mantissa 尾数 IEEE floating point Standard unsigned integer 无符号整数 that can only hold zero and positive numbers. For an n-bit integer, the range of values it can represent is[ 0 , 2^n ]. signed integer, For an n-bit integer, the range of values it can represent is[ -2^(n-1) , 2^(n-1) - 1]. ...

2023-11-18 · 5 min · Atom.X

Reading code

Week 2 Quiz Execute the following code by hand: int anotherFunction(int a, int b) { int answer = 2; int x = 0; printf("In anotherFunction(%d,%d)\n",a,b); while (b > a) { printf("a is %d, b is %d\n", a, b); answer = answer + (b - a); b -= x; a += x / 2; x++; } return answer; } int someFunction(int x, int y) { int a = x + y; if (x < y) { for (int i = 0; i < x; i++) { printf("In the loop with i = %d, a = %d\n", i, a); a = a + x; } } else { y = anotherFunction(y,a+1); } return a * (y-10); } int main(void) { int x = 2; int b = someFunction(3,x); printf("b = %d\n", b); printf("x = %d\n", x); return 0; } Execute main main //setep 1, execute main box, jump to calculate functions in b box. // get result from step 3 in y box, print first 4 lines In anotherFunction(2,6) a is 2, b is 6 a is 2, b is 6 a is 2, b is 5 //step 5, come back to main box, input last step value b=15 //step 6, print last two lines. b=15; x=2; b = someFunction(3,2); ...

2023-11-15 · 2 min · Atom.X

Programming Fundamentals

Original Course link: Programming Fundamentals Week 2 lvalue, rvalue eg. int x = 3; lvalue is x, rvalue is 3 erroneous code for loop = syntactic sugar, it allows you to write more compact code for counting, a common programming idiom(behavior, custom). What does f(5) evaluate to? int f (int n) { int ans = 0; for (int i = 1; i < n; i++) { if (i < n/2) { ans -= i; } else { ans += i; } } return ans; } Hint: dividing integers results in an integer answer. eg. 9/4 = 2, 5/2=2 (四舍五入rounding 3 is wrong) ...

2023-11-12 · 6 min · Atom.X