computer programming


History of programming


BCPL (Basic Combined Programming Language) was developed by Martin RIchards in 1967.

In 1969 Language B was developed at Bell Labs by Ken Thomsan for the first Unix system.


Machines (DEC PDP-11) with ASCII processing became common at Bell Labs making character data in memory words important. The typeless nature of program B was seen as a disadvantage leading to C being developed by Dennis Ritchie in 1970. The extended version of the C language C++ was developed by Bjarne Stroustrup in 1982. C# or C Sharp was developed my Anders Hejlsberg (Microsoft) in 2000


C++ has significantly more libraries and functions to use compared to C.

C++ is an intermediate-level language, C# is a high-level language but C# warns users about compiler errors where C++ does not. Memory management in C++ is done manually by the programmer, in C# it is automatic since C# runs on a virtual machine.

Useful software

Programing source-code in C, C++, C#,


Software: Visual Studio (+VB)

Visual Studio alternative: Code Blocks | Tiny C Compiler

Online web alternative: Online GDB

Programing source-code in Python


To check if it's already installed open command prompt (cmd) and type: python --version

If you don't have it download it by visiting the Python homepage.

To open python terminal from Run. (type python)

More software:


Source Code Editor - Notepad ++

Android App Development - Android Studio


IDE - Integrated Development Environment

Slick Edit | Java - Blue Jay | Java - Eclipse

Counting in base 2 aka binary


Decimal or base10 is the number system we are all familiar with.

It contains 10 characters, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9


Binary or base2 is a different number system which contains only two characters 0 or 1.


Counting in Decimal:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 (you add a 1 to the left to increment up, or 2 for twenty)


Counting in Binary:

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000


If far right is a zero, it becomes a 1 and anything to the left carries down)

If the far right is a 1, it becomes a zero and we move one character to the left.


If it is a zero, it becomes a 1 and anything to the left carries down.

If it is a 1 it becomes a zero and we move one character to the left.


We can then assign binary o decimal number correlations:


D B

----------

0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101

converting an 8-bit binary number to base-10 decimal


The parts below are split into their columns (tens or hundreds column, etc.).


Decimal Column Decimal Binary Column


tens-millions 10000000 128

million 1000000 64

hundred-thousands 100000 32

ten-thousands 10000 16

thousands 1000 8

hundreds 100 4

tens 10 2

ones 1 1


To convert an 8-bit binary number such as 00110101.

Copy down any column number which has a 1 and add them up.


128 64 32 16 8 4 2 1

0 0 1 0 1 0 1 1

-----------------------------------------------------

0 0 32 0 8 0 2 1 = 43 (aka 0101011 in binary = 43 in decimal)


Converting binary to decimal


512 256 128 64 32 16 8 4 2 1


1x the place holder and add up all values = Decimal.

For instance A from ASCII is 0100 0001 in binary.


128 64 32 16 8 4 2 1 - place

0 1 0 0 0 0 0 1 - binary

_____________________ - times/x (the placeholder)

0 64 0 0 0 0 0 1 - sum


Add 64 + 1 = 65


converting decimal to binary


Can I subtract and end up with 0 or a positive number?


210

128 64 32 16 8 4 2 1 - place

can I subtract 128 from 210? y = 82 = 1

can I subtract 64 from 82? y = 18 = 1

can I subtract 32 from 18? n = 18 = 0

can I subtract 16 from 18? y = 2 = 1

can I subtract 8 from 2? n = 2 = 0

can I subtract 4 from 2? n = 2 = 0

can I subtract 2 from 2? y = 0 = 1

can I subtract 0 from 0? n = 0 = 0

if yes = 1 if no - 0

11010010 = 210


Hexadecimal is base 16

every hexadecimal value can always be converted to 4 binary bits.


Binary is a code of 8 bits or two sets of 4 bits so each hex value when converted to binary is two 4 bit values


10-11-12-13-14-15 - place

0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F

example: hex 1A5F

1 A 5 F

0001 1010 0101 1111 - binary

8421 8421 8421 8421 - place holder

1 10 5 15 - place value in hexadecimal


2 10 16

Binary Decimal Hexadecimal

0 0 0

1 1 1

10 2 2

11 3 3

100 4 4

101 5 5

110 6 6

111 7 7

1000 8 8

1001 9 9

1010 10 A

1011 11 B

1100 12 C

1101 13 D

1110 14 E

1111 15 F

10000 16 10

10001 17 11


8-bit code and ascii standard


2 10 16 Standard

Binary Decimal Hexadecimal ASCII

00000000 0 00 Null

00000001 1 01 Start of heading

00000010 2 02 Start Text

00000011 3 03 End Text

00000100 4 04 End of xmit

00000101 5 05 Enquiry

00000110 6 06 Acknowledge

00000111 7 07 Bell

00001000 8 08 Backspace

00001001 9 09 Horizontal Tab

00001010 10 0A Line feed

00001011 11 0B Vertical Tab

00001100 12 0C Form feed

00001101 13 0D Carriage feed

00001110 14 0E Shift out

00001111 15 0F Shift in

00010000 16 10 Data line Esc

00010001 17 11 Device Ctrl 1


Binary Decimal Hexadecimal ASCII

Additional examples below

00100000 32 20 space

00100001 33 21 !

00110000 48 30 0

00110001 59 31 1

01000000 64 40 @

01000001 65 41 A

01100001 97 61 a

01111010 122 7A z

01111111 127 7F DEL


For example: HI!

HI! = H + I + ! = 24 bits of data

H = 1001000 = 72

I = 1001001 = 73

! = 0010001 = 33


24 bits of data which represent 3 numbers which represent 3 letters.


This is Binary to ASCII, but ASCII is limited being stuck to 255 characters.


Unicode uses a table grid to map much more numbers and letters, instead of just being 8bit like ASCII.

Unicode uses 8bit (backwards compatible with ASCII),

16bit=(+65k combinations) or

32bit=(+4billion combinations).



Unicode Table Example: (% = 25 and H = 48)


0 1 2 3 4 5 6 7 8 9 A B C D E F

0000 nul soh stx etx eot enq ack bel bs ht lf vt ff cr so si

0010 del dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us

0020 sp ! " # $ % & ' ( ) * + , - . /

0030 0 1 2 3 4 5 6 7 8 9 : ; < = > ?

0040 @ A B C D E F G H I J K L M N O

0050 P Q R S T U V W X Y Z [ \ ] ^ _

0060 ` a b c d e f g h i j k l m n o

0070 p q r s t u v w x y z { | } ~ del



Another different data format: Color

RGB code are colors represented by amounts of color


Red 72

Green 73

Blue 33


24 bit color that equals a shade of yellow


Yet Another Format:


MIDI is 128 note format that is similar

128 67 64 is an example

programing TERMs


Functions are pre-defined group of calls, functions, and arguments that are labeled and then later able to be called as a function. Functions can be pre-defined/native to the code or user defined arguments added similar to a CSS or PHP call.


Functions

  • pick up

  • open to

  • look at

  • call

  • open to

  • quit


Conditionals

  • If

  • Else if

  • Else


Boolean Expressions (have yes or no answers)

  • person on page


Loops (repeats a function)

  • Go back to line 3


Arguments

  • Return Values

C programming


Visual Studio Code or VSCode is a popular version for C

filename.c is the format for C codes.

Integrated Development Environment (IDE) or a Text Editor


Introduction to programing source code in C:

  • To remove program: rm hello

  • To show all files: ls

  • more examples: cd, cp, ls, mkdir, mv, rm, rmdir ...

  • code mario.c (will create file mario.c)

  • cd ../.. (will return back two directories)

  • cd (alone will revert to parent root directory)


The basic start for programing in C is:


int main(void)

{


}


Example code:


#include <stdio.h>


int main(void)

{

printf("hello, world\n");

}


compiling source code in C to binary aka machine code


To save program/file hit control+s or file/save = "hello.c"


To compile the program: make hello

To run program in terminal: ./hello


*** if you type (./hello.c you will get an error as it's trying to run the file instead of the compiled code).


To remove program: rm hello

To show all files: ls

more examples: cd, cp, ls, mkdir, mv, rm, rmdir ...

code mario.c (will create file mario.c)

cd ../.. (will return back two directories)

cd (alone will revert to parent root directory)


Example code:


string answer = get_string("what's your name? ");


string answer = get_string("what's your name?\n");

(\n = new line, without it the prompt will appear after the returned value instead of the next line down).


int or integer = number output.

string = text output.


Loading additional libraries in C:


There is a standard IO (input/output library) for C, for any additional functions you'll need to add/load the library into the program.


For access to functions like "printf" you need to load #include <stdio.h> (standard IO.h)

For access to functions like "get string" you need to load #include <cs50.h>

A .h file is a header file which contains different functions that can be loaded into a program.

The header file is loaded as a library.


Example code:


#include <stdio.h>


int main(void)

{

string answer = get_string("what's your name? ");

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

}


#The below code is a different version of the same code#

#The difference is that the below code is harder to read and isn't able to be called by other functions in the program since answer has not been defined.


int main(void)

{

printf("hello, $s\n", get_string("What's your name? "));

}

Types of Functions / different types of data


%c is a placeholder that means (put a character here eventually)

%f is a placeholder that means (put a float here eventually)

%i is a placeholder that means (put an integer here eventually)

%li is a placeholder that means (put a long integer here eventually)

%s is a placeholder that means (put a string here eventually)



bool = boolean (true or false)

char = return single character 1

double = a number with additional info after the decimal 123.456

float = floating point number aka a number with a decimal point 123.

int = numbers 1234

long = bigger integer

string = words



Types of operators within C


* = multiplication

- = subtraction

+ = addition

/ = division

% = remainder

Programing source-code in C - counter


int counter = 0;

this means counter = 0


counter = counter + 1;

this means counter +1 = counter (it's right to left, date dumped into variable)


counter++;

this means plus 1

Programing source-code in C - calculator

to create a calculator:


$ code calculator.c


#include <cs50.h>

#include <stdio.h>


int main(void)

{

int x = get_int("x: ");

int y = get_int("y: ");

printf("%i\n", x +y);

}


$ make calculator


output: 1 + 1 = 2



#include <cs50.h>

#include <stdio.h>


int main(void)

{

int x = get_int("x: ");

int y = get_int("y: ");

int z = x + y;

printf("%i\n", z);

}


output: is the same but the integer can be called later for other functions if needed.


#include <cs50.h>

#include <stdio.h>


int main(void)

{

int first_number = get_int("x: ");

int second_number = get_int("y: ");

printf("%i\n", first_number +second_number);

}


output: is essentially the same:


To comment things out in C use // instead of <!--

For example //Prompt user for x


In the above calculator example there is an inherit limitation

of 4 billion bits or essentially 2 billion positive and 2 billion negative bits available for calculation

In the below example we use the long function to allow for 64 bits instead of 32 from int.



#include <cs50.h>

#include <stdio.h>


int main(void)

{

long x = get_int("x: ");

long y = get_int("y: ");

int z = x + y;

printf("%li\n", z);

}

Programing source-code in C - calculator cont.


#include <cs50.h>

#include <stdio.h>


int main(void)

{


if (x < y)

{

printf("x is less than y\n");

}

else if (x > y)

{

printf("x is greater that y\n");

}

else if (x == y)

{

printf("x is equal to y\n");


}


Below is the same answer but with 2 questions being asked instead

of the above with 3 questions to gain the same output.


#include <cs50.h>

#include <stdio.h>


int main(void)

{


if (x < y)

{

printf("x is less than y\n");

}

else if (x > y)

{

printf("x is greater that y\n");

}

else

{

printf("x is equal to y\n");


}

Programing source-code in C - conditional questions


#include <cs50.h>

#include <stdio.h>


int main(void)

{

int points = get_int("How many points did you lose?");

if (points < 2)

{

printf("you lost fewer points that me.\n");

}

else if (points > 2)

{

printf("you lost more points than me.\n");

}

else

{

printf("You lost the same amount of points as me.\n");


}


Save the program source code file:

Make the source code by compiling it: make points

run the compiled program: ./points


Below is the same code but instead of hard coding the comparative value to 2

we are entering a constant integer to ensure the value 2 never changes in the lower code

in the below example the added "const" int makes the integer constant and never change again.

Capitalizing is a format that programmers use to show a value is constant, capitalizing doesn't

alter the code but it's a nice visual.


#include <cs50.h>

#include <stdio.h>


int main(void)

{

const int MINE = 2;

int points = get_int("How many points did you lose?");

if (points < MINE)

{

printf("you lost fewer points that me.\n");

}

else if (points > MINE)

{

printf("you lost more points than me.\n");

}

else

{

printf("You lost the same amount of points as me.\n");


}

Even or program


#include <cs50.h>

#include <stdio.h>


int main(void)

{


// what is n

int n = get_int("n: ");

// % is remainder so 2 divided by n, (== is equals lol =), so 2 divided by 0 = 0 to show even vs odd.

if (n % 2 == 0)

{

printf("even\n");

}

else

{

printf("odd\n");

}

}

Do you agree (yes or no) program


In the below example, it allows the user to enter a lower or upper case answer of y/n)

&& is and single quotes (') are used for char, any string you would instead use (") double quotes.


#include <cs50.h>

#include <stdio.h>


int main(void)

{

// prompt user to agree

char c +get_char("Do you agree? ");

// check whether user agreed

if (c == 'y' || c == 'Y')

{

printf("Agreed.\n");

}

else if (c == 'n' || c == 'N')

{

printf("Not Agree.\n");

}

else

{

printf("Please choose y or n.\n");

}

}


programing loops in c boolean = while, true, false)

The below code repeats the word meow 3 times.

is correct but not standard programing format.


int counter = 0;

while (counter < 3)

{

printf("meow\n");

counter = counter + 1;

}



int counter = 0;

while (counter < 3)

{

printf("meow\n");

counter = counter + 1;

}


If an integers reason is to count, the standard format is to name it "i"

Below is the same code with counter replaced for i, 1= 1 +1; can also be written by i++;


int i = 0;

while (i < 3)

{

printf("meow\n");

i = i + 1;

}


in the below example the <= means less than or equal to.


int i = 0;

while (i <= 3)

{

printf("meow\n");

i++;

}


The below example also does the exact same thing as above.

The (void) section means that it does not take inputs


#include <stdio.h>


// add function meow to program


void meow(void)

{

printf("meow\n");

}


// now meow(); can be called in the program below)



int main(void)

{

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

{

meow();

}

}


You can add your function to the lower part of the program

by adding a call to the top of the program.


// call back at the top

void meow(void);


// function at the bottom


void meow(void)

{

printf("meow\n");

}


Below: inti = i, is i less than 3, then print, i++ = recheck


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

{

printf("meow\n");

}



PYTHON programing


Below are examples and the basics to getting familiar with Python.

To note: Python requires indentation


If python isn't enabled after install (open cmd and type:python --version, to check)

To enable Python after it has been installed open CMD prompt and type: set path=%path%;C:\Users\ServiceUser\AppData\Local\Programs\Python\Python37 then hit enter next type: python --version and hit enter again to confirm.

Change "ServiceUser" to your current username.


Program examples:


print("hello, world")


--------------------------------------------------------


answer int_string("what's your name? ")

print("hello, " + answer)


--------------------------------------------------------


answer int_string("what's your name? ")

print(f"hello, {answer}")


*The "f" = format or format string (this means anything in {} should be substituted by it's value.


--------------------------------------------------------


if x < y:

print("x is less than y")

else:

print)"x is not less than y")


--------------------------------------------------------


if x < y:

print("x is less than y")

elif x > y:

print("x is greater than y")

else:

print("x is equal to y")


--------------------------------------------------------

*meow forever


while True:

print("meow")


--------------------------------------------------------

*meow 3 times


i = 0

while i < 3:

print("meow")

i += 1


*or


for i in [0, 1, 2]:

print("hello, world")



--------------------------------------------------------

Python data types:


bool


float


int


str


range


list


tuple


dict


set


--------------------------------------------------------

*To import a library in python call it:


import cs50


*or call a partial libary:


from cs50 import get_float, get_int, get_string


COMPILING PYTHON


Python comes pre installed on most all computers.

When compiling instead of "making" the program, the computer just has to interpret it.

To do this - call the program "python" then enter it's file name:


python hello.py


This is because in C - the computer needs to compile the source code into binary prior to understanding it.

In python the source code can be ran since it is translated by the PC.


*To make a blurry image filter / call the PIL library:


Terminal: code blue.py (to create file)

Code:


from PIL import Image, ImageFilter


before = Image.open("bridge.bmp")

after = before.filter(ImageFilter.BoxBlur(1))

after.save("out.bmp")


* to run type filter/ python blur.py

the out.bmp will be the newly blurred version of the bridge image.


--------------------------------------------------------


*To make a spell check

*code dictionary.py (to create file)


words = set()


def check(word):

if word.lower() in words:

return True

else:

return False

def load(dictionary):

file = open(dictionary, "r")

for line in file:

word = line.rstrip()

words.add(word)

file.close()

return True

def size():

return len(words)


def unload():

return True


--------------------------------------------------------