Adder.c 1/1: Lectures/weeks/1/src
Adder.c 1/1: Lectures/weeks/1/src
1/1
lectures/weeks/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:
/****************************************************************************
* adder.c
*
* Computer Science 50
* David J. Malan
*
* Adds two numbers.
*
* Demonstrates use of CS 50s library.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
int x, y;
/* ask user for input */
printf("Give me an integer: ");
x = GetInt();
printf("Give me another integer: ");
y = GetInt();
/* do the math */
printf("The sum of %d and %d is %d!\n", x, y, x + y);
}
conditions1.c
1/1
lectures/weeks/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:
/****************************************************************************
* 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 <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
int n;
/* ask user for an integer */
printf("Id like an integer please: ");
n = GetInt();
/* analyze users 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/weeks/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:
/****************************************************************************
* 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 <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
int n;
/* ask user for an integer */
printf("Id like an integer please: ");
n = GetInt();
/* analyze users input (somewhat inaccurately) */
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/weeks/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:
/****************************************************************************
* f2c.c
*
* Computer Science 50
* David J. Malan
*
* Converts Fahrenheit to Celsius.
*
* Demonstrates casting from int to char.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
float f, c;
/* ask user user for temperature in Fahrenheit */
printf("Temperature in F: ");
f = GetFloat();
/* convert F to C */
c = 5 / 9. * (f - 32);
/* display result */
printf("%.1f F = %.1f C\n", f, c);
}
hello1.c
1/1
lectures/weeks/1/src/
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
/****************************************************************************
* hello1.c
*
* Computer Science 50
* David J. Malan
*
* Says hello to the world.
*
* Demonstrates use of printf.
***************************************************************************/
#include <stdio.h>
int
main(int argc, char * argv[])
{
printf("hello, world\n");
}
hello2.c
1/1
lectures/weeks/1/src/
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
/****************************************************************************
* hello2.c
*
* Computer Science 50
* David J. Malan
*
* Says hello to just David.
*
* Demonstrates use of CS 50s library.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
string name = "David";
printf("hello, %s\n", name);
}
hello3.c
1/1
lectures/weeks/1/src/
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
/****************************************************************************
* hello3.c
*
* Computer Science 50
* David J. Malan
*
* Says hello to whomever.
*
* Demonstrates use of CS 50s library and standard input.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
string name;
printf("State your name: ");
name = GetString();
printf("hello, %s\n", name);
}
math1.c
1/1
lectures/weeks/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 <stdio.h>
int
main(int argc, char * argv[])
{
int x = 1;
int y = 2;
int z = x + y;
}
math2.c
1/1
lectures/weeks/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 <stdio.h>
int
main(int argc, char * argv[])
{
int x = 1;
int y = 2;
int z = x + y;
printf("%d", z);
}
math3.c
1/1
lectures/weeks/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 floating-point total.
*
* Demonstrates use of a format string with specified precision.
***************************************************************************/
#include <stdio.h>
int
main(int argc, char * argv[])
{
float answer = 17 / 13.;
printf("%.2f\n", answer);
}
nonswitch.c
1/1
lectures/weeks/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:
/****************************************************************************
* nonswitch.c
*
* Computer Science 50
* David J. Malan
*
* Assesses the size of users input.
*
* Demonstrates use of Boolean ANDing.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
int n;
/* ask user for an integer */
printf("Give me an integer between 1 and 10: ");
n = GetInt();
/* judge users
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");
positive.c
1/1
lectures/weeks/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:
/****************************************************************************
* positives.c
*
* Computer Science 50
* David J. Malan
*
* Demands that user provide a positive number.
*
* Demonstrates use of do-while.
***************************************************************************/
#include <stdio.h>
int
main(int argc, char * argv[])
{
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);
}
progress1.c
1/1
lectures/weeks/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:
/****************************************************************************
* progress1.c
*
* Computer Science 50
* David J. Malan
*
* Simulates a progress bar.
*
* Demonstrates sleep.
***************************************************************************/
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char * argv[])
{
int i;
/* simulate progress from 0% to 100% */
for (i = 0; i <= 100; i++)
{
printf("Percent complete: %d%%\n", i);
sleep(1);
}
printf("\n");
}
progress2.c
1/1
lectures/weeks/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:
/****************************************************************************
* progress2.c
*
* Computer Science 50
* David J. Malan
*
* Simulates a better progress bar.
*
* Demonstrates \r, fflush, and sleep.
***************************************************************************/
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char * argv[])
{
int i;
/* simulate progress from 0% to 100% */
for (i = 0; i <= 100; i++)
{
printf("\rPercent complete: %d%%", i);
fflush(stdout);
sleep(1);
}
printf("\n");
}
progress3.c
1/1
lectures/weeks/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 <stdio.h>
#include <unistd.h>
int
main(int argc, char * argv[])
{
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/weeks/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 Cs data types.
*
* Demonstrates use of sizeof.
***************************************************************************/
#include <stdio.h>
int
main(int argc, char * argv[])
{
/* local 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/weeks/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:
/****************************************************************************
* switch1.c
*
* Computer Science 50
* David J. Malan
*
* Assesses the size of users input.
*
* Demonstrates use of a switch.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
int n;
/* ask user for an integer */
printf("Give me an integer between 1 and 10: ");
n = GetInt();
/* judge users 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/weeks/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:
54:
55:
/****************************************************************************
* switch2.c
*
* Computer Science 50
* David J. Malan
*
* Assesses a users grade.
*
* Demonstrates use of a switch.
***************************************************************************/
#include <cs50.h>
#include <stdio.h>
int
main(int argc, char * argv[])
{
char c;
/* ask user for a char */
printf("Pick a letter grade: ");
c = GetChar();
/* judge users 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");
}
}