CS50 Week 0 – Scratch 6/25/2024
Hand digits – 5 fingers = 31 permutations + 1 (0) = 32.
BIT – Binary Digit (0 or 1)
Base 10 digit system (exponents of 10)– 123 = (100x1 + 20x1 + 3x1)
11111111 = 8 bits, each can be 0 or 1 = 255 (1,2,4,8,16,32,64,128) = 2 to the 8 th power. (256-1
accounts for 0 = 255)
Literal – electricity, switches (metaphorically light bulbs in specific positions)
A = 65 by convention using 7 or 8 bits (used for letters of the alphabet) = 01000001 = ASCII
(American standard code for information interchange).
HI! = 72, 73, 33
Unicode’s (more than 8 bits) purpose is to preserve all human languages for history.
U+1F602 – abbreviates code (emoji)
Fitzpatrick scale – categorizes skin tone for emoji’s
RGB - colors
RED GREEN BLUE = yellow.
CAN GIVEevery color of rainbow
Week 1 Coding with C
Write (code), compile (make) and Run (./ ) code.
./ is like double clicking an icon to run
To create a code type:
“code meow.c” in terminal (bottom window)
#include <stdio.h>
Int main(void)
{
Int i = 5;
While (i > 0)
{
printf(“meow/n”);
i --;
}
}
Or
Int I = 1
While I <=5
OR
Int I = 0
While I < 5
All same, last is “best”
If you are copying and pasting there’s probably a more efficient way to code it.
Code – implementation of algorithms in computers. Algorithm in physical world, code in
computer world.
Source code machine code via “compiler”
Terminal window = where you type commands.
Graphical user interface GUI
Vs. Command line interface – only uses keyboard
Cs50.dev coding website
Print f(“ \n”);
= say
Purple code = functions in VS code
\n = next line
“Go to prompt” means go to dollar sign symbol – convention
Convention - .c for C programs
Always lower case file names, no spaces
; = period – don’t forget!
Printf(“hello, world/n”);
“Answer =” (store answer as answer)
Functions are implementation of algorithms in code. Input is argument/parameters. Output is
“side effects”
Printf(“hello, %/n”, answer);
#include <cs50.h>
#include <stdio.h>
Means include compiler languages from already existing program options so we can use in our
code
Get_string
Get_int
“=” is ==
\n = escape sequence - ensures new line at end of your output (avoids auto $ at end of script
command
Create, compile and run:
Code hello.c
.c is convention for file extension in C
All file names are lower case, never use spaces and file extension is lower case.
make hello
./hello = run hello
Manual.cs50.io website
Manual.cs50.io stdio.h
$ = prompt or terminal window convention
Arguments – algorithm return value
String
Int number
%s = placeholder (“format code” jargon) for string of words/texts
%i = integers
Conditionals – fork in road
Boolean = yes or no, true or false question. If x < y
If, else
Int counter
Counter++ = counter plus 1 (add 1)
Counter -- = counter minus 1
1. #include <cs50.h>
2. #include <stdio.h>
“When green flag clicked”: =
Int main (void)
{
Int x = get_int(“What’s x? “);
Int y = get_int(“What’s y? ”)
If (x < y)
Printf(“x is less than y/n”);
}
If, else if vs else – else if saves time. Else would be final step
Data types: Strings, integers, chars
Char = a single character always
Get_char = get a single character from user.
Strings of text use double quotes, single characters use single quotes
|| = or
&& = both true
“While” loop- while i < 3 – repeat 3 times
Int counter = 3;
While (counter > 0)
{
Printf (“meow/n);
Counter = counter – 1;
}
Forever loop – “ while (true)”
“For” loop – same idea, different syntax – shorter, better, more commonly used.
For (Int i = 0; i < 3; i ++)
Printf(“meow]n”);
(meow 0, 1, 2 = 3 times.)
In for loop, true = forever
For (initialization; condition; update);
{//code to repeat}
For (i = 0, i < 10, i++)
{ printf(“Hello!\n”);
= Hello! 10 times
Int main(void)
#include means include the libraries functions
Must define a function before using it literally
ONLY exception: Void meow(void); single tells compiler it doesn’t exist but it will further down.
So can copy paste it up higher before using it.
Mac, windows and Linux
// = private reminder comment of what following code is for, just for human’s eyes, computer
ignores
“Long” = 64 bits
Format codes:
%i – get integer
%li long integer
%f – float
%char – get character
%s - strings
Float – 32 bits
Double – 64 bits
Truncation = gets rid of everything after decimal point (integers only). Int divided by int = int.
float divided by float = float.
Type casting – converting/treating one type to another (takes away truncation. 1/3
fraction 0.33333 not 0
HELLO WORLD
Mk dir = make file (make directory?)
Cd world (create document? CHANGE DIRECTORIES in file World) – Creates document in file
WORLD
Mario:
Void mario(int n);
Int main(void)
Mario();
}
}
Void mario (int n)
{
For (int I = 0; I <n; i--
Printf(“#”):
----------
#include <stdio.h>
Int main(void)
{
Int n = get_int(“Size: “);
While (n < 1)
For (int i = 0; i < n; i ++)
{
For (int j = 0; j <n; j++)
}
Printf(“#”);
}
Printf(“/n”);
}
}
Constant – keeps it permanent in code – can’t change it by mistake
Const int n = 3
“do while” loop
Do
{
N = get_int(“size: “);
Examples from section 1:
Int age = age as an integer
String name = name as a string
Int calls = 4;
Printf(“calls is %i/n”, calls);
Codes “calls is 4”
Hello world example:
Code hello.c
Include <cs50.h>
Include <stdio.h>
Int main(void)
{
String name = get_string(“What is your name? ”
Printf(“hello, %s\n”, name);
}
Make hello – tells computer to change my code to computer code.
Final “Hello ___”
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = get_string("What is your name?\n");
printf("hello, %s\n", name);
}
#### in code is:
Int j = 0
While (j < 4)
{
Printf(“#”);
J++;
}
Printf)”/n”);
Vs.
For (int I = 0; i < 4; i++)
{
For (int j = 0; j < 4; j++)c
{
Printf(“#”);
}
Printf(“\n”);
}
For loop is good if you know the number of times you want to iterate. While loop is good for if
you don’t know, just as long as the conditional is true.
Int height = get_int(“Height: “);
MARIO FINAL SUBMISSION: RIGHT ALIGNED BOXES/HASHES:
But does i start on new line why do hashes start at 0 and not 1, spaces starxt at 4 and not 5 but i
should start on new line?
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int h;
int spaces;
int bricks;
do
{
h = get_int("Height: ");
}
while (h < 1);
for (int i = 0; i < h; i++)
{
for (spaces = (h - i); spaces >= 2; spaces--)
{
printf(" ");
}
for (bricks = 0; bricks <= i; bricks++)
{
printf("#");
}
printf("\n");
}
}