Lecture 1
Lecture 1
FUNDAMENTALS
Kartik Mathur
1
12 June’18
Course Structure
Basicsof Problem Solving
Programming Fundamentals
Object Oriented Programming
Data Structures
12 June’18
Course Administration
Input Output
Computer
12 June’18
It’s a machine!
Assembly language
Machine Code
ARM/MIPS/IBM
12 June’18
Magic Box
000000 00001
Compiler
00010 00110
sum =I+j;
00000 100000
Linker
In High level In Machine Code
12 June’18
So what is an algorithm?
An algorithm is a self-contained step by step
set of operations to be performed in order to
solve a problem.
Its an effective method that can be
expressed within a finite amount of space
and time and in well-defined formal
language for solving a problem.
Another way to describe an algorithm is a
sequence of unambiguous instructions.
12 June’18
Expressing Algorithms?
Algorithms can be expressed in many kind of
notations, including natural languages,
pseudocode, flowcharts, etc.
Natural Language expressions of algorithms
tend to be verbose and ambiguous, and are
rarely used for complex or technical
algorithms.
Programming languages are primarily
intended for expressing algorithms in a form
that can be executed by a computer.
12 June’18
Variable Actions!
What is a flowchart?
Diagrammatic representation illustrating a
solution to a given problem.
Allows you to break down any process
into smaller steps and display them in a
visually pleasing way
It shows steps as boxes of various kinds, and
their order by connecting them with arrows.
It helps your audience to see the logical flow
and relationship between steps.
12 June’18
Flowchart components
Start Initializer / Terminator
IsI
greater Decision
than N ?
Arrow
A Connector
12 June’18
Lets look at few problems and their
flowcharts!
12 June’18
Read P, R, T
SI=(P*R*T)/100
Output SI
End
12 June’18
Read A,B,C
No
Print C End
12 June’18
Read a, d
Let I=1
I=I+1
Yes
IsI<=10
Output term
No
Read N
Let I=1
Add 1 to I
Yes
IsI<=N ?
Output I
No
End
12 June’18
Time to try?
Read five numbers and print their average
Given N, find sum of even numbers from 1 to
N
Given N, check if its prime or not
12 June’18
Read N
I =2
No Print
Is I < N ?
“IS PRIME”
Add 1 to I Yes
IsN
No Yes Print
divisible
“NOT PRIME”
by I?
End
12 June’18
Read N
No
IsI<=N ? Output L
Add 1 to I Yes
No IsNum >
L?
Yes
L = Num
12 June’18
** I=I+1
Read N
*** Print ‘\n’
**** Let I=1
***** J =J +1
No
IsI<=N ?
Print ‘*’
Yes
Yes
No
Let J =1 IsJ <=I ?
End
12 June’18
Time to try?
Given a list of N integers, find mean,
maximum and minimum value. You would
be given first N, and then N integers of the
list.
Given a number check if it is a member of
Fibonacci sequence or not
Read N, and print the following pattern
1
23
456
7 8 9 10
11 12 13 14 15
12 June’18
BT – 3: Average Salary
Three coworkers would like to know their
average salary. However, they are self-
conscious and don't want to tell each other
their own salaries, for fear of either being
ridiculed or getting their houses robbed. How
can they find their average salary, without
disclosing their own salaries?
12 June’18
Pseudocode!
12 June’18
What is pseudocode?
A notation resembling a simplified
programming language, used in program
design
It allows designers or lead programmers to
express the design in great detail and
provides programmers a detailed template
for the next step of writing code in a specific
programming language
12 June’18
Read N
I =2
No Print
Is I < N ?
“IS PRIME”
Add 1 to I Yes
IsN is
No Yes Print
divisible
“NOT PRIME”
by I?
End
12 June’18
Read N
No
IsI<N ? Output L
Add 1 to I Yes
No IsNum >
L?
Yes
L = Num
12 June’18
Time to try?
Convert your flowcharts into
pseudocode
Read N and print the below pattern
1
232
34543
4567654
567898765
III. Given a number N, find sum of its digits
12 June’18
BT – 4: Criminal Cupbearers
An evil king has 1000 bottles of wine. A neighboring
queen plots to kill the bad king, and sends a
servant to poison the wine. The king's guards catch
the servant after he has only poisoned one bottle.
The guards don't know which bottle was poisoned,
but they do know that the poison is so potent that
even if it was diluted 1,000,000 times, it would still
be fatal. Furthermore, the effects of the poison
take one month to surface. The king decides he
will get some of his prisoners in his vast dungeons to
drink the wine. Rather than using 1000 prisoners
each assigned to a particular bottle, this king
knows that he needs to murder no more than 10
prisoners to figure out what bottle is poisoned, and
will still be able to drink the rest of the wine in 5
weeks time. How does he pull this off?
12 June’18
Programming Fundamentals -1
Basic syntax of C++
Datatypes/Variables
Constants
Ifelse
while
Thank You!
Kartik Mathur