Last active
May 11, 2022 06:04
-
-
Save croepha/f3d8bbb0af6946c93913b8c6efcb8d10 to your computer and use it in GitHub Desktop.
Croepha's guide to computer programming [VERY EARLY DRAFT EVERYTHING SUBJECT TO CHANGE]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TODO: Need to work on language so that ESL and Children would have easier time reading | |
Summary Paragraph: | |
This is a guide for people who have little or no computer programming | |
experience but aspire to create excellent and high quality software. | |
We will cover everything to get you started, including getting your | |
computer ready for programming and talking about some basic concepts. | |
Eventually we will work our way onto making quite sophisticated programs. | |
Although we use C++ as a language, we don't focus on a comprehensive set | |
of its features, instead we address programming concepts in general. | |
Although you don't have to be really good at math to be a good computer | |
programmer, before you start this tutorial you should at-least have a | |
basic understanding of algebra, as I expect you do know things like | |
what a negative number is, and how to solve for unknown values of | |
simple equations. You should also have some basic computer kills, and | |
be able to install software and change system settings on your computer. | |
<possible plug of a "algebra for computer programmers tutorial?"> | |
Chapter 0: Introduction | |
This is a tutorial for people who want to become highly skilled. This | |
is a guide for people who are concerned about getting the most reliability | |
and performance out of their applications, this is not a guide for people | |
who just want to make websites or make some quick scripts here and there. | |
Also this isn't for people who want to make an application as fast as | |
possible with out concern for quality. If you just want to make something | |
fast then there are other things for you. This guide alone won't get you | |
to the point of being a master programmer, but it will help you get started. | |
This tutorial assumes no prior-knowlege of computer programing however having | |
general computer skills and a basic understanding of Algebra would be good | |
to have. | |
TODO: Not everyone can become a programmer. Being good at computer programming | |
requires a strong passion. Good computer programmers are people who write a | |
ton of code, every day. They write code for work and they also write code on | |
their personal time. | |
A programmer's job is to take tasks that we think about, and convert into a | |
form that a computer can understand them. | |
To illustrate, I want you to think of an old calculator. | |
<insert image of calculator> | |
The calculator really only understands a specific number of instructions, like: | |
"insert a digit to the number" | |
"add this number" | |
"divide this number" | |
"clear" | |
etcetera | |
This is different from the way that people think, we generally think like: | |
"How many pounds of flour do I need to bake cookies for this weekend?" | |
"When do I need to get up tomorrow in order to not be late for work?" | |
When you use a calculator, you are essentially programming it with instructions. | |
Can you see the difference? Computers are actually really dumb | |
compared to people. The main advantage that computers have is that | |
they can reliably do large amounts of simple tasks very quickly. | |
The difference between the calculator instructions and the way that | |
people think is often referred to as Low Level and High Level respectively. | |
Computers can actually do high level tasks closer to how people think | |
about things, but that is after a programer has already done the work | |
of converting those high level concepts into low level instructions. | |
Later, we will talk more about high and low level, because eventually | |
we will be making our own high level code! | |
Here is another example: Bob and Alice are throwing a suprise | |
birthday party for Charlie. Bob needs to get some snacks from the | |
store and Alice needs to trick Charlie into coming to the party. | |
<TODO: This needs work, too tired to come up with a better analogy> | |
Here you can see, that another part of programming is sort of like | |
telling a story. You have to keep in your mind all of the elements | |
of the story and the proper sequence of events to get the desired | |
results. | |
In this tutorial we are going to be programming in C++ but we won't | |
really be diving into a lot of C++'s features, so in a way our code | |
will be more like C than C++. I don't fully expect for you do know | |
what C or C++ is or means at this point, but I want you keep that in | |
the back of your mind as the name of the language we are programming | |
in incase you want to do your own research on things later on. Also | |
what I mean by a computer "programming language" is just the | |
specification of how you write the instructions for the computer. | |
This is what our code will look like: | |
"HelloWorld.cpp" */ | |
#include <stdio.h> | |
int main() { | |
printf("Hello World\n"); | |
} | |
/* | |
This is called a "Hello World" program because all it does is output | |
the text "Hello World". This is a very small and basic computer | |
program that is usually one of the first programs that a computer | |
programmer will learn how to write. Its also a good bit of code to | |
start with just to make sure that the compiler is working right. | |
One thing I want to mention before we move on, is that other programmers | |
that you will meet will probably do things differently than how I | |
recommend doing them here, and that is absolutely fine! There are many | |
different tools and styles to chose from and I think that some point | |
you should take it upon yourself to try many of them. But this guide | |
assumes that you have installed and use the programs and methods that | |
I recommend in this guide, and I think that what I recommend here is a | |
good starting point. | |
You will want to know how best to talk to people in the community. | |
As part of following this tutorial you may run into situations where | |
you need help, or maybe you just want to share something clever you | |
came up with! <talk about forums and discord here> | |
(TODO: Is discord/irc an issue because its not always family friendly) | |
The next thing we need to do is get your computer setup to do programming! | |
This next step will be different depending on whether or not you are | |
running Windows, MacOS, or linux. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
GIST NOTE: | |
Below it talks about a tutorial bundle, that is this: | |
https://www.dropbox.com/s/y8fcfhkb2ition8/Chapter_001A_MSWIN.zip?dl=0 | |
Chapter 1A: Setup a Windows PC For Programming: | |
Download the text editor: Here I recommend GNU Emacs for windows. Grab it | |
by going to this URL: http://mirrors.syringanetworks.net/gnu/emacs/windows/ | |
and choosing the highest version number: | |
Download the compiler, "Clang" From here: http://releases.llvm.org/download.html | |
Again grab the highest version number and you want the Pre-built binaries for | |
Windows 64bit | |
Double click the LLVM exe file to install LLVM which include clang | |
- select "Add LLVM to the system PATH for all users" | |
https://www.visualstudio.com/vs/older-downloads/ make an account | |
Download and install Visual Studio Community 2013 with Update 5 | |
Install visual studio, feel free to unselect all of the optional | |
packages, we don't need them. | |
Extract the Emacs ZIP file that you downloaded, I recommend extracting | |
it into your C:\ Drive. Once you extract it, find bin\runemacs.exe | |
Right click runemacs.exe and click Send To > Desktop (Create Shortcut) | |
This will put an icon on your desktop, and we will use it a lot | |
In the tutorial bundle, find the MSWIN folder, and double click the | |
install.bat file. | |
Also I highly recommend going into folder options and: | |
Enabling Show hidden file, folders, and drives | |
Disabling Hide extensions for known file types | |
Now that we got your computer setup, lets compile our first program! | |
Now run emacs for the first time, using the shortcut on the desktop. | |
Click the Open/Create file button at the top | |
Navigate to a place on your computer that you want to store the files | |
for this program. Your "Documents" folder is generally a good place | |
for this. Then click the new folder icon at the top, and make a folder for this | |
project. "Chapter1" might be a good name. | |
Once you are in your new folder you need to enter in the names of | |
files we need to create. First create a file called build.bat by | |
entering "build.bat" into the file name box and hitting open. | |
Now emacs will show at the bottom that you have "build.bat" open. | |
There are two different sides of emacs, so that you can have two | |
things open at a time. So build.bat will be in one of the sides. | |
Build.bat should be completely blank because it's a new file. | |
Enter in the text: | |
clang++ -D_CRT_SECURE_NO_WARNINGS hello_world.cpp -o hello_world.exe | |
So, emacs will use build.bat to do the compiling. We will go into | |
further detail on what build.bat is, and what things we can do | |
with it, but for now, you should just note that we are compiling | |
"hello_world.cpp" and the output (our compiled program) will be | |
"hello_world.exe" | |
So "hello_world.cpp" doesn't exist, lets fix that. Use the | |
Open/Create File option we used earlier and it should already | |
have you in your project directory. Now enter "hello_world.cpp" | |
into the file name field and hit open. | |
Now Copy and paste the hello world source code from chapter 0 | |
into the "hello_world.cpp" file. Then hit the compile button. | |
If everything went well so far, you should see a green "finished" | |
with some text that popped up in one of your sides in emacs. | |
This new test is called your compilation buffer. We will talk | |
more about this later. | |
Note: If you did not see a green "finished" then something is wrong. | |
Please double check to make sure that you followed the directions | |
correctly. If you still cant figure it out, jump over to handmade | |
network and get some help :) | |
So we have compiled our program but we have no run it yet. To | |
run our program, we need to open up a console. In the top | |
menu bar of emacs, there should be a button that says | |
"Open Console". Click it, and a window command console | |
should pop up. In that console, type "hello_world.exe" and then | |
hit return. | |
There you go! You have now successfully compiled and run your first | |
program. Go to chapter 2 to learn more about the source code that | |
we just compiled | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In the last section I mentioned the concept of "compiling" your program, We | |
will go into more detail with what this means later, but for now I'll just | |
say that this just means that a program called a "compiler" is translating | |
the source code from text (what your text editor works with) into a binary | |
file that contains machine code (What your processor understands) | |
One importaint thing to know, is that when you are compiling, any errors that | |
are in your program will shown on one side of your editor. These errors will | |
have number in them that indicate what line the error is on. These errors will | |
have some sort of hint to help you fix the problem but sometimes they aren't | |
as helpful as we would like. If you ever want to know what an error means, | |
then you should ask a tutor to help you. | |
Now that you have all of your tools installed and you have successfully | |
compiled and run your first program lets look at the code. | |
For now we are going to ignore these parts: | |
*/ | |
#include <stdio.h> | |
int main() { ... } | |
/* | |
And we will just focus on what is between those curly brackets ({}). | |
Don't worry, we will cover everything we skip now in a lot of depth later. | |
*/ | |
// This line here will output the words: Hello World. You are actually | |
// Calling a function called "printf". We will talk about functions later | |
// but for now just understand that printf represents some code that you | |
// telling the computer to execute that exists elsewhere | |
printf("Hello World\n"); | |
/* The "\n" is a hidden bit of text that instructs the console(terminal) | |
to make a new line. If you aren't sure what this means, then try to | |
compile and run a program that doesn't have the "\n" and see what I | |
mean! The "\n" has a special name: The New Line Character :) | |
If your output looks jumbled up, then you might be missing some | |
New Line characters. | |
The one importaint thing to know at this point is that everything inside of | |
those curely brackets are instructions that the computer will execute in | |
sequence, starting at the top and working down. | |
Look at this program: | |
*/ | |
#include <stdio.h> | |
int main() { | |
printf("One "); | |
printf("Two\n"); | |
printf("Three "); | |
printf("Four\n"); | |
printf("Five "); | |
printf("Six\n"); | |
} | |
/* | |
Can you guess what the output will be without running it? | |
Go ahead and run it and find out if you were right! | |
NOTE: When running new code, feel free to overwrite your | |
old code if your don't care about it. For example your | |
Hello World code might not be that important so you | |
can just edit the code in place and recompile and rerun | |
it to get a new result. Eventually you will get to the | |
point where you are doing that all the time! You can also | |
of-course just make a new source and build file in a new | |
directory and that way your new code doesn't replace your | |
old code. You can also just append to your existing code. | |
PRO TIP: When you are using the console or terminal, you | |
can press the up arrow on your keyboard to bring up the | |
last command that you pressed, this helps out a lot when | |
you are running the same command over and over again :) | |
From now on, when you see code samples, you should run them | |
before moving on. And if you are confused about what | |
you are seeing, then you should ask for help, please don't | |
skip anythin. And remember, everyone has their own pace :) | |
When you ran the code did it do what you guessed it would do? | |
If you are confused about the outcome ask a tutor for help. | |
If you guessed differently, can you change the program so | |
that it does what you had expected it to do? | |
- - - | |
Alright, now we are going to try something new: | |
Try running this code: */ printf("Some number: %d\n", 123); /* | |
Neat huh? The "%d" is called a "conversion specification". | |
What this means is that %d tells printf that we want to convert | |
a number into text. More specifically we are converting an "int" | |
into text. An "int" is a specific type of number. It is actually | |
short for "integer". | |
Text "123" is different from the number 123 in a subtle but | |
important way. As far as the computer is concerned the text | |
is just the idea that there is "1" followed by a "2" then | |
followed by a "3" Where as the number is stored in a way | |
where the computer can actually do math on it. Down the | |
road we will talk about how exactly your computer works with | |
each type | |
And as you probably expected, we can also do math: */ | |
printf("Math! %d\n", 23 - 4); | |
printf("More Math: %d\n", 26 / 2 - 4 * 5); | |
/* | |
Because of the way that computers work an int can | |
have a lowest value of −2,147,483,648 and a highest | |
value of 2,147,483,647. Going outside of those limits | |
the value will actually wrap around. To illustrate what | |
I mean, check out this code: */ | |
printf("Largest int: %d\n", 2147483647); | |
printf("Largest int + 1: %d\n", 2147483647 + 1); | |
/* | |
This is called an "integer overflow." There are other types | |
of overflow that we will learn about later. Overflow is | |
generally considered something to avoid because it can | |
lead to your program doing things that you didn't expect. | |
But if you are expecting it then it can actually be | |
considered a useful feature in certain situations. Later | |
on we will talk about why there are min/max limits. | |
We can also store numbers in a way that we can refer to | |
them in multiple operations. Check out this code: */ | |
int my_number; // This declares the variable | |
my_number = 4; // This assigns a value to the variable | |
printf("1) My Number: %d\n", my_number); | |
my_number = my_number + 2; | |
printf("2) My Number: %d\n", my_number); | |
my_number = my_number / 2; | |
printf("3) My Number: %d\n", my_number); /* | |
Variable names can't have spaces in them, and they | |
can't start with number. They can only have numbers | |
and letters and the underscore (_). | |
We can also ask the user for input. Try the following code: */ | |
int number_from_user; | |
printf("Enter a number:"); | |
scanf("%d", &number_from_user); | |
printf("This is your number multiplied by 2: %d\n", number_from_user *2); /* | |
Here we introduce a new function, "scanf", this is the | |
opposite of printf, instead of producing output, it | |
lets us process some input from the user. Notice the | |
ampersand (&)? thats a special notation. its called | |
the "address of" operator. We will learn more about | |
that later, but for now just know that its useful | |
here because it allows another function to alter a | |
variable that we specify. Here is more complex example: */ | |
int num1, num2, num3; // Multiple variables of the same type can be declared at the same time | |
printf("Enter number 1:"); | |
scanf("%d", &num1); | |
printf("Enter number 2:"); | |
scanf("%d", &num2); | |
printf("Enter number 3:"); | |
scanf("%d", &num3); | |
printf("sum: %d\n", num1 + num2 + num3); | |
printf("product: %d\n", num1 * num2 * num3); /* | |
Feel free to play around with this. Can you come up | |
any cool things? can you automate some math problems? | |
Expirement if you want before moving on. | |
- - - | |
Remember when I said that the lines between the curly brackets | |
get executed in order? Well... I kinda lied (not going to be | |
the last time...) | |
Below are two new things, goto and labels. Labels are ways | |
to reference specific places in your code. goto is a special | |
thing that tells the computer to just jump to that place instead | |
of going to the next line as it would normally. | |
Check out this code: */ | |
goto middle; | |
top: | |
printf("line 1 !\n"); | |
goto bottom; | |
printf("line 2 !\n"); | |
middle: | |
printf("line 3 !\n"); | |
goto top; | |
printf("line 4 !\n"); | |
bottom: | |
printf("line 5 !\n"); /* | |
What do you think this code will do? | |
Try it out and see if you are right. Are you confused it? | |
if so then ask a tutor for help. | |
NOTE: When working with goto and labels, be careful not cause | |
endless loops, if you make one you can always kill your program | |
by pressing Ctrl+C in the terminal or console. | |
- - - | |
Alright now I want to show you something new. Run this: */ | |
printf(" 2 < 5 : %d \n", 2 < 5 ); | |
printf(" 5 == 5 : %d \n", 5 == 5 ); | |
printf(" 1 || 0 : %d \n", 1 || 0 ); | |
printf(" 0 && 0 : %d \n", 0 && 0 ); | |
/* Run the above code before moving on. | |
Neat huh? These are the relational operators, they compare | |
two numbers: | |
== != > < >= <= | |
These are the logical operators: | |
&& || ! | |
I want you to write some code to play around with these different | |
operators and I want you to figure out what each does. The (!) | |
operator is special in that it only deals with one value, whereas | |
all the other operators work with two values. Think of the (!) | |
as being similar to a (-) infront of a negative value. | |
Have you tried each operator out yet? | |
When playing with the relational operators, make sure that you | |
try cases where the values on each side are the same, and also | |
try different values, and also see what happens when you swap | |
sides. | |
When playing with the logical operators make sure that you try | |
zero with each operator. | |
Have you figured out what each operator does? | |
While you are playing with the code I want you to try this out: */ | |
int v1, v2; | |
printf("Enter two numbers (press the enter key after each number):\n"); | |
scanf("%d", &v1); | |
scanf("%d", &v2); | |
printf(" %d < %d evaluates to: %d \n", v1, v2, v1 < v2 ); | |
printf(" %d < %d evaluates to: %d \n", v2, v1, v2 < v1 ); | |
printf(" %d > %d evaluates to: %d \n", v1, v2, v1 > v2 ); | |
printf(" %d > %d evaluates to: %d \n", v2, v1, v2 > v1 ); | |
printf(" %d == %d evaluates to: %d \n", v1, v2, v1 == v2 ); | |
printf(" %d == %d evaluates to: %d \n", v2, v1, v2 == v1 ); | |
printf(" %d != %d evaluates to: %d \n", v1, v2, v1 != v2 ); | |
printf(" %d != %d evaluates to: %d \n", v2, v1, v2 != v1 ); | |
printf(" %d <= %d evaluates to: %d \n", v1, v2, v1 <= v2 ); | |
printf(" %d <= %d evaluates to: %d \n", v2, v1, v2 <= v1 ); | |
printf(" %d >= %d evaluates to: %d \n", v1, v2, v1 >= v2 ); | |
printf(" %d >= %d evaluates to: %d \n", v2, v1, v2 >= v1 ); | |
printf(" %d && %d evaluates to: %d \n", v1, v2, v1 && v2 ); | |
printf(" %d && %d evaluates to: %d \n", v2, v1, v2 && v1 ); | |
printf(" %d || %d evaluates to: %d \n", v1, v2, v1 || v2 ); | |
printf(" %d || %d evaluates to: %d \n", v2, v1, v2 || v1 ); | |
printf(" !%d evaluates to: %d \n", v1, !v1); | |
printf(" !%d evaluates to: %d \n", v2, !v2); | |
/* | |
Alright once you feel that you have played around enough to know | |
what each operator does, take a look at the following table: | |
Operator Test description | |
== equality | |
!= inequality | |
> greater than | |
< less than | |
>= greater than or equal to | |
<= less than or equal to | |
&& logical "and", are both sides are non-zero? | |
|| logical "or", are one of the sides non-zero? | |
! logical "not", is the value to the right zero? | |
If the test is true, then the result is a 1 else it is a 0. | |
In programming we generally talk about this as true meaning | |
non-zero, and false is always zero. A negative number is | |
always true. Programmers sometimes call statements that deal | |
with true or false values: Boolean. | |
/* | |
Here is another new thing: if. "if" tells the computer to | |
make decisions. It allows you to run code /if/ a condition | |
is met. Look at this code: */ | |
if (5 > 3) printf("5 is bigger"); /* | |
Can you write a program that takes two numbers and prints out | |
which one is bigger? | |
Go ahead and try it. If you get stuck remember you can ask for help. | |
Once you have tired to write the program proceed: | |
This was my solution to the problem: */ | |
int num1, num2; | |
printf("Enter two numbers (press the enter key after each number):\n"); | |
scanf("%d", &num1); | |
scanf("%d", &num2); | |
if (num1 > num2) printf("%d is bigger\n", num1); | |
if (num2 > num1) printf("%d is bigger\n", num2); /* | |
If your solution is different then thats good. There are many | |
ways to write code that does the same thing. | |
- - - | |
Alright, now it is time for a chalenege: | |
I want you to use the tools that we learned in this chapter to | |
write a program that asks for a number and then prints out | |
every number between 1 and that input number and you are | |
allowed to only have one line of code that has "printf" in it. | |
Dont scroll down until you have attempted it. | |
If you are having trouble, here is a hint: | |
You can have a goto at the end of your if statement | |
If you have attempted it but still cant get it to | |
work right, then find a tutor to help you out. Make | |
sure that you have some code that you have made and | |
share it with your tutor so that they can look at | |
it and see if it has any issues. | |
Dont scroll down until you have completed the assignement. | |
Second challenge: | |
I want you to make a simple guessing game. I want | |
you to come up with a secret number and put it | |
into your code and then your code will ask the | |
player to make a guess as to what that number is. | |
Your problem must tell them if they got it right | |
or if their number was too low or too high. | |
Please attempt the challenge now. | |
If you are having trouble please ask a tutor for help. | |
Dont scroll down until you have completed the assignement. | |
So I want to show you a few new things before we | |
wrap up this lesson. | |
For the first chalenge above this was my solution: | |
*/ | |
int counter, end; | |
printf("Enter number to count to:\n"); | |
scanf("%d", &end); | |
i = 1; | |
start: | |
printf("Number: %d\n", i); | |
i=i+1; | |
if (i<end+1) goto start; | |
/* | |
If your solution looks different then that is fine :) | |
I want to show you a little trick, run this: */ | |
int i, end; | |
printf("Enter number to count to:\n"); | |
for (i=1; i<end+1; i=i+1) printf("Number: %d\n", i); | |
/* | |
Neat huh :) | |
Also You can use curly braces to group code together. For example: */ | |
int i, end; | |
printf("Enter number to count to:\n"); | |
scanf("%d", &end); | |
for (i=1; i<end+1; i=i+1) { | |
printf("Number: %d\n", i); | |
printf("Number again: %d\n", i); | |
} | |
/* Run that. | |
- - - | |
This was my solution second challenge: */ | |
int secret_number, guess; | |
secret_number=42; | |
start: | |
printf("Enter your guess:\n"); | |
scanf("%d", &guess); | |
if (guess==secret_number) goto end; | |
if (guess> secret_number) printf("Your number is too large\n"); | |
if (guess< secret_number) printf("Your number is loo low\n"); | |
goto start; | |
end: | |
printf("You guessed correctly!\n"); | |
/* | |
Check this out: */ | |
int secret_number, guess; | |
secret_number=42; | |
printf("Enter your guess:\n"); | |
while(guess!=secret_number) { | |
scanf("%d", &guess); | |
if (guess> secret_number) printf("Your number is too large\n"); | |
if (guess< secret_number) printf("Your number is loo low\n"); | |
} | |
printf("You guessed correctly!\n"); | |
/* | |
These are called for and while loops, they are just ways to | |
do the same thing as goto, but they are a little easier to | |
read. Most people discourage the use of goto and prefer | |
the use of these loop constructs. | |
Also one more thing, there is a special code called "break". | |
If you put break; into one of those loops, it will stop the | |
loop from running. So you could have done this instead: */ | |
while(1) { | |
if (guess> secret_number) printf("Your number is too large\n"); | |
if (guess< secret_number) printf("Your number is loo low\n"); | |
if (guess==secret_number) break; | |
} | |
/* | |
Now you have learned the basics of computer programming! | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Computers do math a little bit differently then you and I would. | |
People use numbers that are called "Base ten" what this means is | |
that each digit can store the value of zero through nine (ten different | |
values) Base ten numbering evolved naturally for people because | |
we have ten fingers and it is easy to count on your fingers so | |
thinking of things in terms of tens is natural. | |
For computers the natural number system is called "Binary" or | |
"base 2" Computer's use base 2 because storing something as either | |
a one or a zero is the more reliable way for computers store | |
information. If a computer tried to store any value in-between | |
that value would drift over time. | |
One digit in binariy is called a "bit". Bits are usually put | |
together in groups of eight called bytes. Ony byte can store | |
any value from 0 to 255. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment