Happy About the progress
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
libcs50/
|
||||||
|
a.out
|
||||||
Executable
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,21 @@
|
|||||||
|
.global _start
|
||||||
|
.intel_syntax noprefix
|
||||||
|
|
||||||
|
_start:
|
||||||
|
// sys_write
|
||||||
|
mov rax, 1
|
||||||
|
mov rdi, 1
|
||||||
|
lea rsi, [hello_world]
|
||||||
|
mov rdx, 14
|
||||||
|
syscall
|
||||||
|
|
||||||
|
|
||||||
|
// sys_exit
|
||||||
|
mov rdi, 60
|
||||||
|
mov rdi, 69
|
||||||
|
syscall
|
||||||
|
|
||||||
|
hello_world:
|
||||||
|
.asciz "Hello, World!\n"
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
@@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
# VIM Notes
|
||||||
|
|
||||||
|
:wq - Save and Exit
|
||||||
|
:q! - Exit without Saving
|
||||||
|
:q - Exit only If Unchanged.
|
||||||
|
:x Quick Save and Exit.
|
||||||
|
|
||||||
|
# Keyboard Shortcuts
|
||||||
|
ESC - From Normal to a state where you can enter commands.
|
||||||
|
I = Get into Insert mode.
|
||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,20 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <cs50.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
|
||||||
|
{
|
||||||
|
char c = get_char("Do you agree? ");
|
||||||
|
|
||||||
|
if (c == 'y' || c == 'Y')
|
||||||
|
|
||||||
|
{
|
||||||
|
printf("Agreed.\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Not Agreed.'\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
//Proacive Promise for future.
|
||||||
|
void meow(int tiomes);
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
int n = get_int("What's n? ");
|
||||||
|
meow(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void meow(int times)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < times; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
|
||||||
|
{
|
||||||
|
int x = get_int("What's x? ");
|
||||||
|
int y = get_int("what's y? ");
|
||||||
|
|
||||||
|
if (x < y)
|
||||||
|
|
||||||
|
{
|
||||||
|
printf("x is greater than y\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#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,13 @@
|
|||||||
|
Conditional
|
||||||
|
If (boolean-expression)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the boolean-expression evaluates to true , all lines of code between phrases.
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
string name;
|
||||||
|
int votes;
|
||||||
|
|
||||||
|
} candidate;
|
||||||
|
|
||||||
|
candidate get_candidate(void);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
candidate candidates[3];
|
||||||
|
|
||||||
|
for (int i =0; i <3; i++)
|
||||||
|
{
|
||||||
|
candidates[i] = get_candidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
printf("Candidate %i is named %s and has %i votes.\n",i + 1, candidates[i].name, candidates[i].votes);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get a new candidate
|
||||||
|
|
||||||
|
candidate get_candidate(void)
|
||||||
|
{
|
||||||
|
candidate new_candidate;
|
||||||
|
new_candidate.name = get_string("Name: ") ;
|
||||||
|
new_candidate.votes = get_int("Votes:") ;
|
||||||
|
|
||||||
|
return new_candidate;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
string name = get_string("What's your name> ");
|
||||||
|
printf("hello, %s\n", name);
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
{
|
||||||
|
int x = get_int();
|
||||||
|
switch(x)
|
||||||
|
{
|
||||||
|
case 5:
|
||||||
|
printf("Five, ");
|
||||||
|
case 4:
|
||||||
|
printf("Four, ");
|
||||||
|
case 3:
|
||||||
|
printf("Three, ");
|
||||||
|
case 2:
|
||||||
|
printf("Two, ");
|
||||||
|
case 1:
|
||||||
|
printf("One, ");
|
||||||
|
default:
|
||||||
|
printf("Blast-off!\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
//Boolean expression.
|
||||||
|
if (balance < 0)
|
||||||
|
|
||||||
|
{
|
||||||
|
printf("Insufficient Funds");
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Available Balance");
|
||||||
|
}
|
||||||
|
|
||||||
|
//For Loops
|
||||||
|
for (int i =1; i <=30; i++)
|
||||||
|
{
|
||||||
|
printf("I can count to %i!\n", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// While Loops
|
||||||
|
int i =1;
|
||||||
|
while (i <= 30)
|
||||||
|
{
|
||||||
|
printf("I can count to %i")
|
||||||
|
i++; //Direction ->
|
||||||
|
}
|
||||||
|
|
||||||
|
int n;
|
||||||
|
{
|
||||||
|
n = get_int("N; ");
|
||||||
|
|
||||||
|
}
|
||||||
|
while (n <= 0);
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
|
||||||
|
{
|
||||||
|
string name = get_string("Name: ");
|
||||||
|
int age = get_int("Age: " );
|
||||||
|
string phone = get_string("Phone number: ");
|
||||||
|
string location = get_string("what's your location? " );
|
||||||
|
// Correct Version of This program is
|
||||||
|
// int age = get_int("Age: ");
|
||||||
|
// int phone = get_int("Phone: ")";"
|
||||||
|
// Location is correct on the above program so its okay.
|
||||||
|
|
||||||
|
// printf("Name:, %s\n", name);
|
||||||
|
// printf("Age:, %s\n", age);
|
||||||
|
// printf("Location:, %s\n", location);
|
||||||
|
|
||||||
|
printf("New Contacts: %s, %i, lives in %s, and can be reached at %s\n", name, age, location, phone );
|
||||||
|
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int factorial(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n = get_int("Factorial of: ");
|
||||||
|
printf("Factorial of %i is %i\n", n, factorial(n));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int factorial(int n)
|
||||||
|
{
|
||||||
|
// Base case
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive case
|
||||||
|
return n * factorial(n - 1);
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int fib(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
|
||||||
|
{
|
||||||
|
int n = get_int("Fibonacci: ");
|
||||||
|
printf("The %i Fibonacci number is %i\n", n, fib(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Takes which fibonacci number you want, returns that
|
||||||
|
|
||||||
|
int fib(int n)
|
||||||
|
{
|
||||||
|
// Base Case
|
||||||
|
if (n ==0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 1)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renursive Case
|
||||||
|
return fib(n-1) + fib(n-2);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
string name = get_string("What's your name? ");
|
||||||
|
// %s = percentage i just operator and "s"is for string.
|
||||||
|
printf("Hello, %s\n", name);
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Prototype
|
||||||
|
void draw(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int height = get_int("Height: ");
|
||||||
|
draw (height);
|
||||||
|
|
||||||
|
}
|
||||||
|
// Prototype now being used.
|
||||||
|
void draw(int n)
|
||||||
|
{
|
||||||
|
// for each row of pyramid
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < i; j++)
|
||||||
|
{
|
||||||
|
printf("#");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const int n = 3;
|
||||||
|
//for each row
|
||||||
|
for (int row = 0; row < 10; row++)
|
||||||
|
{
|
||||||
|
//for Each Coloumn
|
||||||
|
for (int column = 0; column < 10; column++)
|
||||||
|
{
|
||||||
|
// Print on brick
|
||||||
|
printf("#");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void print_row(int bricks);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for heing (int)
|
||||||
|
int height = get_int("Height: ");
|
||||||
|
|
||||||
|
// Print Payramid of that height
|
||||||
|
for (int i = 0; i <= height; i++)
|
||||||
|
{
|
||||||
|
print_row(i + 1);
|
||||||
|
|
||||||
|
//Print row based on height
|
||||||
|
printf("#");
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// Given a how many a # to print out, print that number of bricks
|
||||||
|
void print_row(int bricks)
|
||||||
|
{
|
||||||
|
for (int i = 0; i< bricks; i++)
|
||||||
|
{
|
||||||
|
printf("#");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# Check Code Server Status
|
||||||
|
sudo systemctl status code-server@$USER
|
||||||
|
|
||||||
|
# Start the Code Server
|
||||||
|
sudo systemctl start code-server@$USER
|
||||||
|
|
||||||
|
# Git - Cheat Sheet
|
||||||
|
|
||||||
|
// First Set the Username and Email
|
||||||
|
git config --global user.name "Hayet"
|
||||||
|
git config --global user.email "your_email@example.com"
|
||||||
|
|
||||||
|
//
|
||||||
|
git status
|
||||||
|
git add.
|
||||||
|
git commit -m "Happy About the progress"
|
||||||
|
|
||||||
|
// Ignore a specific File
|
||||||
|
vi .gitignore
|
||||||
|
- libcs50/
|
||||||
|
- a.out
|
||||||
|
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
|
||||||
|
{
|
||||||
|
string name;
|
||||||
|
string number;
|
||||||
|
|
||||||
|
} person;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
|
||||||
|
{
|
||||||
|
person people[3];
|
||||||
|
|
||||||
|
people[0].name = ""Kelly";
|
||||||
|
people[0].number = "+091398023890123";
|
||||||
|
|
||||||
|
people[1].name = "Hayet";
|
||||||
|
people[1].number = "77666327";
|
||||||
|
|
||||||
|
people [2].name ="Munna";
|
||||||
|
people[2].number ="59950786";
|
||||||
|
|
||||||
|
string name = get_string("Name: ");
|
||||||
|
for (int i = 0 ; i < 3; i++)
|
||||||
|
|
||||||
|
{
|
||||||
|
if (strcmp(people[i].name, name) == 0)
|
||||||
|
{
|
||||||
|
printf("Found %s\n", people[i].number);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Not fouind \n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
nano hello.c
|
||||||
|
|
||||||
|
#----------------- Program -------------------
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
printf("Hello, world!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ----------- End ------------------------
|
||||||
|
|
||||||
|
# Compilation
|
||||||
|
gcc hello.c -o hello#
|
||||||
|
|
||||||
|
./hello
|
||||||
|
|
||||||
|
# -------------------------------------------
|
||||||
|
Important Bookmarks
|
||||||
|
|
||||||
|
https://cs50.harvard.edu/x/notes/1/#header-files-and-cs50-manual-pages
|
||||||
|
https://manual.cs50.io/
|
||||||
|
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void draw(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
|
||||||
|
{
|
||||||
|
int height = get_int("Height; ");
|
||||||
|
|
||||||
|
draw(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(int n)
|
||||||
|
{
|
||||||
|
// Base case, making sure loop has exit condition.
|
||||||
|
if (n <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print a pyramid of height n -1
|
||||||
|
draw (n-1);
|
||||||
|
|
||||||
|
// Print one more row
|
||||||
|
for (int i =0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("#");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
|
||||||
|
{
|
||||||
|
int numbers[] = {20, 500, 10, 5, 100, 1, 50};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// Comparing against lowercase char
|
||||||
|
|
||||||
|
#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')
|
||||||
|
{
|
||||||
|
printf("Agreed.\n");
|
||||||
|
}
|
||||||
|
else if (c == 'n')
|
||||||
|
{
|
||||||
|
printf("Not agreed.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// Comparing against lowercase and uppercase char
|
||||||
|
|
||||||
|
#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')
|
||||||
|
{
|
||||||
|
printf("Agreed.\n");
|
||||||
|
}
|
||||||
|
else if (c == 'Y')
|
||||||
|
{
|
||||||
|
printf("Agreed.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Not agreed.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
{
|
||||||
|
printf("Not agreed.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// Addition with int
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for x
|
||||||
|
int x = get_int("What's x? ");
|
||||||
|
|
||||||
|
// Prompt user for y
|
||||||
|
int y = get_int("What's y? ");
|
||||||
|
|
||||||
|
// Add numbers
|
||||||
|
int z = x + y;
|
||||||
|
|
||||||
|
// Perform addition
|
||||||
|
printf("%i\n", z);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// Addition with int, without third variable
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for x
|
||||||
|
int x = get_int("What's x? ");
|
||||||
|
|
||||||
|
// Prompt user for y
|
||||||
|
int y = get_int("What's y? ");
|
||||||
|
|
||||||
|
// Perform addition
|
||||||
|
printf("%i\n", x + y);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// Doubles a number
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for x
|
||||||
|
int x = get_int("What's x? ");
|
||||||
|
|
||||||
|
// Double it
|
||||||
|
printf("%i\n", x * 2);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// Overflow
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int dollars = 1;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
char c = get_char("Here's $%i. Double it and give to next person? ", dollars);
|
||||||
|
if (c == 'y')
|
||||||
|
{
|
||||||
|
dollars *= 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Here's $%i.\n", dollars);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// long
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
long dollars = 1;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
char c = get_char("Here's $%li. Double it and give to next person? ", dollars);
|
||||||
|
if (c == 'y')
|
||||||
|
{
|
||||||
|
dollars *= 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Here's $%li.\n", dollars);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// Division with ints, demonstrating truncation
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for x
|
||||||
|
int x = get_int("What's x? ");
|
||||||
|
|
||||||
|
// Prompt user for y
|
||||||
|
int y = get_int("What's y? ");
|
||||||
|
|
||||||
|
// Divide x by y
|
||||||
|
printf("%i\n", x / y);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// Casting
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for x
|
||||||
|
int x = get_int("What's x? ");
|
||||||
|
|
||||||
|
// Prompt user for y
|
||||||
|
int y = get_int("What's y? ");
|
||||||
|
|
||||||
|
// Divide x by y
|
||||||
|
printf("%f\n", (float) x / y);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// Floats
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Prompt user for x
|
||||||
|
float x = get_float("What's x? ");
|
||||||
|
|
||||||
|
// Prompt user for y
|
||||||
|
float y = get_float("What's y? ");
|
||||||
|
|
||||||
|
// Divide x by y
|
||||||
|
printf("%.50f\n", x / y);
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// Opportunity for better design
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
printf("meow\n");
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
// Better design
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 3;
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// Uses a do-while loop instead.
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
n = get_int("What's n? ");
|
||||||
|
}
|
||||||
|
while (n < 0);
|
||||||
|
|
||||||
|
for (int i = 0; i < n; 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,20 @@
|
|||||||
|
// Demonstrates scope
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void meow(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n = 3;
|
||||||
|
meow(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Meow some number of times
|
||||||
|
void meow(int n)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
// User input
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void meow(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
n = get_int("Number: ");
|
||||||
|
}
|
||||||
|
while (n < 1);
|
||||||
|
meow(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Meow some number of times
|
||||||
|
void meow(int n)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
// Return value
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int get_positive_int(void);
|
||||||
|
void meow(int n);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n = get_positive_int();
|
||||||
|
meow(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get number of meows
|
||||||
|
int get_positive_int(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
n = get_int("Number: ");
|
||||||
|
}
|
||||||
|
while (n < 1);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Meow some number of times
|
||||||
|
void meow(int n)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
// Print values of i
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 1;
|
||||||
|
while (i <= 3)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
// Better design
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
while (i < 3)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// Better design
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
// Infinite loop
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
// Prompts user for n.
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n = get_int("What's n? ");
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
// Prompts user again if need be. (Poor design.)
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n = get_int("What's n? ");
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
n = get_int("What's n? ");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
// Uses a loop with continue/break.
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
n = get_int("What's n? ");
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
// Uses a loop with just break.
|
||||||
|
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
n = get_int("What's n? ");
|
||||||
|
if (n >= 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("meow\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// Conditional, Boolean expression, relational operator
|
||||||
|
|
||||||
|
#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 that are mutually exclusive
|
||||||
|
|
||||||
|
#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 that aren't mutually exclusive
|
||||||
|
|
||||||
|
#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");
|
||||||
|
}
|
||||||
|
if (x > y)
|
||||||
|
{
|
||||||
|
printf("x is greater than y\n");
|
||||||
|
}
|
||||||
|
if (x == y)
|
||||||
|
{
|
||||||
|
printf("x is equal to y\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
// Conditional that isn't necessary
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
#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,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,23 @@
|
|||||||
|
// Helper function
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void print_row(int width);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const int n = 3;
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
print_row(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_row(int width)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < width; i++)
|
||||||
|
{
|
||||||
|
printf("#");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
string name;
|
||||||
|
int votes;
|
||||||
|
|
||||||
|
} candidate;
|
||||||
|
|
||||||
|
candidate get_candidate(void);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
candidate candidates[3];
|
||||||
|
|
||||||
|
for (int i =0; i <3; i++)
|
||||||
|
{
|
||||||
|
candidates[i] = get_candidate();
|
||||||
|
printf("Candidate %i is named %s and has %i votes. \n", i + 1, candidates[i].name, candidates[i].votes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get a new candidate
|
||||||
|
|
||||||
|
candidate get_candidate(void)
|
||||||
|
{
|
||||||
|
candidate new_candidate;
|
||||||
|
new_candidate.name = get_string("Name: ") ;
|
||||||
|
new_candidate.votes = get_int("Votes:") ;
|
||||||
|
|
||||||
|
return new_candidate;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
string name;
|
||||||
|
int votes;
|
||||||
|
|
||||||
|
} candidate;
|
||||||
|
|
||||||
|
candidate get_candidate(void);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
candidate candidates[3];
|
||||||
|
|
||||||
|
for (int i =0; i <3; i++)
|
||||||
|
{
|
||||||
|
candidates[i] = get_candidate();
|
||||||
|
printf("Candidate %i is named %s and has %i votes. \n", i + 1, candidates[i].name, candidates[i].votes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get a new candidate
|
||||||
|
|
||||||
|
candidate get_candidate(void)
|
||||||
|
{
|
||||||
|
candidate new_candidate;
|
||||||
|
new_candidate.name = get_string("Name: ") ;
|
||||||
|
new_candidate.votes = get_int("Votes:") ;
|
||||||
|
|
||||||
|
return new_candidate;
|
||||||
|
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
int main (void)
|
||||||
|
|
||||||
|
{
|
||||||
|
string strings [] = {"battleship", "boot", "cannon", "iron", "thimble", "top hat"};
|
||||||
|
|
||||||
|
string s = get_string("Strings: ");
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(strings [i], == s) == 0)
|
||||||
|
{
|
||||||
|
printf("Found\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Not found \n");
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user