0% found this document useful (0 votes)
17 views

Lecture 1

Here are flowcharts for the problems: 1) Given a list of N integers, find mean, maximum and minimum value: Start Read N Let I = 1 Let Sum = 0 Let Max = -infinity Let Min = infinity Is I <= N? Yes Read Num Add Num to Sum If Num > Max then Max = Num If Num < Min then Min = Num Add 1 to I No Mean = Sum / N Output Mean, Max, Min End 2) Given a number check if it is a member of Fibonacci sequence or not: Start Read N Let F1 = 0 Let F2 = 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 1

Here are flowcharts for the problems: 1) Given a list of N integers, find mean, maximum and minimum value: Start Read N Let I = 1 Let Sum = 0 Let Max = -infinity Let Min = infinity Is I <= N? Yes Read Num Add Num to Sum If Num > Max then Max = Num If Num < Min then Min = Num Add 1 to I No Mean = Sum / N Output Mean, Max, Min End 2) Given a number check if it is a member of Fibonacci sequence or not: Start Read N Let F1 = 0 Let F2 = 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Lecture-1

FUNDAMENTALS

• Basics of Problem Solving


• Flowcharts, PseudoCode

Kartik Mathur
1
12 June’18

BT-1 Hour Glass


 You have two hourglasses: a 7 minute one
and a 11 minute one. Using just two
hourglass, accurately measure time 15
minutes.
12 June’18

Course Structure
 Basicsof Problem Solving
 Programming Fundamentals
 Object Oriented Programming
 Data Structures
12 June’18

Course Administration

 Slack – Communication Channel


cblpa18.slack.com
 Codes Repository – Github
 Regular Assignments
(www.hackerblocks.com)
 Laptops
12 June’18

What does a computer do?

Input Output
Computer
12 June’18

Tool for solving problems with data


 To communicate the solution we create
Programs.
 So a program is a sequence of instructions
that tells a computer how to perform a task.
 When computer follows the instructions we
say it executes the program.
12 June’18

It’s a machine!

 Computers are a machine, and at the


most basic level, they are collection of
switches – where 1 represents “on” and 0
represents “off”.
 Everything that a computer does is
implemented in this most basic of all
numbering systems – binary
12 June’18
12 June’18

Its nearly impossible to write in


Binary!
12 June’18

So we use programming language


with Flowcharts & Algorithms for
solving a Problem
12 June’18
12 June’18

What is programming language?


A programming language is a set of rules
that provides a way of telling a computer
what operations to perform
 It provides a linguistic framework for
describing computations.
 A programming language also has w o r d s,
symbols and rules of grammar.
 The grammatical rules are called syntax
12 June’18

High/Low Level Languages!

C++/ Java/ Python

Assembly language

Machine Code

ARM/MIPS/IBM
12 June’18

How do we work with High Level?

Magic Box

000000 00001
Compiler
00010 00110
sum =I+j;
00000 100000

Linker
In High level In Machine Code
12 June’18

Before we write a program for a


solution we need an Algorithm.
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

Two basic aspects of programming


 Data
 Instructions
12 June’18

To understand data we need to


understand Variables!
12 June’18

What are Variables?


 Variables in a computer program are
analogous to Buckets or Envelopes where
information can be maintained and
referenced.
 On the outside of the bucket is a name.
 When referring to the bucket, we use the
name of the bucket, not the data stored in
the bucket.
12 June’18

Variable Actions!

 Create one (with a nice name). A variable


should be named to represent all possible values
that it might contain. Some examples are:
midterm_score, midterm_scores, data_points,
course_name, etc.
 Put some information into it (destroying
whatever was there before). We "put"
information into a variable using the assignment
operator, e.g., midterm_score =93;
 Get a copy of the information out of it (leaving a
copy inside). We "get" the information out by
simply writing the name of the variable, the
computer does the rest for us, e.g., average =
(grade_1 +grade_2) / 2.
12 June’18

Now lets talk about Instructions!


12 June’18

What are instructions?


 Its anorder given to computer.
 At lowest level each of these is a sequence
of 0s and 1s.
 In assembly language, each statement is
one instruction but in high level each
statement can be further broken into
multiple steps.
12 June’18

Six basic computer instructions


 Reading/Receiving some information
 Outputting/Printing some information
 Performing arithmetic operation
 Assigning a value to a variable or
memory location
 Conditional Execution
 Repeat a group of actions
12 June’18

Time for Flowcharts!


12 June’18

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

Read N Input / Output

Let Sum =0, I=1 Process

IsI
greater Decision
than N ?

Arrow

A Connector
12 June’18
Lets look at few problems and their
flowcharts!
12 June’18

Read Principal, Rate & Time and Print SI


Start

Read P, R, T

SI=(P*R*T)/100

Output SI

End
12 June’18

Find largest of three numbers


Start

Read A,B,C

IsA >B Yes


and Print A
A >C
No

IsB > A Yes


and B Print B
>C

No

Print C End
12 June’18

Read a & d, print 10 numbers of form


a+d, a+2d, a+3d…
Start

Read a, d

Let I=1

I=I+1
Yes
IsI<=10

Output term
No

End term =a +I*d


12 June’18

Given N, print all numbers from 1 to N


Start

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

Check if a number is prime or not?


Start

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

Some more examples!


12 June’18

Find largest of N numbers


Start

Read N

Let I=1, L =-1000000

No
IsI<=N ? Output L

Add 1 to I Yes

Read Num End

No IsNum >
L?
Yes
L = Num
12 June’18

Print the below pattern


* Start

** 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

Time for Brain Teasers!


12 June’18

BT – 2: Apples and Oranges


There are three closed and opaque
cardboard boxes. One is labeled "APPLES",
another is labeled "ORANGES", and the last is
labeled "APPLES AND ORANGES". You know
that the labels are currently misarranged, such
that no box is correctly labeled. You would like
to correctly rearrange these labels. To
accomplish this, you may draw only one fruit
from one of the boxes. Which box do you
choose, and how do you then proceed to
rearrange the labels?
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

Notation for those basic six!


 Reading/Receiving [ read N ]
 Outputting/Printing [ print Sum, print
“Coding Blocks”, print 1, print ‘\n’ ]
 Assignment [Sum  5]
 Arithmetic operators [ a+b, a *5, sum +i ]
 If Else [ if I < N then … else then … endif ]
 While Loop [ while I < N do … endwhile ]
12 June’18

Lets convert some flowcharts into


pseudocode!
12 June’18

Check if a number is prime or not?


Start

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

Pseudocode- Check if N is prime?


read N
I 2
While I<N do
if N is divisible by Ithen
print “NOT PRIME”
exit
endif
I I+1
endwhile
print “IS PRIME”
exit
12 June’18

Find largest of N numbers


Start

Read N

Let I=1, L =-1000000

No
IsI<N ? Output L

Add 1 to I Yes

Read Num End

No IsNum >
L?
Yes
L = Num
12 June’18

Pseudocode –Largest of N numbers


read N
I 1, L  -100000
while I<N do
read num
if num >L then
L  num
endif
I I+1
endwhile
print L
exit
12 June’18

Lets try one more pattern!


1 read N
i 1
123 while i <=N do
j 1
12345 while j <=n-i do
print ‘ ‘
1234567 j j+1
endwhile
123456789 j  1, value  1
while j <=2*i - 1 do
print value
j j+1
value  value +1
endwhile
print ‘\n’
i  i +1
endwhile
exit
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

What is next class about?


12 June’18

Programming Fundamentals -1
 Basic syntax of C++
 Datatypes/Variables
 Constants
 Ifelse
 while
Thank You!

Kartik Mathur

You might also like