Monday, August 31, 2020
linking - combining 0s and 1s from our source code and the various libraries
assembling - taking assembly code and converting to 0s and 1s
compiling - although this is often used as a generalized and simplified term, it actually has a more precise definition
for example:
...
main: # @main
.cfi_startproc
# BB#0:
pushq %rbp
.Ltmp0:
.cfi_def_cfa_offset 16
.Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
subq $16, %rsp
xor1 %eax, %eax
mov1 %eax, %edi
movabsq $.L.str, %rsi
movb $0, %al
callq get_string
movabsq $.L.str, %rdi
movq %rax, -8(%rbp)
movq -8(%rbp), %rsi
movb %0, %al
callq printf
...
cs50, lec2
pre-processing
for example:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string s = get_string("what is your name\n");
printf("hello, %s\n", s);
}
becomes
....
string get_string(string prompt);
void printf(string format, .....);
....
int main(void)
{
string s = get_string("what is your name\n");
printf("hello, %s\n", s);
}
.................the actual code pasted in is simplified here, but this is enough to illustrate the concept......
cs50, lec2
The term "compile" is often used as an "over-simplification" of 4 actions:
compiling
assembling
linking
.......these 4 steps are invoked when we run clang or make
cs50, lec2
get a string and print a string (lec2 reviewing lec1)
#include <stdio.h>
int main(void)
{
string s = get_string("what is your name?\n");
printf("hi, %s\n", s);
}
cs50, lec2 (reviewing lec1)
Sunday, August 30, 2020
say - command (program) that comes with some systems that speaks out what you type
say this is cs50
(this works on mac)
figlet - command (program) that comes with some systems which produces ASCII art
figlet this is cs50
floating point imprecision & integer overflow
positive.c
#include <stdio.h>
int get_positive_int(void);
int main(void)
{
int i = get_positive_int();
printf("The positive integer you entered is: %i\n", i);
}
get_positive_int(void)
{
int n;
do
{
n = get_int("Enter a positive integer:\n");
}while(n<1)
return n;
}
it is convention to have main() up top before other function definitions
(C is dumb in that it does not scan further down to look for function definitions)
function prototypes : C is dumb
#include <stdio.h>
void cough(int n);
int main(void)
{
cough(3);
}
void cough(int n)
{
for (int i; i<n; i++)
{
printf("cough\n");
}
}
defining your own functions: abstraction
#include <stdio.h>
int main(void)
{
void cough(void)
{
printf("cough\n");
}
for (int i=0; i<3; i++)
{
cough();
}
}
agree.c
#include <stdio.h>
int main(void)
{
char c = get_char("do you agree?\n");
if (c== 'y' || c=='Y')
{
printf("agreed\n");
}
else if (c=='n' || c=='N')
{
printf("do not agree\n");
}
}
parity.c
#include <stdio.h>
int main(void)
{
int n = get_int("please enter an integer:\n");
if(n % 2 == 0)
{
printf("the number you entered is an even number\n");
}
else
{
printf("the number you entered is odd.\n");
}
}
Saturday, August 29, 2020
float.c : get a floating point number from the user
#include <stdio.h>
int main(void)
{
float price = get_float("what is the price?\n");
printf("the price plus tax is %f\n", price*1.0625);
}
limit the output to 2 digits after the decimal point:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
float price = get_float("what is the price?\n");
printf("the price plus tax is %.2f\n", price*1.0625);
}
[Source: harvard, cs50]
int.c : get an integer from user
#include <stdio.h>
int main(void)
{
int age = get_int("What's our age?\n");
int days = age*365;
printf("You are at least %i days old\n", days);
}
better design:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int age = get_int("What's our age?\n");
printf("You are at least %i days old\n", age*365);
}
you can even do this: (but in terms of design, this may reach an inflection point at which the code becomes too hard to read)
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("You are at least %i days old\n", get_int("What's your age?\n")*365);
}
[Source: harvard, cs50]
get_string, get_char, get_int, get_long, get_float, get_double, ...
these are all functions that will prompt the human user for certain values
Data types in C: bool, char, double, float, int, long, string
char (a single character, not two or more)
int (an integer; it has a certain size; you can only count up to a certain size number with int, typically
4 billion)
float (floating point number, which is a fancy way of saying a real number [something that has a
decimal point])
double (just a real number that can have even more digits after the decimal point)
long (uses more bits so can count higher than int; companies like Facebook and Google have more
data than 4 billion, so they may need these types of numbers)
string (one or more characters inside double quotes " " )
...
.
.
.
[source: harvard, cs50, david malan]
Repeat 50 times: for ( int i=0 ; i < 50 ; i++ ) { }
{
printf("hello, world\n");
}
....... the syntax of the "for loop" is a little more funky (than the while loop), but the for loop is more succinct while achieving the exact same thing
[Source: Harvard, CS50]
Thursday, August 27, 2020
if, else if, else
{
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");
}
if, else if, else if
{
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");
}
make - included in Linux, MacOS, and Windows these days
make string
if you type the command "make hello" , the program make will look for a file called "hello.c" and compile it into the executable "hello"
[Source: Harvard, CS50]
clang -o string string.c -lcs50
you also have to "-lcs50"
where "l" stands for "link"
[Source: Harvard, CS50]
Monday, August 24, 2020
String.c
#include <cs50.h>
int main(void)
{
String name = get_string("Please enter your name:");
printf("Hello, %s\n", name);
}
Source: Harvard, CS50
clang compile error message: hello.c:5:26 ......... this tells you to look on line 5 character 26
it can't tell you exactly what's wrong, but can tell you approximately where it is happening.....start googling
Sunday, August 23, 2020
"rm" = "remove"
for example:
rm a.out
.........the system will then prompt:
remove regular file a.out?
-------> type "y" or "yes" to confirm deletion
Saturday, August 22, 2020
The * behind a file name denotes that that file is an "executable"
"clang -o hello hello.c"
"Options" or "command line arguments"
"-o" stands for output
Rename the output file to anything you want.
"New line character" is represented by "\n"
This is a special command or special character that tells the computer to have the cursor go to a new line.
"a.out" stands for "assembly output"
It is the machine code (binary) a computer can actually understand and run.
The dollar sign in the terminal window is just the "prompt" or "shell"
It just tells you to "type here".
A compiler is also a software program
A compiler takes source code and produces machine code (0's and 1's).
Friday, August 21, 2020
Software - abstraction - have functions instead of having everything mixed together
In code, abstract away functions instead of having long linear complex segments of code.
Software - Loop instead of Copy&Paste - better design
In code, your should not have to copy&paste any code, use loops instead - better design
Wednesday, August 19, 2020
Software - 2 threads at the same time
in scratch
2 "when green flag clicked" blocks
the 2 threads communicate with each other using things like variables
Software engineering - toggle - Technique often used
A technique often used in software engineering is :
when a key is pressed, check a variable and toggle it
Programming - Topics to be discussed
- functions
-conditions
-Boolean expressions
-loops
-variables
-threads
-events
...
[Source: Harvard, CS50]
Tuesday, August 18, 2020
Represent music in a computer
We can use three number values: note, duration, and volume
to represent each music sound
Sunday, August 16, 2020
RGB - System for representing colored dots
ASCII and unicode represents characters and emoji information.
The RGB system uses binary numbers to represent colored dots which in turn compose what is displayed on our computer screens.
[Source: Harvard CS50]
128514 - face with tears of joy (emoji)
Unicode number for "face with tears of joy" emoji is 128514.
Binary pattern: "111110011000000010"
[Source: Harvard CS50]
Unicode is a superset of ASCII
ASCII uses 8 bits to represent information,
Unicode uses 8 or 16 or 32 or 64 bits or more to represent information, therefore, Unicode can represent more types of characters and emoji.
Emoji are included in Unicode.
ASCII - American Standard Code for Information Interchange
.... A B C D E F G H I ....
.... 65 66 67 68 69 70 71 72 73 ....
[Source: Harvard Cs50 ]
Saturday, August 15, 2020
Humans often count in decimal
Humans often represent numbers using decimal notation,
with "dec" meaning "10"
0123456789
How to represent information - computer science
This is one of the first topics to discuss in computer science
Computers store and process information in binary at the most basic level
Friday, August 14, 2020
Waterfall Model
The waterfall model for software development was first proposed by Royce in 1970.
Agile software development methods are against some concepts proposed in the Waterfall Model.
Agile software development 1 - 2020aug14
On "requirements":
Bech (Extreme Programming):
Requirements gathering is in a phase that produces a static document, but an activity producing detail, just before it is needed, throughout development
Cohn (Scrum):
Scrum projects do not have an upfront analysis or design phase; all work occurs within the repeated cycle of sprints
Poppendieck (Lean):
And those things called requirements? They are really candidate solutions; separating requirements from implementation is just another form of handover
[source: edx course on Agile Software Development]
Tuesday, August 11, 2020
automate the process of compiling using clang with "make"
for example:
make hello
....make executes clang with various command-line arguments
execute the resulting executable:
make hello
specify executable file name for clang compiler
clang -o hello hello.c
-o is a "command-line argument", or a "flag" , or a "switch"
Commands in Unix are case sensitive
This means that a capital letter and a lower case letter are treated as different