Problem Solving Using JAVA
Course Code: CSE1004
Module 1 :
Introduction to Problem Solving
Dr. Alok Kumar Shukla
School - SCOPE
VIT-AP Amaravati
What is a computer ?
A computer is an electronic
device that manipulates
information or data. It has the
ability to store, retrieve, and
process data.
Characteristics of Computers
1- Store a large amount of data and
information for a long period of time.
2- process data and information in high
accuracy level.
3- Speed in processing data information.
4- Sharing of information / network.
Categories
A computer can be divided into two main categories:
1. Hardware
Hardware is basically anything that you can touch with
your fingers.
2. Software
For computer hardware to work it must follow a set of
instructions that is supplied to it as software.
Hardware
Hardware refers to the computer’s physical components,
• Computer Case
• CPU (central processing unit...Pentium chip)
• Monitor
• Keyboard & Mouse
• Disk Drive
• Hard Drive
• Memory (RAM)
• Scanner
• Printer
Software
The term software refers to the set of instructions
that directs the hardware to accomplish a task.
Application
Software
1. System Software
2. Application Software System
Software (OS)
3. Utility Software
Hardware
1. System Software
• The software that controls everything
that happens in a computer.
• Background software, manages the
computer’s internal resources
Resources examples : Operating systems, CPU, RAM , I/O devices, …
2. Application Software
• Word processors– example: Microsoft
word
• Spreadsheets-- example: Microsoft
Excel
• Database managers-- example:
Microsoft Access
• Graphics-- example: Photoshop
Spreadsheets: Computer software that allows the user to enter
columns and rows of numbers in a accounting book like format.
3. Utility Software
Utility Software is used to remove any problem or
solve a complex situation in computer.
Partition Magic Backup utility
Antivirus Data Recovery
Security Software Win Ghost
Memory Unit
Bit: One digit, either 0 or 1
Byte: any combination of 0 or 1. also
called an octet.
Computer only understand 0 or 1
0 OFF
1 ON
The number 25 stored as 8 bits using the binary number system:
25 = 00011001 =
The number 179 stored as 8 bits using the binary number system:
179 = 10110011 =
The letter A stored as 8 bits using American Standard Code for Information
Interchange code:
A = 01000001 =
Information for the Exam
INPUT
PROCESSING
OUTPUT
Exam Results!
PC four major functions are:
Input data
Process data
Output information
Store data and information
DATA IN
INFORMATION
OUT
So
Computer is an Electronic device which can:
1. accept data
2. process it
3. give the result after that processing
4. and also store processed data Processing
Output
Input
Storage
Data vs. Information
Data is a representation of a fact or idea
Number examples of data
3547 Ahmad Kuala Lumpur Malaysia
Word
Picture examples of
information
Sound Roll No. 3547 Name-Ahmad City-Kuala Lumpur
Country-Malaysia
Information is data that has been organized or
presented in a meaningful.
History of Computer
First Generation Vacuum tubes
Second generation Transistors
Third generation Integrated Circuits
Fourth generation Large Scale
Integrated Circuits
Fifth generation Very Large Scale Integrated
Circuits
Four Kinds of Computers
1. Microcomputers 2. Minicomputers
3. Mainframe computers 4. Supercomputers
• Microcomputer =>Personal
Computer => PC
• There are 3 types of the
Microcomputers :
1.Laptop
2.Desktop
3.Workstation
• Personal Computer: A small, single-user
computer based on a microprocessor.
• Workstation: A powerful, single-user computer.
A workstation is like a personal computer, but it
has :
a more powerful microprocessor and,
in general, a higher-quality monitor.
Minicomputer, Mainframe, and
Supercomputer
• Minicomputer: A multi-user computer capable
of supporting up to hundreds of users
simultaneously.
• Mainframe: A powerful multi-user computer
capable of supporting many hundreds or
thousands of users simultaneously.
• Supercomputer: An extremely fast computer
that can perform hundreds of millions of
instructions per second.
Internally
Block Diagram of Computer
The CPU consists of :
Control Unit (CU)
Arithmetic Logical Unit (ALU) (addition,
subtraction, multiplication, division,
and logic operations)
Some Registers (i.e., Accumulator ,
Program Counter)
Control Unit
• Control unit (CU) is a component of a
computer's central processing unit (CPU)
that directs the operation of the processor.
• It tells the computer's memory, arithmetic
and logic unit and input and output devices
how to respond to the instructions that
have been sent to the processor.
The ALU :
consists of electronic circuitry to
perform:
• Arithmetic operations (addition,
subtraction, multiplication and division)
• Logical operations (and, or, not, …) and
to make some comparisons (less-than,
equal, … etc.)
Primary Memory
• Memory (fast, expensive, short-term
memory): Enables a computer to store, at
least temporarily, data, programs, and
intermediate results.
• Two general parts:
1.RAM
2.ROM
RAM (Main Memory)
• its a primary storage or random access memory
(RAM).
• it temporarily holds data and programs for use
during processing (volatile)
• Any information stored in RAM is lost when the
computer is turned off.
• RAM is the memory that the computer uses to
temporarily store the information as it is being
processed. The more information being
processed the more RAM the computer needs.
• RAM consists of locations or cells. Each cell has
a unique address which distinguishes it from
other cells.
ROM: Read Only Memory
ROM is part of memory
Programmed at manufacturing time
Its contents cannot be changed by users
It is a permanent store
Information coding
• Binary
0 or 1
• Octal
0-7
• Hexadecimal
0-9+A-F
• Decimal
0-9
ALGORITHMS ANDFLOWCHARTS
Introduction
The computer is a machine that follows the set of
instructions called program. The problem solving
techniques involves the following steps:
1. Define the problem.
2. Formulate the mathematical model.
3. Develop an algorithm.
4. Write the code for the problem.
5. Test the program.
Problem solving approach
1. Define the Problem
• A clear and concise problem statement is provided.
• The problem definition should specify the input and
output.
• Full knowledge about the problem is needed.
Example: TO FIND THE AVERAGE OF TWO NUMBERS.
2. Formulate the Mathematical Problem
• Any technical problem provided can be solved
mathematically.
• Full knowledge about the problem should be provided
along with the underlying mathematical concept.
Example: (data1+data2)/2
Continue….
3. Develop an Algorithm
• An algorithm is the sequence of operations to be
performed.
• It gives the precise plan of the problem.
Example: TO FIND THE AVERAGE OF TWO NUMBERS.
Algorithm:
1. Set the sum of the data values to 0.
2. Set the count of the data values to zero.
3. As long as the data values exist, add the next data value
to the sum and add 1 to the count.
Continue…..
4. To compute the average, divide the sum by the count.
5. Print the average.
Task: To find the average of 20 and 30 manually.
20 + 30=50 ; 50/2 =25.
4. Write the Code for the Problem
• The algorithm developed must be converted to any
programming language.
• The compiler will convert the program code to the
machine language which the computer can understand.
5. Test the Program
• Testing involves checking errors both syntactically and
semantically.
Why Algorithm is needed?
Computer Program ?
Set of instructions to perform some specific task
Is Program itself a Software ?
NO, Program is small part of software.
Software merely comprises of Group of Programs (Source
code),
Code: refers to statements that are written in any
programming language as machine code or C code.
•Code is some time interchangeably used with program.
2
Program design
Problem Program
Algorithm
Informal definition of algorithm
Algorithms
An algorithm is a finite set of steps defining the solution of a
particular problem.
Need not to belong one particular programming language
Sequence of English statements can also be algorithm
It is not a computer program
An algorithm can be expressed in English or in the form of
flowchart.
Algorithm Specification
Every algorithm must satisfy the following
criteria:
Input. Zero or more quantities are externally supplied.
Output. At least one quantity is produced.
Definiteness. Each instruction must be clear and
unambiguous (Unique meaning).
Finiteness. An algorithm terminates in a finite number of
steps.
Effectiveness. Operations used in algorithm must be simple
and easy to understand.
Flowchart
A graphical representation of an algorithm, often
used in the design phase of programming to
work out the logical flow of a program.
Visual way to represent the information flow
Make our logic more clear
Help during writing of program
Make testing and debugging easy
Flowchart or program constructs
Sequence: The order of execution, this typically refers to the order
in which the code will execute. Normally code executes line by
line, so line 1 then 2 then 3 and so on.
Selection: Selection, like branching, is a method of controlling the
execution sequence, you can create large control blocks, using if
statements testing a condition, or switch statements evaluating a
variable to control and change the execution of the program
depending on this environment and changing variables.
Iteration (Repetition): Iteration is typically used to refer to
collections and arrays of variables and data. Repeating set of
instruction. Counting from 1 to 10, you are iterating over the first
22 10 numbers. for, while, do-while loops will be implemented for
iteration.
program constructs
Flowchart
Flowchart (cont..)
24
Flowchart (cont..)
25
Write algorithm to add two numbers
• Step 1: Start
• Step 2: Take two numbers a, b and Sum for result
• Step 3: Enter two numbers a and b
• Step 4: Sum <- a + b
• Step 5: Display Sum
• Step 6: Stop
NOTE :
• An algorithm is a step by step procedure to solve a particular problem
whereas a program is an algorithm that is encoded in any programming
language.
• Program is language dependent and algorithm is language independent.
Flowchart (Cont..)
Flowchart for Sum of two numbers
Exchanging/Swapping the values of two variables
Two different methods:
i) Using a temporary variable
ii) Without using a temporary variable
Exchanging the values of two variables
Using temporary variable
Suppose x and y are the two variables.
x=4 and y=5
Take temporary variable say T
T=x
x=y
y=T
• After swapping the values of x=5 and y=4
Exchanging the values of two variables
Without using temporary variable
Suppose x and y are the two variables.
x=4 and y=5
x=x+y
y = x-y
x = x-y
After swapping the values of x and y will be 5 and 4
Flowchart/Alogoirthm for
exchanging values of two variables
Algorithm of Counting
Algorithm to Print “Hello World” 10 times
Step1: Start
Step2: Initialize count = 0 (PROCESS)
Step3: Print Hello World (I/O)
Step4: Increment count by 1 (PROCESS)
Step5: if count < 10 go to step 3 (DECISION)
Step6: Stop
Flow chart of Counting
Flowchart to Print “Hello World” 10 times
Summation of first n numbers
Algorithm for sum of first n natural numbers
Step1: Start
Step2: Read the value of n.
Step3: i = 1 , Sum = 0
Step4: if ( i >= n ) Go to 8
Step5: Sum = Sum + i
Step6: i = i + 1
Step7: Go to 4
Step8: Display the value of Sum
Step9: Stop
Flowchart for sum of first n numbers
Factorial computation
Algorithm to calculate the factorial of a number
step 1. Start
step 2. Read the number n
step 3. Initialize i=1, fact=1
step 4. Check is i <= n if TRUE then continue
ELSE Goto step 7
step 5. fact=fact*i
step 6. i=i+1 Goto step 4
step 7. Print fact
step 8. Stop
Factorial computation
Flowchart to calculate the factorial of a number
Generation of the Fibonacci sequence
Algorithm to Generation Fibonacci sequence
step 1. Start
step 2. Declare variables i, a, b
step 3. Initialize the variables, a=0, b=1, i=2 and show =0
step 4. Enter the number of terms of Fibonacci series to be
printed say N
step 5. Print First two terms of series
step 6. while(i<N)
show=a+b
a=b
b=show
i=i+1
Print “show”
step 7. Stop
Flow chart of Fibonacci sequence
Algorithm of Reversing the digits of integer
Example:
n = 4562
sum = 0
• r = n % 10 = 4562 % 10 = 2
• sum = sum *10 + r = 2
n = n/10 = 456
• sum = sum *10 + r = 20 + 6 = 26
n = n/10 = 45
• sum = sum *10 + r = 260 + 5 = 265
n = n/10 = 4
• sum = sum *10 + r = 2650 + 4 = 2654
n = n/10 = 0
Flowchart of Reversing the digits of integer