iteration.
1 // Draws a pyramid using iteration
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 void draw(int h);
7
8 int main(void)
9 {
10 // Get height of pyramid
11 int height = get_int("Height: ");
12
13 // Draw pyramid
14 draw(height);
15 }
16
17 void draw(int h)
18 {
19 // Draw pyramid of height h
20 for (int i = 1; i <= h; i++)
21 {
22 for (int j = 1; j <= i; j++)
23 {
24 printf("#");
25 }
26 printf("\n");
27 }
28 }
names0.c
1 // Implements linear search for names
2
3 #include <cs50.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 int main(void)
8 {
9 // An array of names
10 string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
11
12 // Search for EMMA
13 for (int i = 0; i < 4; i++)
14 {
15 if (strcmp(names[i], "EMMA") == 0)
16 {
17 printf("Found\n");
18 return 0;
19 }
20 }
21 printf("Not found\n");
22 return 1;
23 }
names1.c
1 // Implements linear search for names using !
2
3 #include <cs50.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 int main(void)
8 {
9 // An array of names
10 string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
11
12 // Search for EMMA
13 for (int i = 0; i < 4; i++)
14 {
15 if (!strcmp(names[i], "EMMA"))
16 {
17 printf("Found\n");
18 return 0;
19 }
20 }
21 printf("Not found\n");
22 return 1;
23 }
numbers.c
1 // Implements linear search for numbers
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // An array of numbers
9 int numbers[] = {4, 8, 15, 16, 23, 42};
10
11 // Search for 50
12 for (int i = 0; i < 6; i++)
13 {
14 if (numbers[i] == 50)
15 {
16 printf("Found\n");
17 return 0;
18 }
19 }
20 printf("Not found\n");
21 return 1;
22 }
phonebook0.c
1 // Implements a phone book without structs
2
3 #include <cs50.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 int main(void)
8 {
9 string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
10 string numbers[] = {"617-555-0100", "617-555-0101", "617-555-0102", "617-555-0103"};
11
12 for (int i = 0; i < 4; i++)
13 {
14 if (!strcmp(names[i], "EMMA"))
15 {
16 printf("Found %s\n", numbers[i]);
17 return 0;
18 }
19 }
20 printf("Not found\n");
21 return 1;
22 }
phonebook1.c
1 // Implements a phone book with structs
2
3 #include <cs50.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 typedef struct
8 {
9 string name;
10 string number;
11 }
12 person;
13
14 int main(void)
15 {
16 person people[4];
17
18 people[0].name = "EMMA";
19 people[0].number = "617-555-0100";
20
21 people[1].name = "RODRIGO";
22 people[1].number = "617-555-0101";
23
24 people[2].name = "BRIAN";
25 people[2].number = "617-555-0102";
26
27 people[3].name = "DAVID";
28 people[3].number = "617-555-0103";
29
30 // Search for EMMA
31 for (int i = 0; i < 4; i++)
32 {
33 if (strcmp(people[i].name, "EMMA") == 0)
34 {
35 printf("Found %s\n", people[i].number);
36 return 0;
37 }
38 }
39 printf("Not found\n");
40 return 1;
41 }
recursion.c
1 // Draws a pyramid using recursion
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 void draw(int h);
7
8 int main(void)
9 {
10 // Get height of pyramid
11 int height = get_int("Height: ");
12
13 // Draw pyramid
14 draw(height);
15 }
16
17 void draw(int h)
18 {
19 // If nothing to draw
20 if (h == 0)
21 {
22 return;
23 }
24
25 // Draw pyramid of height h - 1
26 draw(h - 1);
27
28 // Draw one more row of width h
29 for (int i = 0; i < h; i++)
30 {
31 printf("#");
32 }
33 printf("\n");
34 }