Saturday, November 7, 2020

two-dimentional array

 #include <stdio.h>

#include <cs50.h>


int main(void){

string names[4]; 

names[0]="EMMA";

names[1]="RODRIGO";

names[2]="BRIAN";

names[3]="DAVID";

printf("%s\n", names[0]);

printf("%c%c%c%c", names[0][0], names[0][1], names[0][2], names[0][3]);

}




source: harvard, cs50



names.c

 #include <stdio.h>

#include <cs50.h>


int main(void){

string names[4]; 

names[0]="EMMA";

names[1]="RODRIGO";

names[2]="BRIAN";

names[3]="DAVID";

printf("%s\n", names[0]);

}




source: harvard, cs50



Tuesday, September 15, 2020

In C - arrays do not remember their own length

 People who program in Java or Python might not understand this at first.


you do not get that feature for free in C.


cs50, lec2


Saturday, September 12, 2020

scores.c --- more advanced version

#include <cs50.h>

 #include <stdio.h>


float average(int length, int array[]);


int main(void)

{

int n=get_int("number of scores:");

int scores[n];

for(int i=0; i<n:i++)

{

scores[i] = get_int("Score %i:", i+1);

}

printf("Average: %.1f\n", average(n, scores))

}


float average(int length, int array[])

{

int sum=0;

for(int i=0;i<length;i++)

{

sum += array[i];

}

return (float) sum / (float) length

}


cs50, lec2


in C, divide float by float and you will get a float

 if you have two integers that you want to divide, but do not want everything after the decimal point to be thrown away, you can type cast so that the computer treats the integers as floats, thereby ending up with more precise results

const int N;

int sum;

average = (float) sum / (float) N;



cs50, lec2



in C, if you divide an integer by an integer, you will get an integer (everything after decimal point is truncated)

 cs50, lec2



global variables are often frowned upon, except for consts

 placed outside of main()

const

and variable name capitalized

....... that is the convention


This is so you wont have to go visually fishing for a certain number value throughout your code int the future.



const variable - do not have hard coded numbers in more than one place (hard to maintain, often source of bugs)

 #include <stdio.h>


const int N = 3;    //make scope global; capital denotes constant


int main(void)

{

int scores[3];

scores[0]=72;

scores[1]=73;

scores[2]=33;


printf("average: i%\n", (scores[0]+scores[1]+scores[2])/N );

}



cs50, lec2



Thursday, September 10, 2020

scores.c -- Arrays version

#include <stdio.h>

#include <cs50.h>

 int main(void)

{

int scores[3];

scores[0]=72;

scores[1]=73;

scores[2]=33;


printf("average: %i\n", (scores[0] + scores[1] + scores[2])/3 );

}



cs50, lec2



scores.c

#include <stdio.h>

#include <cs50.h>


int main(void)

{

int score1= 72;

int score2= 73;

int score3=33;


printf("average: %i\n", (score1+score2+score3)/3);

}




 cs50, lec2

Wednesday, September 9, 2020

type casting : converting one type to another

 refer to hi.c in the previous post


type casting: convert characters to the ASCII number value that represents those characters


#include <stdio.h>


int main(void)

{

char c1='H';

char c2='i';

char c3='!';


printf("i% i% i%\n", (int) c1, (int) c2, (int) c3);

}



in reality, clang is actually smart enough that even if you don't explicitly cast the chars to ints, it will know that the casting needs to occur and implicitly cast the chars to ints automatically


#include <stdio.h>


int main(void)

{

char c1='H';

char c2='i';

char c3='!';


printf("i% i% i%\n", (int) c1, (int) c2, (int) c3);

}



source: cs50, lec2


hi.c

 #include <stdio.h>


int main(void)

{

char c1='H';

char c2='i';

char c3='!';


printf("%c %c %c\n", c1, c2, c3);

}



source: cs50, lec2



single quotes for char ; double quotes for string ......... C requires this

 char c1 = 'a';

string s1 = "apple";



Sunday, September 6, 2020

bool, char, int, float, long, double, .... = data types in C

bool      1 byte
char      1 byte
int         4 bytes
float      4 bytes                    (in most systems these days, these are the amount of space these data
long      8 bytes                      types are most likely to take up)
double  8 bytes
string    ? bytes
....
....


cs50, lec2


design decisions - not only get problems solved, but solved well

when at facebook , google, microsoft ......

when huge amounts of data and users

design decisions could result in significant cost, cpu cycles, or memory .......



cs50, lec2



reasonable people may disagree on: (1) style (2) design

what style is better, more readable, more maintainable


what design is better




cs50, lec2

when cannot solve problem - go for jog, or rubber duck, or nap

many people have the experience of

when cannot solve problem in programming when using debugger etc.

go for jog

change mental mindset

many have the experience of solving problem when taking nap

or talk through your code with a rubber duck



cs50, lec2




help50, printf, debug50, check50, style50

help50 ........... correctness
printf ............. correctness
debug50......... correctness
check50 ......... correctness
style50 ........... style




cs50, lec2



style50 ........ important ........ code maintainability

nothing worse than being unable to understand what you are looking at when you come back after a while to maintain your code


cs50, lec2


check50 is representative of the real world process of testing one's code repeatedly

cs50, lec2, around 29:00 mins 處 ....... how to use the check50 test


check50 checks whether your program does what is required,

but it is actually a "test"




Using a debugger is a very important life long skill

not just for C but for other languages as well

cs50, david malan


CTRL + C ...... this jumps out of programs like the cs50 ide debugger

CTRL + C ........ jumps out out of the window, kills the debugger, and goes back to the code and terminal window view


"C" stands for cancel



Saturday, September 5, 2020

debugger - allows to step through code step by step

there is a debugger included in the cs50 ide but not in the cs50 sandbox


cs50 IDE (ide.cs50.io) - Eclipse, Visual Studio, Netbeans, and more ....

these are all "integrated development environments"

cs50 IDE: ide.cs50.io


cs50, lec2, 2019


canonical -meaning

conforming to a general rule or acceptable procedure: orthodox


source: google search (merriam-webster.com)


debugging tool: printf() - use to poke around and see what is going on inside your computer


printf() is a useful tool for debugging




for example:

#include <stdio.h>

int main(void)
{

for(int i =0; i<=10;i++)
{
printf("i is now %i\n", i);  //debug line
printf("#\n");
}

}


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



Sunday, June 21, 2020

Record Screen on Mac Computer

To record everything that happens on your Mac computer (such as MacBook) screen,

press "command+shift+5" .

Remember, if you also want to record audio,

be sure to go to "options" and select "built-in microphone" .


Friday, March 6, 2020

Numbers, ASCII, Unicode (UTF8, UTF16), Big5, RGB

Ways of representing information in computers:

Numbers

ASCII
Unicode ( UTF8, UTF16, [emoji] )
Big5

RGB


Thursday, March 5, 2020

ASCII

ASCII:

American Standard Code for Information Interchange


An N95 Face Mask made by 3M

This is an N95 face mask made by 3M:



"Bit" Means "Binary Digit"

In computer science, a unit of storage is called a "bit," which stands for "binary digit."

Eight bits are called a "byte."


Monday, March 2, 2020

“!” (“Bang”) in Software Programming

The exclamation mark “!” is used to “invert” things in programming languages such as C.

For example, “if x=3” means “if x equals 3”;

                      “if x!=3” means “if x does not equal 3”


Saturday, February 29, 2020

Zi Nan Temple (紫南宮)


Zi Nan Temple in Nantou County is a very popular tourist destination in Taiwan. It is said that people who borrow money from the Land God there will become very prosperous, which is why so many people go there to worship the Land God and borrow money. It is required that people who borrow money from the Land God pay back the borrowed money within a year. Since many people become prosperous or make a lot of money after they borrow money from the Land God, many people choose to pay back more money than they originally borrowed. 

Below is a picture taken from outside the front of Zi Nan Temple:



The Land God and Land Goddess, with other gods by their side:




People who with to borrow money from the Land God must first obtain his permission by throwing a pair of crescent-moon-shaped wood pieces onto the floor. If you get one wood piece side-up, and the other side-down, that means the Land God has agreed to lend you money. You get six tries per visit. If you cannot succeed within six attempts, then you have to come some other time to try again. If you succeed on your first throw, then you have been granted 600 NT dollars by the Land god; if you succeed on your second throw, then you have been granted 500 NT dollars by the Land God; if you succeed on your third throw, then you have been granted 400 NT dollars by the Land God; and so on and so forth....... Before you throw the wood pieces onto the floor, you need to respectfully tell the Land God your name and ask his permission to borrow money:





Once you are granted money by the Land God, go here to get your borrowed money:




One year later, come here to pay back what you borrowed (don't forget to add some additional money if you prospered because of the Land God's blessings):




Touch and pat the golden hen for good luck and good fortune:




Links: