An Introduction to Programming
through C++
Abhiram G. Ranade
Reading: Ch. 2: A bird’s eye view
A basic question
• How is a computer able to do so many things?
– Search for information
– Predict weather
– Process pictures and say what is in them
– Play chess..
• Goal of this lecture sequence: provide high level answers
– Most real life problems can be viewed as mathematical problems on
numbers
– A computer is good at solving math problems
• High level answers will give good background for later
discussion.
Outline
• How to express real life problems as numerical problems.
– Picture processing
– Predicting the weather
– Processing text/language
• Algorithms and Programs
– Enable us to tell a computer what operations to perform
• How a computer does the required operations
– Digital circuits
– How numbers are represented
– Parts of a computer
– Machine language program, compilation.
“What is in this picture?”
[Link]
How to represent black and white pictures using
numbers
• Suppose picture is 10cm x 10cm.
• Break it up into 0.1 mm x 0.1 mm squares
• 1000 x 1000 squares. 1 million “pixel”s
• If square is mostly white, represent it as 0.
• If square is mostly black, represent it as 1.
• Picture = 1 million numbers!
Picture, Representation, Reconstruction
0 0 0 1 1 1 0 0 0 0
0 0 1 0 0 0 1 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1 0
1 0 1 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 1 1 1 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 1 1 0 0
0 0 0 1 1 1 0 0 0 0
(a) (b) (c)
Remarks
• Better representation if picture divided into more cells.
• Pictures with different “gray levels”: use numbers 0,
0.1, …, 1.0 to represent level of darkness rather than
just 0, 1.
• Pictures with colours: picture = 3 sequences
– sequence for red component,
– sequence for blue component,
– sequence for green component
• Add up the colours to get the actual colour.
Computer vision/Image processing
Input: sequence P of 1 million numbers (0 or 1)
• Representing a 10cm x 10cm black and white picture,
• Given in left to right, top to bottom order.
A very simple image processing problem:
• Is there a vertical line in the picture?
Expressing the problem mathematically: What property does the sequence need
to have if it is to contain a vertical line?
• All 0s, except for 1s in consecutive rows of some column
• Going down a column = move 1000 positions in the sequence
• 1s in positions i, i+1000, i+2000, i+3000, i+4000,… for some i
• ”Is there a vertical line?” = “Does sequence P satisfy above property?”
One way of solving the problem:
• Try all values of i..
Does the picture contain a chameleon?
• In principle, same as asking whether the picture
contains a single vertical line:
– Identify a set of properties that the sequence of numbers
representing the picture must satisfy if the picture
contains a chameleon.
– Identify computations that can check if the given number
sequence satisfies the required properties.
• In practice requires enormous ingenuity
• Main concern of the deep subject “Computer Vision”
Weather prediction
• Divide the surface of the earth into small regions (like
pixels).
• Let pi, ti, hi = pressure, temperature, humidity in region i
• Laws of physics tell us how to the values will change with
time.
• We can measure current pressure, humidity, temperature
values, and calculate what will happen tomorrow!
• Smaller the regions, better will be the accuracy. (Smaller
the pixels, better will be the picture representation).
Language/text using numbers
• Define a code for representing letters.
• Commonly used code: ASCII
– (American Standard Code for Information Interchange)
• Letter ‘a’ = 97 in ASCII, ‘b’ = 98, …
• Uppercase letters, symbols, digits also have codes. Code also for space
character.
• Words = sequences of ASCII codes of letters in the word.
• ‘computer’ = 99, 111,109,112,117,116,101,114.
• Sentences/paragraphs = larger sequences.
• Does the word “computer” occur in a paragraph?
– Does a certain sequence of numbers occur inside another sequence of numbers?
Exercises
• What pattern of 1s and 0s would correspond to a ”+” of any
size being present at the center of an otherwise white picture?
• Suppose you are given a sentence in a language you cannot
understand. Would you still be able to count the number of
words in the sentence? Can you express this as a question on
sequences of numbers representing the ASCII codes of
different characters?
• How will you represent Chess playing as a question on
numbers? Start by representing a chess board with pieces on
it using numbers.
Summary of what we discussed
• Questions about pictures, weather, documents
can be converted to questions about properties of
number sequences.
• Finding answers requires solving interesting math
problems.
🐿
A historical remark
• Computers are used to solve problems, but problem solving is not
new, humans have been solving numerical problems for millenia
– Astronomical calculations for navigation
– Land records/geometry
– Poetry raised some interesting computational problems and some were
solved very elegantly by ancient Indian mathematicians
– Last 500 years: development of differential equations to analyze structures,
weather, ...
• “Solving problems” = deciding what operations to perform to
calculate the required answer
• Algorithms = A precise description of the operations needed to solve
a problem.
You already know many algorithms!
• The procedures you have learnt in primary school for arithmetic on numbers
with many digits are really algorithms.
• Primary school algorithms contain all ingredients that are present in
advanced algorithms
– Arithmetic operations: “Add the least significant digit of the first number to the
least significant digit of the second number”
– Conditional operation: “Set carry = 1 if the previous sum was greater than 9”
– Repetition: “Repeat as many times as there are digits”
• Other algorithms you know
– determine whether a number is prime,
– finding the greatest common divisor
– Drawing a polygon on the screen
• All these algorithms will be useful on computers!
Programs
• Algorithms written in precise syntax/language.
• C++ is one such language.
• Other languages: Fortran, Basic, Lisp, …
• All these languages can be used to specify
arithmetic operations, conditional execution, and
repetition.
– You have already seen how to specify repetition – use
the repeat statement!
Exercise
• Write down the algorithm for multiplying an n digit
number by another n digit number that you learned in
primary school.
– Break up the description into parts, e.g. in the first part we
do multiplications and in the second we do additions.
– To describe what operations you perform, it might be useful
to give names e.g. Let Qi, Ri denote the ith least significant
digits of the multiplicand and the multiplier.
– Are there phases in the algorithm in which you do similar
operations? If yes can you say what happens in “ith phase”?
What we discussed
• Notion of problem solving, algorithms is old
• An algorithm specifies a sequence of operations
– May contain arithmetic operations
– May contain operations to be executed conditionally
– May ask for sequence of operations to be repeated
• You already know many nice algorithms
– Arithmetic on numbers, matrices, root finding...
– Very useful for programming
• Programs are formal descriptions of algorithms, in specific
languages.
🐿
How a computer solves (numerical) problems
Outline
• Digital circuits
• How numbers are represented
• Parts of a computer
• Machine language program, compilation.
Digital circuits: building blocks of computers
• Digital circuits: interpret electrical potential/voltage as numbers.
• Simplest convention
– Voltage above 1 volt = number 1, Voltage between 0 and 0.2 volt = number 0
– Circuit designed so that voltage will never be between 0.2 and 1 volt, hence no ambiguity.
• Current may also be used, e.g. current < some value represents 0…
• Charge stored on a capacitor may also denote numbers
– Capacitor has low charge = number 0, High charge = number 1
– Once charge is stored on a capacitor, it persists. “Memory”
• Once you can represent 0 and 1, you can represent anything: NEXT
• We can design circuits which perform arithmetic:
– Circuit inputs: sets of voltages representing two numbers
– Circuit outputs: set of voltages representing their sum!
– Or product, or quotient …
Standard representation for non-negative
numbers
• Use many capacitors to store a single number
– Typical number of capacitors used : 8, 16, 32, 64
• Example: To store 25 using 32 bits:
– Represent 25 in binary.
25 Decimal = 00000000000000000000000000011001
• So store the following charge pattern (H=High, L=Low)
LLLLLLLLLLLLLLLLLLLLLLLLLLLHHLLH
• Range stored: 0 to 11111111111111111111111111111111 = 2 32 –
1.
• If your numbers are likely to be larger, then use 64 bits.
• Transmitting numbers: send high or low voltages on as many wires.
Binary representation revision
• Binary number an-1an-2...a1a0 . a-1a-2…a-k
– Example: 101.11
• Decimal value v = ∑i ai2i
– 1*22 + 0*21 + 1*20 + 1*2-1 + 1*2-2 = 5.75
• Converting a decimal integer v to binary
– Divide v by 2, remainder gives a0
– Repeat previous step with the quotient to get a1, a2, …
• Converting fraction f to binary
– If f > 0.5, a-1 = 1
– Similarly other bits…
Representing integers that can be positive or
negative
• One of the bits is used to indicate sign
• Sign bit = 0 (low charge/voltage) means positive number, = 1 means
negative number.
• To store -25 use
10000000000000000000000000011001
• Leftmost bit = sign bit
• Max positive number: 231 - 1
01111111111111111111111111111111
• Range stored: -231 – 1 to 231 – 1.
• Actual representation used:
– more complex. “Two’s complement”.
int nsides;
• C++ will typically designate some 32 capacitors in your
computer for the variable nsides.
• The bit pattern stored in it will be interpreted as having a
sign and a magnitude.
unsigned int nsides;
• 32 capacitors will be given, but the bit pattern in it will be
interpreted as 32 bit binary number.
• 10000000000000000000000000011001 will mean 231+25
Bits, bytes, half-words, words
• Bit = 1 binary “digit”, (one number = 0 or 1)
• byte = 8 bits
• half-word = 16 bits
• word = 32 bits
• double word = 64 bits
“one byte of memory” = memory capable of storing
8 bits = 8 capacitors.
Representing Real numbers
• Use analogue of “scientific notation”:significand * 10exponent
e.g. 6.022 * 1023
• Same idea, but significand, exponent are in binary, thus number is:
significand * 2exponent
• “Single precision”: store significand in 24 bits, exponent in 8 bits.
– Fits in one word!
– 24 bits of significand = 7-8 decimal digits
• “Double precision”: store significand in 53 bits, exponent in 11 bits.
– Fits in a double word!
– 53 bits of significand = 16-17 decimal digits
• Actual representation: more complex. “IEEE Floating Point Standard”.
Indicative example
Let us represent Avogadro’s number 6.022 x 1023
• Convert to binary: 1.11111110001010111 x 21001110
• Use 23 bits for magnitude of fraction, 1 bit for sign of fraction.
• Use 7 bits for magnitude of exponent, 1 bit for sign of exponent
• 0111111110001010111000001001110
• Decimal point is assumed after 2nd bit.
• Inherently imprecise: fraction represented only to certain finite
number of bits.
• IEEE Floating Point Representation: more complex.
Exercise
• Suppose I want to represent 8 digit telephone
numbers. I should use ___ (Fill in 8,16,32,64) bit
_____ (Fill in signed or unsigned) representation.
• Which is bigger, byte or half-word?
• What is roughly the largest number that can be
represented using 64 bits?
– Hint: 210 = 1024 ≈ 103
What we discussed
• Numbers are represented by sequence of 0s and 1s
• The same sequence may mean one number as an unsigned integer, a signed
integer, or a floating point number
• The capacitors only store high or low charge, they are not “aware” that the charge
represents numbers.
• So long as we remember what type of number we are storing, there will be no
problem.
• As a user, you don’t need type/read binary numbers.
– C++ will convert binary numbers to decimal system while printing
– C++ will accept numbers typed in decimal by you and itself convert it to binary for use on
the computer.
– But you should know (roughly) what range of numbers can be stored in k bit unsigned/signed/
floating formats
🐿
How a computer works
Outline
• Overall computer organization
• How the parts work
Organization of a computer
Memory
• Organized into bytes (groups of 8 capacitors)
• Memories of present day computers contain few
Gigabytes, Giga = 230 ≈ 109 , billion.
• Each byte in the memory is assigned a distinct number, or
an address. Much like houses on a street!
• In a memory with N bytes, the addresses go from 0 to N-1
• The memory circuit communicates with the rest of the
world through 3 sets of wires or “ports”: address port,
data port, control port.
Storing/writing data into memory
Suppose we want to store a number D in the byte with address A.
• Place the number A on the address port.
– Address port consists of several wires, place bits of A on respective
wires.
– “Place a number” = “Set the voltage on the wire appropriately”
• Place the number D on the data port.
• Place the number 1 on the control port.
• Hold the values on the ports steady for 1 “clock cycle”.
⇒ Capacitors at address A will get data D!
• Clock cycle = how long? memory designer will tell you..
Reading what is stored
Suppose we wish to know what is stored in address A of the
memory
• Place A on address port
• Place 0 on control port.
• After 1 clock cycle, the values in the capacitors at address A
will appear on the data port.
• Data port connects to the rest of the world by wires.
– Through them the rest of the world will know what was in address
A.
• Reading the value at an address does not destroy it.
Remarks
• You are not expected to understand what circuitry is
present in a memory.
• Instead of reading or writing a byte at a time, entire
word starting at given address A may be read or written
into. (“word oriented” memory)
– “Word starting at address A”: data stored in bytes having
address A, A+1, A+2, A+3.
– The data port will have 32 wires to accept 32 bits = 1 word
• Similarly we can have “double word oriented memory”…
Arithmetic Unit
Ports: Input1, Input2, Output, Control
Input1, Input2, Output will consist of w wires, w =
size of memory data port
Control = several wires. Number appearing on the
control wires will say what operation should be
performed.
• 1 cycle after values are placed on Control, the Output
will take the commanded value: sum, product, …
Peripherals: keyboard, screen, disk…
• Also have control port and data port like organization.
• Value placed on control port tells the peripheral what to
do with the value on the data port, or itself places
values on the data port.
– Screen: data port value will be suitably interpreted and
shown on the screen.
– Keyboard: some value corresponding to what user types will
be placed on the data port by the keyboard circuits.
– Value placed on the data port can be sensed by the rest of the
computer
Control Unit
• Tells other parts of the computer what to do.
– Sends numbers on control wires of each unit
• The control unit decides what to tell other units by reading
a “machine language program” stored in the memory.
• Machine language program = sequence of numbers
representing machine language instructions
• Machine language instruction examples:
– “Make the ALU add the numbers in addresses x,y and store the
result in address z”
– Same but multiply instead of add.
What we discussed
• Memory, ALU, Peripherals communicate with the rest of the world
through
– ”Data port”, “Address port”, “Control port”
• Control unit places values on control ports of other devices and
tells them what to do.
• Control unit arranges movement of data between other parts of
the computer.
• Control unit know what to tell others by reading a ”machine
language program”
🐿
Machine language instruction
Machine language instruction = sequence of numbers
• Possible structure of machine language instruction:
– First number = says what operation to perform (“operation code”)
– Second and third numbers : addresses in memory from where the
operands are to be taken
– Fourth number: address in memory where the result is to be stored.
• Machine language instructions are designed by the computer
designer.
– A machine language instruction will be designed for every operation
the computer can perform
(Fictitious) Examples of machine language
instructions
• Hypothetical instruction: 57, 100, 200, 300
• Operation code 57 might mean “multiply”
• On reading the above instruction control unit does the following:
– Tells the memory to read the words at the first two addresses and send them to the
Arithmetic unit.
– Tells the arithmetic unit to perform multiplication by sending appropriate number on
its control wires.
– Moves the result from the arithmetic unit to the memory
– Tells memory to store the received word into the word at the third address
• This instruction causes the product of the numbers stored in addresses 100,
200 to be stored in the address 300.
• 58 might mean the same thing as above, except perhaps the numbers would
be added.
Machine language program (hypothetical)
example
• Example: 57, 100, 100, 100, 57, 100, 100, 100
• This contains two instructions.
• Both instructions cause the word at address 100 to be
multiplied by itself and the result stored back in address 100.
• After executing the first instruction, address 100 would
contain the square of the number that was present before.
• The second operation would repeat the squaring operation.
• Thus this is a machine language program to compute the
fourth power of a number.
Exercise
• Give a machine language program which
computes the cube of the number stored in
address 100 and stores in address 200. Hint:
Modify the fourth power program slightly.
Control unit operation
• Control unit must be told where the machine
language program is in memory.
• The control unit then fetches the instructions
constituting the program, interprets the codes, and
performs the required operation.
• After one instruction is fetched and executed, it
fetches the next instruction and repeats the process.
• Yes, the circuitry in the control unit is very tricky, but
it can be built!
Remarks
• We said 57 is the code for multiplication..
– The actual code used on different machines will be different.
– You do not need to remember that 57 = multiplication.
• Actual machine languages are more complex, will have many more
instructions.
• On early computers, you would have to write machine language programs.
– Decide what operation you want to perform.
– Look up the manual and find its code.
– Enter the code into the memory of the computer.
– Enter the addresses of operands, result.
– Repeat.
Process is laborious, error-prone.
Machine language programs and C++ programs
• On a modern computer you write a C++ program.
• A prewritten program, “compiler”, translates your C++
program to a machine language program.
– When you type s++ [Link] the compiler is called upon to
compile your file [Link].
– It creates the “machine language program” which by default is
called [Link] on unix.
• When you type ./[Link] :
– [Link] gets loaded into memory by the “loader” program
– Then [Link] executes.
Concluding Remarks
• In order to solve problems on a computer, they must be formulated as
problems on numbers.
• Numerical codes can represent non numerical entities
– Letters and other symbols: ASCII code
• Algorithm: precise sequence of calculations needed to solve a certain problem
– Human beings have been using algorithms well before computers were invented, for
pencil paper calculations.
– You yourself know many algorithms.
– Computer algorithms are very similar to paper pencil algorithms
• Very natural strategy for designing algorithms:
– Think about how you would solve the problem using pencil/paper, given enough time.
– Understand the structure of the required calculations.
– Express those in a programming language. (Rest of the course!)
Concluding Remarks
• Current/charge/voltage values in the computer circuits
represent bits (0 or 1).
– Larger numbers can be represented using sequence of bits.
– In a fixed number of bits you can represent a fixed number of
numbers.
• Circuits can be designed which take as input voltages
representing numbers and produce voltages representing
their product/sum/…
• Memory in a computer is organized as a sequence of
bytes, each byte can be identified by its address.
Concluding Remarks
• Machine language program : sequence of machine language
instructions
– Must be present in the memory
• Control unit reads machine language instructions and
interprets them
– Decides what needs to be done
– Sends control signals to other units and makes them do the needful
• Users write program written in high level language e.g. C++
– User program compiled into machine language by compiler
– Loader loads compiled program into memory and then it executes.