P T H I N K F A ST S
Competitive Programming From Problem 2 Solution in O(1)
Introduction to Programming
Mostafa Saad Ibrahim
Teaching Assistant @ Cairo University
The big problem…
Students
grow up = can’t code
medium thinking problem
Students
grow up = can’t code
medium project
Problem: Much knowledge + Little
Exercises
What is in? What is out?
This
series is oriented for
competitions
Itcovers only the basics needed for
them
Series = Basic knowledge + Much
Exercises
For competitions guys only? No
Computer Program?
Read Number 1 from user Computer + CPU
Read Number 2 from user
Sum Them
If even Say YES, EVEN
Else Say So Bad
Computations +
Logical Decisions
Computer Program = Set of instructions
mage src: http://www.bbc.co.uk/schools/gcsebitesize/ict/images/processor.jpg
Programming Language
A way to talk to the computer
(source code)
Many ones: C++, Java, C#...
Each language has pros and cons
C++ is a very popular start for
programming
Learning Language
States
Remember, new human language takes tim
Programming Language is same
Struggle <= 2 months…then you admire it
Image sources in notes section
Writing a Computer
Program
Pick a language
WriteSource Code (text) that
follows language
Usecompiler of language to convert
code to what computer understands
Getan exe file..double click it (for
windows)
Source Code Life Cycle
To Learn more details about cycle: http://www.tenouk.com/ModuleW.html
Img Src: http://commons.wikimedia.org/wiki/File:Program_life_cycle_IPL.png
Our first Computer
Program
Write a program that displays to user
My First Program. Hellllllo.
Use C++ Language
C++ Source Code
Example
Compile and Run Exe
IDE: Integrated development
environment
We could write the code in a notepad
file, then go to compiler to compile and
generate exe to run
Whatabout something gathers all in
simple way?
IDE = tools integrate staff for u
Many ones: CodeBlocks, Eclipse, C++Dev
CodeBlocks IDE
Eclipse IDE
Which IDE?
CodeBlocks is an easier start
Eclipse
is more professional and
many companies use it
Need little effort to start with it
I suggest it more
Note:Good programmer knows how
to compile through command.
Summing 2 numbers Example
Powering 2 numbers Example
#include ??
Let
say you want to find square root of 25,
which is 5
One day you will write code to do it. But in
real life, why let you write it from scratch!
There are some libraries ready for use to save
your time. This is the #include part
#include <iostream>
Allows you to write to console
cout<<“Competitions";
▪ This Display statement competition
cout<<“\n";
▪ This Display an empty line
It also allows you to read from user
cin>>first_number
▪ Reads number from user of the program
#include <Cmath>
This is where many math functions implemented
for you
sqrt get square root
pow gets power of 2 numbers
sqrt and pow are called functions
and many other ones
Int main()
Torun your code, Computer needs to
know where is your code start
It searches for main()
Int refers to status of finishing.
Return 0; means code finished with
no run time mistakes
int first_number = 5;
This line do following
Reserve memory blocks
Name it first_number
Set its value to 5
Int
means integer. This is the type of
memory reserved.
;=> semicolon end of any
statement in C++
Don’t worry
This
is the first time to see source
code
Most of it will be weird now
Themore you see code…the more
you get it
The magic fact: Write =
Read
You
could write Programs as much as
what you read
Want to write long code? Read long
ones
Wantto write smart code? Read
smart ones
You must read too much=> to write