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; } }