Sie sind auf Seite 1von 61

Problem solving using

computers
• Block Diagram of a Computer
• Computer Memories
• Algorithms &Flowcharts
• C++ Fundamentals
• Operators and Expressions
• Flow of control
• Derived Data types
• Functions
• OOPs
12/07/21 Dept of CS&Engg. 1
References:

• Programming in ANSI C, 2nd Edition, - E. Balaguruswamy,


TataMcGraw Hill
• Object Oriented Programming with C++, 2nd edition, -E.
Balaguruswamy, TataMcGraw Hill
• C++:The Complete Reference 4th edition – Herbert Schildt,
Tata McGrawHill.
• Object Oriented Programming with Turbo C++ - Robert
Lafore, Gagotia Publications, 2002

12/07/21 Dept of CS&Engg. 2


• Evaluation:

• Theory : Class participation & Assignments (10), Test


(40), Final exam (50)
• Laboratory : Continuous Evaluation 60(20-Viva, 20-
Record, 20-Regular lab performance. Final Test 40 (20-
write up,20-execution)
• Class participation includes participation in both
lectures and tutorials (attendance, asking and
answering questions, presenting solutions to tutorial
questions etc).
• Assignments (in class, 1 hr) are conducted in the
class prior to every test (last class before every
sessional exam) – Mode: open/close book / quiz
Block Diagram of a Computer

12/07/21 Dept of CS&Engg. 4


Computer Components--
Hardware

Dept of CS&Engg. 5
Flow of Information
• The parts are connected to one another by a
collection of wires called a bus

12/07/21 Dept of CS&Engg. 6


Central Processing Unit
 Arithmetic/Logic Unit
 performs arithmetic operations, and makes logical
comparisons.
 Control Unit
 controls the order in which your program instructions are
executed.
 Fetches data and instructions from main memory
 Interprets these instructions
 Controls the transfer of data and instructions to and from
main memory
 Controls input and output devices.
 Overall supervision of computer system
12/07/21 Dept of CS&Engg. 7
Computer Memories

12/07/21 Dept of CS&Engg. 8


Memory Unit
• It is a storage device where the data and
instructions fed by the user are stored.
• It is an ordered sequence of storage cells, each
capable of holding a piece of information

• each cell has its own unique address

• the information held can be input data,


computed values, or your program instructions.

12/07/21 Dept of CS&Engg. 9


Memory Unit

12/07/21 Dept of CS&Engg. 10


Memory Unit (2)
• The memory is measured in terms of bits,
bytes and words.
• A bit is a binary digit either 0 or 1.
• A byte is basic unit of memory and is
defined as sequence of 8 bits.
• The word can be defined as a sequence of
16 bits or 2 bytes.
• The memory is classified into
– Main memory
– Secondary memory
– Cache Memory
12/07/21 Dept of CS&Engg. 11
Main memory (3)
• It is the place where the data and
instructions supplied by the input devices
are stored.

• This is a temporary memory because the


data and instructions stored here erased /
lost when the power goes off.

• This is also referred as primary memory.

12/07/21 Dept of CS&Engg. 12


Main memory (4)
• It is a semiconductor memory and
measured in terms of megabytes and
gigabytes.

• This can be further classified into:


– Random Access Memory (RAM)
– Read Only Memory (ROM)

12/07/21 Dept of CS&Engg. 13


RAM stands for Random Access Memory

• It is the read and write memory.


• The information typed by the user are stored
in this memory and any memory location can be
accessed directly without scanning it
sequentially.
• Hence it is called as random access
memory.
• It is also called as volatile memory.

12/07/21 Dept of CS&Engg. 14


ROM stands for Read Only Memory

• Permanent memory and non volatile.


• The contents in locations in ROM can
not be changed.

• It stores mainly stored program and


basic input output systems programs.
12/07/21 Dept of CS&Engg. 15
• PROM :Programmable Read Only Memory
• It is the improved version of ROM.
• The contents of this memory are decided by the
user
• These contents can not be erased once they
are written to it.

• EPROM : Erasable Programmable Read Only


Memory .
• It is a modification to PROM.
• Contents stored can be erased by exposing it to
Ultra Violet Light source.

12/07/21 Dept of CS&Engg. 16


• EEPROM: Electrically Erasable
Programmable Read Only Memory

• The contents stored in this can be erased


electrically.

12/07/21 Dept of CS&Engg. 17


Secondary memory
• It is permanent memory and made up of
magnetic material
• Stores large amount of information for long
time.
• It is connected to CPU.
• Magnetic tapes, disks etc

12/07/21 Dept of CS&Engg. 18


Magnetic Tape

• The first truly mass


auxiliary storage
device was the
magnetic tape drive

12/07/21 Dept of CS&Engg 19


Magnetic Disks
• A read/write head travels across a spinning
magnetic disk, retrieving or recording data

12/07/21 Dept of CS&Engg 20


Compact Disks
• A CD drive uses a
laser to read
information stored
optically on a plastic
disk
• CD-ROM is Read-
Only Memory
• DVD stands for
Digital Versatile Disk

12/07/21 Dept of CS&Engg 21


Cache memory
• It is a High speed memory and placed
between the CPU and the Main Memory.

• Users can not access this memory.

• It stores the data and instructions currently


to be executed.

• More costlier than main memory.

• Less Capacity than main memory

12/07/21 Dept of CS&Engg. 22


Peripherals
 input, output, or auxiliary storage devices attached
to a computer

• Input Devices include keyboard and mouse.


• Output Devices include printers, video display, LCD
screens.
• Auxiliary/Secondary Storage Devices include disk drives,
scanners, CD-ROM and DVD-ROM drives, modems,
sound cards and digital cameras.

12/07/21 Dept of CS&Engg. 23


12/07/21 Dept of CS&Engg 24
Application Software :
General purpose program or a collection of
programs written by the users to solve a particular
problem,
e.g., Payroll, Inventory System, etc.

System Software :
Collection of programs written for computer system
management. They are supervisory programs and
help in executing the user’s programs effectively.
Types:
(i) Operating System,
(ii) Language Processors and
(iii) System Utilities.
12/07/21 Dept of CS&Engg 25
Operating System (OS) :

• is an integrated collection of programs which make


the computer operational and help in executing user
programs.

• It acts as interface between the man and machine.

• It manages the system resources like memory,


processors, input-output devices and files.

•single user operating system, multi user operating


system, real time operating system etc.

Examples are : DOS , UNIX , Windows


12/07/21 Dept of CS&Engg 26
Language Processors :

• basically translator programs


• used to translate programs written in one programming
language to machine level language.
Eg: Interpreters, Compilers and Assemblers.

Interpreter : Program which translates one statement of


a high level language program into machine language at a
time and executes it.
e.g., Basic Interpreters, Java Interpreters.

Compiler : Entire high level language program as input


and translates it into machine language at a time.

12/07/21 Dept of CS&Engg 27


Assembler : is a program which translates an
assembly language program into machine language.
e.g., TASM(Turbo ASseMbler), MASM(Macro AsseMbler).

System Utilities : assist in loading and linking object


programs. e.g. Loader and Linker.

Loader: loads the object program into the main


memory.

Linker : links the specified subprograms referenced in


the main program, for the purpose of successful
execution of the program.
12/07/21 Dept of CS&Engg 28
Low level languages :
• are machine dependent languages.
• are easily understood by computers.
eg: machine language, assembly languages
Machine Languages :
As the name implies, programming is done at machine
level. The vocabulary of computer consists of only two
words zero and one. e.g., 10101011

Assembly Language :
uses symbolic instructions.
e.g., ADD X,Y; Add the contents of y to x
12/07/21 Dept of CS&Engg 29
Algorithms & Flowcharts

12/07/21 Dept of CS&Engg. 30


Typical steps involved in problem
solving by a Computer
• Problem Definition
• Analysis
• Algorithm
• Flowchart
• Pseudo code
• Coding
• Running the program
• Debugging
• Testing
• Documentation
12/07/21 Dept of CS&Engg. 31
ALGORITHM
What is an Algorithm?

An algorithm is a step by step procedure to


solve a particular problem

12/07/21 Dept of CS&Engg. 32


CHARACTERISTICS OF AN
ALGORITHM
• Finiteness
• Definiteness
• Effectiveness
• Input
• Output

12/07/21 Dept of CS&Engg. 33


Steps involved in developing an
algorithm
• Understand the problem statement very clearly

• Specify the inputs and outputs

• Design the process that will produce the desired


results after taking the inputs
• Test the algorithm by giving different test data

12/07/21 Dept of CS&Engg. 34


Advantages of algorithm
• Very easy to understand as it is a step-by-step
solution to the given problem
• It has got a definite procedure that can be
executed within a definite period of time

• Independent from any programming language


syntax
• Very easy to find the errors as every step is
clearly mentioned
Algorithmic Notations

• Name of the algorithm

• Step Number

• Explanatory comment

• Termination

12/07/21 Dept of CS&Engg. 36


Algorithm to compute the area of circle
Algorithm : Area of a circle

Step1: Read radius

Step 2: [Compute the area]


Area  3.1416 * radius * radius

Step 3: [Print the Area]


Print ‘Area of a circle =‘, Area

Step 4: [End of algorithm]


Stop
12/07/21 Dept of CS&Engg. 37
Algorithm for arithmetic operations

Algorithm: Arithmetic operations


Step1: input A,B
Step2: sum  A+B
diff  A-B
prod  A*B
quot  A/B
Step 3: Print ‘sum of A and B=‘,sum
Print ‘difference of A and B=‘,diff
Print ‘product of A and B=‘,prod
Print ‘quotient of A and B=‘,quot
Step4: [End of Algorithm]
Stop

12/07/21 Dept of CS&Engg. 38


Algorithm to calculate Simple Interest and Compound Interest
Where P=amount deposited T=duration R=rate of interest

Algorithm: Calculate SI and CI


Step1: input P,R,T
Step 2: SI  ( P*R*T)/100

Step 3: CI  P*(1+ R/100)T-P

Step 4: Print ‘Simple Interest=‘,SI


Print ‘Compound Interest=‘,CI
Step 5: [End of Algorithm]
Stop

12/07/21 Dept of CS&Engg. 39


Algorithm to find largest of 3 numbers

Step 1: Input A,B,C


Step 2: LA
Step 3: If (B>L) then
L B
Step 4: if (C>L) Then
LC

Step 5: Print L
Step 6: [End of Algorithm]
Stop

12/07/21 Dept of CS&Engg. 40


Algorithm to find largest and smallest of 3 nos

Algorithm: largest of 3 and smallest of 3 numbers

Step 1 : INPUT A,B,C


Step 2 : L A
Step 3: S A
Step 4: if ( B>L) then LB
else if (B<S) then SB
Step 5: if(C>L) then LC
else if(C<S) SC
Step 6: Print L,S

12/07/21 Dept of CS&Engg. 41


Algorithm to interchange the values of two
numbers
Algorithm: Interchange of 2 numbers
Step1: INPUT A,B
Step 2: temp  A

Step 3: AB

Step 4: Btemp

Step 5: Print A,B


Step 6: [End of Algorithm]
Stop

12/07/21 Dept of CS&Engg. 42


Algorithm to interchange the values of two
Numbers without using another variable

Algorithm: interchanging values


Step1: Input A,B

Step 2: AA+B

Step 3: BA-B

Step 4: AA-B

Step 5: Print A,B


Step 6: [End of Algorithm]
Stop
12/07/21 Dept of CS&Engg. 43
Algorithm to find the sum and mean of first N
natural numbers
Algorithm: sum and mean of natural numbers.
Step1: Input N

Step 2: Sum  0

Step 3: For Num=1 to N in step of 1 do


begin
Sum  Sum +Num
end
Step 4: Mean  Sum / N
Step 5: Print ‘Sum of N natural numbers=‘,Sum
Print ‘Mean of N natural numbers =‘,Mean
Step6: [End of algorithm]
12/07/21 Stop Dept of CS&Engg. 44
Algorithm to compute factorial of a number

Algorithm: Factorial of a number N.


Step1: Input N
Step 2: fact 1

Step 3: for count=1 to N in step of 1 do


begin
factfact*count
end

Step 4: Print ‘fact of N=‘, fact


Step 5: [end of algorithm]
Stop
12/07/21 Dept of CS&Engg. 45
Algorithm for fibonacci series
Algorithm : Fibonocci Series
Step 1 : Input Limit
Step 2: A0,B1
Step 3: print A
Step 4: while (B<limit)
begin
Print B
CA+B
AB
B C
end
Step 5: [End of Algorithm]
Stop

12/07/21 Dept of CS&Engg. 46


Algorithm to count the no of digits of a
number
Algorithm: count the no of digits.
Step 1: input N
Step 2: count 0
Step 3: while (N>0)
begin
NN/10 (integer)
count count+1
end
Step 4 : Print count.
Step 5: : [End of Algorithm]
Stop

12/07/21 Dept of CS&Engg. 47


Algorithm to calculate sum of digits of
a number
Algorithm: find sum of digits of a number
Step 1: Input N
Step 2: sum0
Step 3: while (N>0)
begin
remN mod 10
sum sum +rem
NN/10 (integer)
end
Step 4: print sum
Step 5 : [End of Algorithm]
Stop
12/07/21 Dept of CS&Engg. 48
Algorithm to check whether 3-digit
no. is Armstrong
Algorithm: Check for Armstrong number.
Step 1: Input N
Step 2: Num N , Sum  0
Step 3: while(N>0)
begin
D N mod 10
sum sum + D * D *D
N N/10
end
Step 4 : if ( sum = Num) then
print ‘Armstrong’
else
print ‘not Armstrong’
Step 5: [End of Algorithm]
Stop
FLOW CHARTS
•It is a chart showing flow of logic involved in solving
the problem.
•It is defined for an algorithm.
•It can also be defined as the diagrammatic
representation of an algorithm.
•It is also defined as a visual or graphical
representation of an algorithm.
•It is easy to understand and analyze the problem.
•It is an useful aid for programmers and system
analysts.
•It is machine independent. They can be used for
any type of logic
Symbols used in Flowcharts

Name Symbol Function


Terminal To indicate begin and end in a flowchart

Input/output Input or output data or information

Processing Calculations, data manipulation or


information processing
Looping Processes a group of instructions
repeatedly
Decision A comparison or decision which leads the
flow of control to take alternate paths
Predefined Group of operations not listed in the
particular flowchart
process
Connector An entry from or an exit to another part in
a flowchart
Flow Direction Represents the direction of flow of control

12/07/21 Dept of CS&Engg. 51


Flowchart to find the largest of 3 numbers
Start

Read A, B, C

A>B

A>C B>C

Print B Print C
Print A Print C

Stop
Q Show using flowchart how to find the average height of boys and
average height of girls in the class.

Ans:
t_b_h  total boys height
t_g_h  total girls height
t_b  total no of boys
t_g  total no of girls
a_v_b  average height of boys
a_v_g  average height of girls
n  total no of students
n1  no of boys and girls after each iteration
code=0 (for girl)
code=1 (for boy)
start

t_b_h  0
t_b  0
t_g_h  0
t_g  0
n  10
a_v_g  0
a_v_b 0
n1  0
B
read roll no , code , height

Y N
code =1

t_b_h  t_b_h + height t_g_h  t_g_h + height


t_b  t_b + 1 t_g  t_g +1
n1  n1 + 1 n1  n1+1

A
A

a_v_b  t_b_h / t_b


B n1=n
a_v_g  t_g_h / t_g

Print a_v_b , a_v_g

Stop
Start
To find
Read n , num1 largest of n
numbers
using 2
memory
repeat till 4 , locations
till n = 1 num1 and
num2
read num2

num2>num1 num1  num2

n  n-1
4

Print num1

stop
Q) Draw a flowchart to count the number of non-zero numbers in a
series of 10 numbers.
Start

nzd  0,
i=1

repeat till 4 ,
till i=11

enter d

d=0 nzd  nzd + 1

i  i+1

Print nzd

stop
Draw a flowchart to find the sum of the first 50
natural numbers
Draw a flowchart for computing factorial of N
QUESTIONS TO BE SOLVED BY YOU
Write an algorithm and draw a flow chart for the following problems
1. Read a number N and print all its divisors.
2. Compute the sum of the digits of any given number
3. Find the sum of given N numbers.
4. Sum of squares of integers from 1 to 50
5. Print the Message “Hello” on the Screen.
6. Display a Triangle of * on the screen.
7. Perform the different arithmetic Operations.
8. Evaluate the area of the circle Area = Pi * R*R
9. Convert the time in seconds to hours, minutes and seconds. (1 hr
=3600 sec).
10. Find the sum of the digits of a four-digit number (ex 1234 sum=10).
11. Convert temperature given in Fahrenheit to Centigrade and
Centigrade to Fahrenheit (C=5/9(F-32)).
12. Converting distance in mm to cm, inch, feet (1 cm =10mm,
1inch=2.5cm, 1 feet =12 inches).
13. Compute nth Fibonacci no.
14. Generate Fibonacci series up to n.
15. Covert binary number to decimal.
16. Convert decimal number (Positive) to binary.

Das könnte Ihnen auch gefallen