added ws202425 courses

This commit is contained in:
2024-11-14 13:11:04 +01:00
parent 800282c2e8
commit beb31897b1
461 changed files with 20368 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
// 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
@@ -0,0 +1,25 @@
// 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
@@ -0,0 +1,36 @@
// 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
@@ -0,0 +1,23 @@
// 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
@@ -0,0 +1,34 @@
// 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
@@ -0,0 +1,23 @@
// 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
@@ -0,0 +1,24 @@
// 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;
}