This commit is contained in:
2024-11-28 17:05:14 +01:00
parent b034aad83f
commit 19b5b201b0
205 changed files with 0 additions and 2340 deletions
-28
View File
@@ -1,28 +0,0 @@
// Draws a pyramid using iteration
#include <cs50.h>
#include <stdio.h>
void draw(int n);
int main(void)
{
// Get height of pyramid
int height = get_int("Height: ");
// Draw pyramid
draw(height);
}
void draw(int n)
{
// Draw pyramid of height n
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i + 1; j++)
{
printf("#");
}
printf("\n");
}
}
-25
View File
@@ -1,25 +0,0 @@
// Implements a phone book without structs
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// Arrays of strings
string names[] = {"Carter", "David"};
string numbers[] = {"+1-617-495-1000", "+1-949-468-2750"};
// Search for name
string name = get_string("Name: ");
for (int i = 0; i < 2; i++)
{
if (strcmp(names[i], name) == 0)
{
printf("Found %s\n", numbers[i]);
return 0;
}
}
printf("Not found\n");
return 1;
}
-36
View File
@@ -1,36 +0,0 @@
// Implements a phone book with structs
#include <cs50.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
string name;
string number;
}
person;
int main(void)
{
person people[2];
people[0].name = "Carter";
people[0].number = "+1-617-495-1000";
people[1].name = "David";
people[1].number = "+1-949-468-2750";
// Search for name
string name = get_string("Name: ");
for (int i = 0; i < 2; i++)
{
if (strcmp(people[i].name, name) == 0)
{
printf("Found %s\n", people[i].number);
return 0;
}
}
printf("Not found\n");
return 1;
}
-23
View File
@@ -1,23 +0,0 @@
// Draws a pyramid (incorrectly) using recursion,
// must be compiled with -Wno-infinite-recursion
#include <cs50.h>
#include <stdio.h>
void draw(int n);
int main(void)
{
draw(1);
}
void draw(int n)
{
for (int i = 0; i < n; i++)
{
printf("#");
}
printf("\n");
draw(n + 1);
}
-34
View File
@@ -1,34 +0,0 @@
// Draws a pyramid using recursion
#include <cs50.h>
#include <stdio.h>
void draw(int n);
int main(void)
{
// Get height of pyramid
int height = get_int("Height: ");
// Draw pyramid
draw(height);
}
void draw(int n)
{
// If nothing to draw
if (n <= 0)
{
return;
}
// Draw pyramid of height n - 1
draw(n - 1);
// Draw one more row of width n
for (int i = 0; i < n; i++)
{
printf("#");
}
printf("\n");
}
-23
View File
@@ -1,23 +0,0 @@
// Implements linear search for integers
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// An array of integers
int numbers[] = {20, 500, 10, 5, 100, 1, 50};
// Search for number
int n = get_int("Number: ");
for (int i = 0; i < 7; i++)
{
if (numbers[i] == n)
{
printf("Found\n");
return 0;
}
}
printf("Not found\n");
return 1;
}
-24
View File
@@ -1,24 +0,0 @@
// Implements linear search for strings
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// An array of strings
string strings[] = {"battleship", "boot", "cannon", "iron", "thimble", "top hat"};
// Search for string
string s = get_string("String: ");
for (int i = 0; i < 6; i++)
{
if (strcmp(strings[i], s) == 0)
{
printf("Found\n");
return 0;
}
}
printf("Not found\n");
return 1;
}
-15
View File
@@ -1,15 +0,0 @@
// Explicitly casts chars to ints
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("String: ");
for (int i = 0; i < strlen(s); i++)
{
int c = (int) s[i];
printf("%c %i\n", s[i], c);
}
}
-14
View File
@@ -1,14 +0,0 @@
// Implicitly casts chars to ints
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("String: ");
for (int i = 0; i < strlen(s); i++)
{
printf("%c %i\n", s[i], s[i]);
}
}
-11
View File
@@ -1,11 +0,0 @@
// Buggy example for printf
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 3; i++)
{
printf("#\n");
}
}
-24
View File
@@ -1,24 +0,0 @@
// Buggy example for debug50
#include <cs50.h>
#include <stdio.h>
int get_negative_int(void);
int main(void)
{
int i = get_negative_int();
printf("%i\n", i);
}
// Prompt user for positive integer
int get_negative_int(void)
{
int n;
do
{
n = get_int("Negative Integer: ");
}
while (n < 0);
return n;
}
-10
View File
@@ -1,10 +0,0 @@
// Uses get_string
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, %s\n", answer);
}
-10
View File
@@ -1,10 +0,0 @@
// Uses get_string with better-named variable
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
-16
View File
@@ -1,16 +0,0 @@
// Prints a command-line argument
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("hello, %s\n", argv[1]);
}
else
{
printf("hello, world\n");
}
}
-12
View File
@@ -1,12 +0,0 @@
// Prints command-line arguments
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
for (int i = 0; i < argc; i++)
{
printf("%s\n", argv[i]);
}
}
-6
View File
@@ -1,6 +0,0 @@
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
-8
View File
@@ -1,8 +0,0 @@
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
-12
View File
@@ -1,12 +0,0 @@
// Prints chars
#include <stdio.h>
int main(void)
{
char c1 = 'H';
char c2 = 'I';
char c3 = '!';
printf("%c%c%c\n", c1, c2, c3);
}
-12
View File
@@ -1,12 +0,0 @@
// Prints chars' ASCII codes
#include <stdio.h>
int main(void)
{
char c1 = 'H';
char c2 = 'I';
char c3 = '!';
printf("%i %i %i\n", c1, c2, c3);
}
-10
View File
@@ -1,10 +0,0 @@
// Prints string
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
printf("%s\n", s);
}
-10
View File
@@ -1,10 +0,0 @@
// Prints string's ASCII codes
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
printf("%i %i %i\n", s[0], s[1], s[2]);
}
-10
View File
@@ -1,10 +0,0 @@
// Prints string's ASCII codes, including NUL
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
printf("%i %i %i %i\n", s[0], s[1], s[2], s[3]);
}
-13
View File
@@ -1,13 +0,0 @@
// Multiple strings
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = "HI!";
string t = "BYE!";
printf("%s\n", s);
printf("%s\n", t);
}
-15
View File
@@ -1,15 +0,0 @@
// Array of strings
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string words[2];
words[0] = "HI!";
words[1] = "BYE!";
printf("%s\n", words[0]);
printf("%s\n", words[1]);
}
-14
View File
@@ -1,14 +0,0 @@
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string words[2];
words[0] = "HI!";
words[1] = "BYE!";
printf("%c%c%c\n", words[0][0], words[0][1], words[0][2]);
printf("%c%c%c%c\n", words[1][0], words[1][1], words[1][2], words[1][3]);
}
-18
View File
@@ -1,18 +0,0 @@
// Determines the length of a string
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt for user's name
string name = get_string("Name: ");
// Count number of characters up until '\0' (aka NUL)
int n = 0;
while (name[n] != '\0')
{
n++;
}
printf("%i\n", n);
}
-25
View File
@@ -1,25 +0,0 @@
// Determines the length of a string using a function
#include <cs50.h>
#include <stdio.h>
int string_length(string s);
int main(void)
{
// Prompt for user's name
string name = get_string("Name: ");
int length = string_length(name);
printf("%i\n", length);
}
int string_length(string s)
{
// Count number of characters up until '\0' (aka NUL)
int n = 0;
while (s[n] != '\0')
{
n++;
}
return n;
}
-13
View File
@@ -1,13 +0,0 @@
// Determines the length of a string using a function
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// Prompt for user's name
string name = get_string("Name: ");
int length = strlen(name);
printf("%i\n", length);
}
-14
View File
@@ -1,14 +0,0 @@
// Averages three (hardcoded) numbers
#include <stdio.h>
int main(void)
{
// Scores
int score1 = 72;
int score2 = 73;
int score3 = 33;
// Print average
printf("Average: %f\n", (score1 + score2 + score3) / 3.0);
}
-16
View File
@@ -1,16 +0,0 @@
// Averages three (hardcoded) numbers using an array
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Scores
int scores[3];
scores[0] = 72;
scores[1] = 73;
scores[2] = 33;
// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
-16
View File
@@ -1,16 +0,0 @@
// Averages three numbers using an array
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Get scores
int scores[3];
scores[0] = get_int("Score: ");
scores[1] = get_int("Score: ");
scores[2] = get_int("Score: ");
// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
-17
View File
@@ -1,17 +0,0 @@
// Averages three numbers using an array and a loop
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Get scores
int scores[3];
for (int i = 0; i < 3; i++)
{
scores[i] = get_int("Score: ");
}
// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
-35
View File
@@ -1,35 +0,0 @@
// Averages three numbers using an array, a constant, and a helper function
#include <cs50.h>
#include <stdio.h>
// Constant
const int N = 3;
// Prototype
float average(int length, int array[]);
int main(void)
{
// Get scores
int scores[N];
for (int i = 0; i < N; i++)
{
scores[i] = get_int("Score: ");
}
// Print average
printf("Average: %f\n", average(N, scores));
}
float average(int length, int array[])
{
// Calculate average
int sum = 0;
for (int i = 0; i < length; i++)
{
sum += array[i];
}
return sum / (float) length;
}
-15
View File
@@ -1,15 +0,0 @@
// Returns explicit value from main
#include <cs50.h>
#include <stdio.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Missing command-line argument\n");
return 1;
}
printf("hello, %s\n", argv[1]);
return 0;
}
-16
View File
@@ -1,16 +0,0 @@
// Prints string char by char, using strlen
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
for (int i = 0; i < strlen(s); i++)
{
printf("%c", s[i]);
}
printf("\n");
}
-16
View File
@@ -1,16 +0,0 @@
// Prints string char by char, using strlen, remembering string's length
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c", s[i]);
}
printf("\n");
}
-23
View File
@@ -1,23 +0,0 @@
// Uppercases a string
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
{
printf("%c", s[i] - 32);
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
}
-24
View File
@@ -1,24 +0,0 @@
// Uppercases string using ctype library (and an unnecessary condition)
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (islower(s[i]))
{
printf("%c", toupper(s[i]));
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
}
-17
View File
@@ -1,17 +0,0 @@
// Uppercases string using ctype library
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c", toupper(s[i]));
}
printf("\n");
}
-20
View File
@@ -1,20 +0,0 @@
// Logical operators
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user to agree
char c = get_char("Do you agree? ");
// Check whether agreed
if (c == 'Y' || c == 'y')
{
printf("Agreed.\n");
}
else if (c == 'N' || c == 'n')
{
printf("Not agreed.\n");
}
}
-16
View File
@@ -1,16 +0,0 @@
// Addition with int, allows for overflow
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
int x = get_int("x: ");
// Prompt user for y
int y = get_int("y: ");
// Perform addition
printf("%i\n", x + y);
}
-16
View File
@@ -1,16 +0,0 @@
// Addition with long
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Perform addition
printf("%li\n", x + y);
}
-16
View File
@@ -1,16 +0,0 @@
// Division with longs, demonstrating truncation
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Divide x by y
printf("%li\n", x / y);
}
-17
View File
@@ -1,17 +0,0 @@
// Division with longs, demonstrating truncation
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Divide x by y
float z = x / y;
printf("%f\n", z);
}
-17
View File
@@ -1,17 +0,0 @@
// Division with longs, demonstrating type casting
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Divide x by y
float z = (float) x / (float) y;
printf("%f\n", z);
}
-17
View File
@@ -1,17 +0,0 @@
// Division with longs, demonstrating floating-point imprecision
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Divide x by y
float z = (float) x / (float) y;
printf("%.20f\n", z);
}
-17
View File
@@ -1,17 +0,0 @@
// Division with longs, demonstrating double
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Divide x by y
double z = (double) x / (double) y;
printf("%.20f\n", z);
}
-17
View File
@@ -1,17 +0,0 @@
// Conditionals, Boolean expressions, relational operators
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for integers
int x = get_int("What's x? ");
int y = get_int("What's y? ");
// Compare integers
if (x < y)
{
printf("x is less than y\n");
}
}
-21
View File
@@ -1,21 +0,0 @@
// Conditionals, Boolean expressions, relational operators
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for integers
int x = get_int("What's x? ");
int y = get_int("What's y? ");
// Compare integers
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
}
-25
View File
@@ -1,25 +0,0 @@
// Conditionals, Boolean expressions, relational operators
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for integers
int x = get_int("What's x? ");
int y = get_int("What's y? ");
// Compare integers
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else if (x == y)
{
printf("x is equal to y\n");
}
}
-25
View File
@@ -1,25 +0,0 @@
// Conditionals, Boolean expressions, relational operators
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for integers
int x = get_int("What's x? ");
int y = get_int("What's y? ");
// Compare integers
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
}
-15
View File
@@ -1,15 +0,0 @@
// Counting
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int i = 0;
while (true)
{
printf("\r%i", i);
i++;
}
}
-14
View File
@@ -1,14 +0,0 @@
// Counting
#include <stdbool.h>
#include <stdio.h>
int main(void)
{
int i = 0;
while (true)
{
printf("\r%i", i);
i++;
}
}
-8
View File
@@ -1,8 +0,0 @@
// A program that says hello to the world
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
-10
View File
@@ -1,10 +0,0 @@
// get_string and printf with incorrect placeholder
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, answer\n");
}
-10
View File
@@ -1,10 +0,0 @@
// get_string and printf with %s
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, %s\n", answer);
}
-8
View File
@@ -1,8 +0,0 @@
// Prints a row of 4 question marks
#include <stdio.h>
int main(void)
{
printf("????\n");
}
-12
View File
@@ -1,12 +0,0 @@
// Prints a row of 4 question marks with a loop
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 4; i++)
{
printf("?");
}
printf("\n");
}
-11
View File
@@ -1,11 +0,0 @@
// Prints a column of 3 bricks with a loop
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 3; i++)
{
printf("#\n");
}
}
-15
View File
@@ -1,15 +0,0 @@
// Prints a 3-by-3 grid of bricks with nested loops
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("#");
}
printf("\n");
}
}
-16
View File
@@ -1,16 +0,0 @@
// Prints a 3-by-3 grid of bricks with nested loops using a constant
#include <stdio.h>
int main(void)
{
const int n = 3;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
-18
View File
@@ -1,18 +0,0 @@
// Prints an n-by-n grid of bricks with nested loops
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Size: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
-22
View File
@@ -1,22 +0,0 @@
// Prints an n-by-n grid of bricks, re-prompting user for positive integer
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Size: ");
while (n < 1)
{
n = get_int("Size: ");
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
-25
View File
@@ -1,25 +0,0 @@
// Prints an n-by-n grid of bricks, re-prompting user for positive integer
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Get size of grid
int n;
do
{
n = get_int("Size: ");
}
while (n < 1);
// Print grid of bricks
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
-36
View File
@@ -1,36 +0,0 @@
// Prints an n-by-n grid of bricks, re-prompting user for positive integer
#include <cs50.h>
#include <stdio.h>
int get_size(void);
void print_grid(int n);
int main(void)
{
int n = get_size();
print_grid(n);
}
int get_size(void)
{
int n;
do
{
n = get_int("Size: ");
}
while (n < 1);
return n;
}
void print_grid(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
-10
View File
@@ -1,10 +0,0 @@
// Opportunity for better design
#include <stdio.h>
int main(void)
{
printf("meow\n");
printf("meow\n");
printf("meow\n");
}
-13
View File
@@ -1,13 +0,0 @@
// Better design
#include <stdio.h>
int main(void)
{
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
}
-11
View File
@@ -1,11 +0,0 @@
// Better design
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
}
-19
View File
@@ -1,19 +0,0 @@
// Abstraction
#include <stdio.h>
void meow(void);
int main(void)
{
for (int i = 0; i < 3; i++)
{
meow();
}
}
// Meow once
void meow(void)
{
printf("meow\n");
}
-19
View File
@@ -1,19 +0,0 @@
// Abstraction with parameterization
#include <stdio.h>
void meow(int n);
int main(void)
{
meow(3);
}
// Meow some number of times
void meow(int n)
{
for (int i = 0; i < n; i++)
{
printf("meow\n");
}
}
-24
View File
@@ -1,24 +0,0 @@
// Conditionals, Boolean expressions, relational operators
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for points
int points = get_int("How many points did you lose? ");
// Compare points against mine
if (points < 2)
{
printf("You lost fewer points than me.\n");
}
else if (points > 2)
{
printf("You lost more points than me.\n");
}
else if (points == 2)
{
printf("You lost the same number of points as me.\n");
}
}
-24
View File
@@ -1,24 +0,0 @@
// Design
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for points
int points = get_int("How many points did you lose? ");
// Compare points against mine
if (points < 2)
{
printf("You lost fewer points than me.\n");
}
else if (points > 2)
{
printf("You lost more points than me.\n");
}
else
{
printf("You lost the same number of points as me.\n");
}
}
-27
View File
@@ -1,27 +0,0 @@
// Constants
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Number of points that I lost
const int MINE = 2;
// Prompt user for points
int points = get_int("How many points did you lose? ");
// Compare points against mine
if (points < MINE)
{
printf("You lost fewer points than me.\n");
}
else if (points > MINE)
{
printf("You lost more points than me.\n");
}
else
{
printf("You lost the same number of points as me.\n");
}
}

Some files were not shown because too many files have changed in this diff Show More