Monday, August 31, 2020

command for using "help50" to help you debug errors

help50 make buggy1

cs50, lec2


linking - combining 0s and 1s from our source code and the various libraries

links the various 0s and 1s into an overall program in machine code


assembling - taking assembly code and converting to 0s and 1s

this portion is also done by the clang compiler


compiling - although this is often used as a generalized and simplified term, it actually has a more precise definition

compiling is taking your source code (after being pre-processed) and converting it into assembly code


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

in this step, the pre-processor goes through your code, and copy pastes relevant code from .h header files into your code

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:

pre-processing
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 <cs50.h>
#include <stdio.h>

int main(void)
{
string s = get_string("what is your name?\n");
printf("hi, %s\n", s);
}


cs50, lec2 (reviewing lec1)


stdio.h ....... manifestation of a library, a header file

cs50, lec2

Sunday, August 30, 2020

say - command (program) that comes with some systems that speaks out what you type

for example:

say this is cs50

(this works on mac)


figlet - command (program) that comes with some systems which produces ASCII art

for example:

figlet this is cs50


ACSII art

ACSII art


floating point imprecision & integer overflow

both exist because computers only use a finite amount of bits to store numbers


sleep(1) - wait for i second

to use this handy function, you must :

#include <unistd.h>


RAM - where programs are stored while they are running, and where files are stored while they are open

[source: harvard, cs50]


positive.c

#include <cs50.h>
#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

hence the need for function prototypes ("one liners")

(C is dumb in that it does not scan further down to look for function definitions)


function prototypes : C is dumb

#include <cs50.h>
#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 <cs50.h>
#include <stdio.h>

int main(void)
{

void cough(void)
{
printf("cough\n");
}

for (int i=0; i<3; i++)
{
cough();
}

}


agree.c

#include <cs50.h>
#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 <cs50.h>
#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

"Parity" is just a fancy way to saying "is a value even or odd"

[Source: harvard, cs50]


float.c : get a floating point number from the user

#include <cs50.h>
#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 <cs50.h>
#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]


place holders: %s, %c, %f, %i, %li

%s     string

%c     char

%f      float, double

%i      int

%li     long



get_string, get_char, get_int, get_long, get_float, get_double, ...

the cs50 library provides these functions

these are all functions that will prompt the human user for certain values


Data types in C: bool, char, double, float, int, long, string

bool  (true or false)
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++ ) { }

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]



Repeat 50 times: while ( i < 50 ) { }

int i=0;

while (i<50)
{

printf("hello,world\n");

i++;

}


while(true) { } = "forever loop"

this is the same as saying:

while (2==2)
{

}


Thursday, August 27, 2020

if, else if, else

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");
}

"==" equality operator ; "=" assignment operator

C programming

if, else if, else if

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");
}


C: no semicolons after condition statements (semicolons after some kind of "action")

Source: Harvard , CS50


if else

if (x<y)
{
printf("x is less than y \n");
}
else
{
printf("x is not less than y \n");
}

Condition:

if (x<y)
{
printf("x is less than y\n");
}


Increment by 1 : three forms

counter = counter +1;

counter += 1;

counter++;


counter++;

increment by 1

counter += 1;

increment by 1


counter = counter + 1 ......... "=" assignment operator

assignment operator "="

make - included in Linux, MacOS, and Windows these days

the make program is now included with these operating systems

make string

when you type this command, the program "make" will look for a file called "string.c" to compile into the executable "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

in addition to " #include <cs50.h> "

you also have to "-lcs50"

where "l" stands for  "link"



[Source: Harvard, CS50]


Monday, August 24, 2020

String.c

#include <stdio.h>
#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



the printf function taking 2 arguments: printf("hello, %s\n", name);

 source: harvard, CS50



%s -- "place holder" for printf in C

 printf("hello, %s\n", name);


" = " is the "assignment operator" in C

 for example:

String name = get_sting("What's your name?\n")



String name = get_string("What's your name?\n")

 source: Harvard, CS50



get_string("What's your name?\n");

 ask user to enter their name


source: Harvard, CS50

Saturday, August 22, 2020

The * behind a file name denotes that that file is an "executable"

Which just means that that file contains machine code that can be run by the computer.


"ls" stands for "list"

 This command lists all of the files in the directory.



"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"

 The "." means "current folder" .

This tells the computer to run the program a.out



"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).

C - Hello World

 #include <stdio.h>


int main(void)

{

printf("Hello world\n");

}



Friday, August 21, 2020

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

Sunday, August 16, 2020

Pixel = one colored dot on the screen

By combing a number of pixels, any image can be displayed.



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]



Emoji are included in Unicode

 Emoji are represented using Unicode.



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

Transistors store 0 or 1

Having a charge or not having a charge

Inside a computer 



"bit" = "binary digit"

 a bit is a 0 or 1 in a computer or other digital system



Pedagogical - of or related to teaching

 source: Google search results (Oxford Languages)

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"





run a program from the command line in Unix

 ./a.out



clang - C compiler

 clang stands for "C language"


for example:

clang hello.c



ls command in Unix

 ls stands for "list"



hello world program in C

 #include <stdio.h>

int main(void)

{

printf("hello, world\n");

}



standard i/o library in C

 stdio.h


#include <stdio.h>



"cd" Unix command

 cd stands for "change directory"

an example would be:

cd ~/dir1



"mkdir" Unix command

 mkdir stands for "make directory"

an example would be: 

mkdir ~/directory1



Commands in Unix are case sensitive

 This means that a capital letter and a lower case letter are treated as different



"~" in Unix command Line

 ~  represents the home directory in Unix command line