added ws202425 courses
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Counting
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
printf("\r%i", i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Counting
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
printf("\r%i", i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// A program that says hello to the world
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("hello, world\n");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// 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");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Prints a row of 4 question marks
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("????\n");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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");
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Prints a column of 3 bricks with a loop
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
printf("#\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Opportunity for better design
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("meow\n");
|
||||
printf("meow\n");
|
||||
printf("meow\n");
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Better design
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < 3)
|
||||
{
|
||||
printf("meow\n");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Better design
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
printf("meow\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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");
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user