adder.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:

/**************************************************************************** * adder.c * * Computer Science 50 * David J. Malan * * Adds two numbers. * * Demonstrates use of CS50’s library. ***************************************************************************/ #include #include int main(void) { // ask user for input printf("Give me an integer: "); int x = GetInt(); printf("Give me another integer: "); int y = GetInt(); // do the math printf("The sum of %d and %d is %d!\n", x, y, x + y); }

conditions1.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:

/**************************************************************************** * conditions1.c * * Computer Science 50 * David J. Malan * * Tells user if his or her input is positive or negative (somewhat * innacurately). * * Demonstrates use of if-else construct. ***************************************************************************/ #include #include int main(void) { // ask user for an integer printf("I’d like an integer please: "); int n = GetInt(); // analyze user’s input (somewhat inaccurately) if (n > 0) printf("You picked a positive number!\n"); else printf("You picked a negative number!\n"); }

conditions2.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:

/**************************************************************************** * conditions2.c * * Computer Science 50 * David J. Malan * * Tells user if his or her input is positive or negative. * * Demonstrates use of if-else if-else construct. ***************************************************************************/ #include #include int main(void) { // ask user for an integer printf("I’d like an integer please: "); int n = GetInt(); // analyze user’s input if (n > 0) printf("You picked a positive number!\n"); else if (n == 0) printf("You picked zero!\n"); else printf("You picked a negative number!\n"); }

f2c.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:

/**************************************************************************** * f2c.c * * Computer Science 50 * David J. Malan * * Converts Fahrenheit to Celsius. * * Demonstrates arithmetic. ***************************************************************************/ #include #include int main(void) { // ask user user for temperature in Fahrenheit printf("Temperature in F: "); float f = GetFloat(); // convert F to C float c = 5 / 9.0 * (f - 32); // display result printf("%.1f F = %.1f C\n", f, c); }

hai1.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:

/**************************************************************************** * hai1.c * * Computer Science 50 * David J. Malan * * Says hello to the world. * * Demonstrates use of printf. ***************************************************************************/ #include int main(void) { printf("O hai, world!\n"); }

hai2.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:

/**************************************************************************** * hai2.c * * Computer Science 50 * David J. Malan * * Says hello to just David. * * Demonstrates use of CS50’s library. ***************************************************************************/ #include #include int main(void) { string name = "David"; printf("O hai, %s!\n", name); }

hai3.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:

/**************************************************************************** * hai3.c * * Computer Science 50 * David J. Malan * * Says hello to whomever. * * Demonstrates use of CS50’s library and standard input. ***************************************************************************/ #include #include int main(void) { printf("State your name: "); string name = GetString(); printf("O hai, %s!\n", name); }

holloway.c lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:

/* http://www.ioccc.org/years.html */ #include "stdio.h" #define e 3 #define g (e/e) #define h ((g+e)/2) #define f (e-g-h) #define j (e*e-g) #define k (j-h) #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a)) long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L }; int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 }; main(m1,s) char *s; { int a,b,c,d,o[k],n=(int)s; if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); } else switch(m1-=h){ case f: a=(b=(c=(d=g)<=e)for(b=g<
1/1

math1.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:

/**************************************************************************** * math1.c * * Computer Science 50 * David J. Malan * * Computes a total but does nothing with it. * * Demonstrates use of variables. ***************************************************************************/ #include int main(void) { int x = 1; int y = 2; int z = x + y; }

math2.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:

/**************************************************************************** * math2.c * * Computer Science 50 * David J. Malan * * Computes and prints an integral total. * * Demonstrates use of a format string. ***************************************************************************/ #include int main(void) { int x = 1; int y = 2; int z = x + y; printf("%d", z); }

math3.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:

/**************************************************************************** * math3.c * * Computer Science 50 * David J. Malan * * Computes and prints a floating-point total. * * Demonstrates loss of precision. ***************************************************************************/ #include int main(void) { float answer = 17 / 13; printf("%.2f\n", answer); }

math4.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:

/**************************************************************************** * math4.c * * Computer Science 50 * David J. Malan * * Computes and prints a floating-point total. * * Demonstrates use of floating-point math. ***************************************************************************/ #include int main(void) { float answer = 17 / 13.0; printf("%.2f\n", answer); }

math5.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:

/**************************************************************************** * math5.c * * Computer Science 50 * David J. Malan * * Computes and prints a floating-point total. * * Demonstrates use of casting. ***************************************************************************/ #include int main(void) { float answer = 17 / (float) 13; printf("%.2f\n", answer); }

nonswitch.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:

/**************************************************************************** * nonswitch.c * * Computer Science 50 * David J. Malan * * Assesses the size of user’s input. * * Demonstrates use of Boolean ANDing. ***************************************************************************/ #include #include int main(void) { // ask user for an integer printf("Give me an integer between 1 and 10: "); int n = GetInt(); // judge user’s if (n >= 1 && n printf("You else if (n >= 4 printf("You else if (n >= 7 printf("You else printf("You }

input <= 3) picked a small number.\n"); && n <= 6) picked a medium number.\n"); && n <= 10) picked a big number.\n"); picked an invalid number.\n");

positive1.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:

/**************************************************************************** * positive1.c * * Computer Science 50 * David J. Malan * * Demands that user provide a positive number. * * Demonstrates use of do-while. ***************************************************************************/ #include #include int main(void) { // loop until user provides a positive integer int n; do { printf("I demand that you give me a positive integer: "); n = GetInt(); } while (n < 1); printf("Thanks for the %d!\n", n); }

positive2.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:

/**************************************************************************** * positive2.c * * Computer Science 50 * David J. Malan * * Demands that user provide a positive number. * * Demonstrates use of bool. ***************************************************************************/ #include #include int main(void) { // loop until user provides a positive integer bool thankful = false; do { printf("I demand that you give me a positive integer: "); if (GetInt() > 0) thankful = true; } while (thankful == false); printf("Thanks for the positive integer!\n"); }

positive3.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:

/**************************************************************************** * positive3.c * * Computer Science 50 * David J. Malan * * Demands that user provide a positive number. * * Demonstrates use of !. ***************************************************************************/ #include #include int main(void) { // loop until user provides a positive integer bool thankful = false; do { printf("I demand that you give me a positive integer: "); if (GetInt() > 0) thankful = true; } while (!thankful); printf("Thanks for the positive integer!\n"); }

progress1.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:

/**************************************************************************** * progress1.c * * Computer Science 50 * David J. Malan * * Simulates a progress bar. * * Demonstrates sleep. ***************************************************************************/ #include #include int main(void) { // simulate progress from 0% to 100% for (int i = 0; i <= 100; i++) { printf("Percent complete: %d%%\n", i); sleep(1); } printf("\n"); }

progress2.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:

/**************************************************************************** * progress2.c * * Computer Science 50 * David J. Malan * * Simulates a better progress bar. * * Demonstrates \r, fflush, and sleep. ***************************************************************************/ #include #include int main(void) { // simulate progress from 0% to 100% for (int i = 0; i <= 100; i++) { printf("\rPercent complete: %d%%", i); fflush(stdout); sleep(1); } printf("\n"); }

progress3.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:

/**************************************************************************** * progress3.c * * Computer Science 50 * David J. Malan * * Simulates a better progress bar. * * Demonstrates a while loop. ***************************************************************************/ #include #include int main(void) { int i = 0; /* simulate progress from 0% to 100% */ while (i <= 100) { printf("\rPercent complete: %d%%", i); fflush(stdout); sleep(1); i++; } printf("\n"); }

sizeof.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:

/**************************************************************************** * sizeof.c * * Computer Science 50 * David J. Malan * * Reports the sizes of C’s data types. * * Demonstrates use of sizeof. ***************************************************************************/ #include int main(void) { // some sample variables char c; double d; float f; int i; // report the sizes of variables’ types printf("char: %d\n", sizeof(c)); printf("double: %d\n", sizeof(d)); printf("float: %d\n", sizeof(f)); printf("int: %d\n", sizeof(i)); }

switch1.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:

/**************************************************************************** * switch1.c * * Computer Science 50 * David J. Malan * * Assesses the size of user’s input. * * Demonstrates use of a switch. ***************************************************************************/ #include #include int main(void) { // ask user for an integer printf("Give me an integer between 1 and 10: "); int n = GetInt(); // judge user’s input switch (n) { case 1: case 2: case 3: printf("You picked a small number.\n"); break; case 4: case 5: case 6: printf("You picked a medium number.\n"); break; case case case case

7: 8: 9: 10: printf("You picked a big number.\n"); break;

default: printf("You picked an invalid number.\n"); } }

switch2.c

1/1

lectures/1/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53:

lectures/1/src/

/**************************************************************************** * switch2.c * * Computer Science 50 * David J. Malan * * Assesses a user’s grade. * * Demonstrates use of a switch. ***************************************************************************/ #include #include int main(void) { // ask user for a char printf("Pick a letter grade: "); char c = GetChar(); // judge user’s input switch (c) { case ’A’: case ’a’: printf("You picked an excellent grade.\n"); break; case ’B’: case ’b’: printf("You picked a good grade.\n"); break; case ’C’: case ’c’: printf("You picked a fair grade.\n"); break; case ’D’: case ’d’: printf("You picked a poor grade.\n"); break; case ’E’: case ’e’: printf("You picked a failing grade.\n"); break; default: printf("You picked an invalid grade.\n"); } }

thadgavin.c 1: /* http://www.ioccc.org/years.html */ 2: 3: int 4: X=320 ,Y=200, 5: n=0,m, x,y, j=1024; 6: double T=44.0 /7,P[ 7: 333333 ],C[5] ={ 0,3, 8: 0,0,8} ,p=1, B=11.0 9: /630, f=0,r = 3,g 10: =7,b =13,*q=P, D,*J; 11: unsigned char 12: U[66666],*v=U,*h,l[5555] 13: ,c=0,*e,*a,*z; 14: 15: #include 16: #define R1(t) t=(int)(t\ 17: *123456789 )%j; t/=j; 18: #define Rl(C,t)\ 19: n++[C] = t*n/12; 20: #define RI(C) B=-B; R1\ 21: (r)R1(g )R1(b )for(n\ 22: =0; n 29: #include 30: #include 31: #define Q(u,v) u##portb(0x3##v 32: #define W ; Q(out,C9),*h++/4) 33: void F(int i){ __dpmi_regs r 34: ; if(i){ for(; i>=0; i-=8)while( 35: ˜Q(in,DA) 36: )&8^i); for(m=0,z 37: =h+j; h pixels; } } 49: #else 50: #include "curses.h" 51: void F(i){ if(i){ for(y=0; 52: y
1/2

thadgavin.c

2/2

lectures/1/src/ 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:

for (x=-X/2,y=-Y/2;y=X/2?x=-X/2,y++:4) {*q++ = sqrt(x*x+y*y); *q++ = atan2(x,y); }for (;n1){p=2-p;RI(l+j)} else if (p<0){p=-p;RI(l)} while(a
; 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: }

}F(8); C[2]+=B; f+=T/360; C[3]+=f; if (f>T) {C[1] += (f-T)/8; if (f>T*2) C[0]=sin(f)+sin(f*2)/2; } }

adder.c 1/1 conditions1.c 1/1 - CS50 CDN

24: printf("char: %d\n", sizeof(c));. 25: printf("double: %d\n", sizeof(d));. 26: printf("float: %d\n", sizeof(f));. 27: printf("int: %d\n", sizeof(i));. 28: } switch1.c. 1/1.

54KB Sizes 2 Downloads 381 Views

Recommend Documents

52/cs50! - CS50 CDN
SSH. • Secure Shell. • Allows you to access another computer through command-‐line interface. • We use SSH to connect to the CS50 Cloud!

52/cs50! - CS50 CDN
A condi on may have two values: true or false. • May be expressed as a logical expression or a. 'bool' variable. • Can be thought of as a yes/no ques on, or a.

CS50 Walkthrough 1 - CS50 CDN
Free Candy. ▫ Time for Change. ▫ I Saw You ... Free Candy. ▫ Seriously, in the CS50 ... ask user for an integer printf("Give me an integer between 1 and 10: ");.

CS50 Walkthrough #3 - CS50 CDN
Go to middle if k < value at middle search for k between first and the one before the middle if k > value at middle search for k between one after the middle and last if k = value at middle return true. If you haven't found k after this loop, return

Merge Sort - CS50 CDN
Data stored in memory has both a value and a location. • Pointers contain the memory address of some piece of data. • * pointer contains address to a ...

pset4 - CS50 CDN
Oct 8, 2010 - Go ahead and execute the command below: hostname. Recall that cloud.cs50.net is actually a cluster of servers. That command tells you the name of the specific server in the cluster that you happen to be connected to at the moment. Take

Merge Sort - CS50 CDN
Data stored in memory has both a value and a location. • Pointers contain the memory address of some piece of data. • * pointer contains address to a ...

CS51 - CS50 CDN
We can still conceptualize & prototype using the right language abstractions. ▻ If we understand relationships between linguistic abstractions, we can realize ...

Untitled - CS50 CDN
http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Characteristics/Characteristics.html ... content="yes"> http://developer.apple.com/library/safari/documentation/appleapplications/reference/SafariHTMLRef/Article

Asymptotic Notation - CS50 CDN
Like searching through the phone book. • Identify ... as you go. If array[i + 1] < array[i], swap them! ... Grab the smallest and swap it with whatever is at the front of ...

Krzysztof Gajos - CS50 CDN
What you will learn in. CS 179. • Discover and understand people's latent needs. • Invent and construct prototypes. • Design for people different than yourself.

CS50 Walkthrough #3 - CS50 CDN
what type are these values? ▫ how do we initialize them? ▫ don't forget! ▫ swap tiles for even d ... Questions? Please email me feedback: [email protected].

cs50.c 1/5 cs50.c 2/5 - CS50 CDN
11: * Based on Eric Roberts' genlib.h and simpio.h. 12: *. 13: * The latest version of this file can be found at. 14: * http://www.cs50.net/pub/releases/cs50/cs50.h.

Quiz 0 - CS50 CDN
In the context of files, Linux uses \n to end lines, Mac OS uses \r, and Windows ... format string's expectation of a leading %f, and so neither f nor c get filled with a ...

Week 8 - CS50 CDN
PHP: PHP Hypertext Preprocessor. • When accessed, dynamically generates a webpage which it then outputs to browser. • PHP code enclosed in tag.

Untitled - CS50 CDN
void swap(int a, int b). { int tmp = a; a = b; b = tmp;. } Page 11. void swap(int *a, int *b). { int tmp = *a;. *a = *b;. *b = tmp;. } Page 12. main's parameters main's ...

Asymptotic Notation - CS50 CDN
break – tell the program to 'pause' at a certain point (either a function or a line number) step – 'step' to the next executed statement next – moves to the next ...

Krzysztof Gajos - CS50 CDN
What you will learn in. CS 179. • Discover and understand people's latent needs. • Invent and construct prototypes. • Design for people different than yourself.

Untitled - CS50 CDN
void swap(int a, int b). { int tmp = a; a = b; b = tmp;. } Page 11. void swap(int *a, int *b). { int tmp = *a;. *a = *b;. *b = tmp;. } Page 12. main's parameters.

Computer Science 124 - CS50 CDN
Computer Science 124 : Who Should Take It. • CS 124 is all about developing techniques for solving problems. • This is what CS is all about! – Take a problem.

Untitled - CS50 CDN
Mac OS 10.6. Windows 7. Mac OS 10.5. Windows Vista. Windows XP. Mac OS 10.4. Linux. 0. 50. 100. 150. 200. Page 19. Elective. Concentration. Unsure.

Untitled - CS50 CDN
50. 100. 150. 200. Page 19. Elective. Concentration. Unsure. Gen Ed. Core. 0. 50. 100. 150. 200. 250. 300. Page 20. arrays. Page 21. to be continued... Page 22.

Untitled - CS50 CDN
http://www.blogcdn.com/www.engadget.com/media/2008/05/iphone_line_1-1.jpg. Page 16. valgrind valgrind -‐v -‐-‐leak-‐check=full a.out. Invalid write of size 4.

CS121 Tease for CS50.pptx - CS50 CDN
Formal Systems and Computation. • Two ways to look at it. 1. Study of problems and computers with all their physicality abstracted away q0 q1 q2 q3 a a a a b b.