SCSA1104 Unit 1
SCSA1104 Unit 1
SCHOOL OF COMPUTING
1
Introduction to Computer Problem Solving
UNIT 1
INTRODUCTION TO COMPUTER PROBLEM SOLVING
1.1 Algorithms
List the data needed to solve the problem (input) and know what is the end
result (output).
Describe the various step to process the input to get the desired output. Break
down the complex processes into simpler statements.
Step1: Start
Step 4: Print c
Step 5: Stop.
The above algorithm is to add two numbers a and b. The numbers are the input
provided by the user .After adding the result is stored in a variable c and it is printed.
1.2.2 Control flow: The process of executing the statements in the given
sequence is called as control flow.
3
Introduction to Computer Problem Solving
Sequence
Selection
Iteration
Sequence
Example: Algorithm to find the average of three numbers, the algorithm is as follows
Step1: Start
Step6: Print d
Step7: Stop
The above algorithm finds the average of three numbers a, b and c. The numbers are
the input provided by the user .After adding the total is divided by 3 and the result is
stored in a variable d and it is printed.
Selection
If the condition is true, one part of the program will be executed, otherwise the other
part of the program will be executed.
4
Introduction to Computer Problem Solving
Example: Algorithm to find the Largest of two numbers, the algorithm is as follows
Step1: Start
Step 6: ENDIF
Step 7: Stop
The above algorithm is used to find the Largest of two numbers a and b. The numbers
are the input provided by the user .The number a and b are compared, If a is larger
Print “A is Large” or if b is larger print “B is Large”.
Iteration
Step 1: Start
Step 6: Go to step 4
Step 7: Stop
5
Introduction to Computer Problem Solving
The above algorithm is for printing first n natural numbers .The user provides
the input. The first value, i is initialized. A loop is initialized. The first value is
printed and the number is incremented. The i value is checked with n, the user input.
The numbers are printed until i becomes greater than n.
1.2.3 Functions
Function is a sub program which consists of block of instructions that performs
a particular task. For complex problems, the problem is been divided into smaller and
simpler tasks during algorithm design.
Benefits of Using Functions
Step 1: Start
Step 2: Call the function add ()
Step 3: Stop
The above algorithm is to call the function add. This function is called as Main
function or calling function.
Subfunction add ()
Step 1: Function start
Step 2: Get two numbers as input and store it in to a and b
Step 3: Add the numbers a & b and store the result in c
Step 4: Print c
6
Introduction to Computer Problem Solving
Step 5: Return.
The above algorithm is called as the called function which is to add two numbers a
and b. The numbers are the input provided by the user .After adding the result is
stored in a variable c and it is printed.
1.3 NOTATIONS
Pseudo code means a short, readable set of instructions written in English to explain
an algorithm.
Comment: //
Start: BEGIN
Stop: END
Input: INPUT, GET, READ
Calculate: COMPUTE, CALCULATE, ADD, SUBTRACT, INITIALIZE
Output: OUTPUT, PRINT, DISPLAY
Selection: IF, ELSE, ENDIF
Iteration: WHILE, ENDWHILE, FOR, ENDFOR
Example: Pseudo code to Add two numbers
7
Introduction to Computer Problem Solving
BEGIN
GET a, b
ADD c=a+b
PRINT c
END
Advantages:
It is not visual.
For a beginner, it is more difficult to follow the logic or write pseudo code
as compared to flowchart.
Flow Chart
Flow chart is defined as the graphical or diagrammatical representation of the process
of solving a problem. It represents the steps of a process in a sequential order.
8
Introduction to Computer Problem Solving
Rules:
Only one flow line should come out from a process symbol
Only one flow line should enter a decision symbol. However, two or three
flow lines may leave the decision symbol
9
Introduction to Computer Problem Solving
Advantages:
10
Introduction to Computer Problem Solving
Disadvantages:
The flowcharts are complex and clumsy when the program is large or
complicated.
The cost and time taken to draw a flowchart for larger applications are
expensive.
The above flowchart is used for adding two numbers Number1 and Number2.The
numbers are the input provided by the user .After adding, the result is stored in the
variable Sum and is printed.
The above flowchart is used for printing all odd numbers up to 100. The first value, i
is initialized as zero. A loop is initialized. Starting from i=0, each value is divided by
11
Introduction to Computer Problem Solving
2 and the remainder is captured .This operation is called modulo of a number. The
modulo of first value is calculated if the result is not equal to zero then the number is
printed and the number is incremented. The i value is checked with the limit, 100.
The numbers are printed until the value is less than the input value.
The above flowchart is to find the Largest of two numbers NO1 and NO2.The
numbers are the input provided by the user .The number NO1 and NO2 are
compared, If NO1 is larger, the number NO1 is printed or if NO2 is larger, the
number NO2 is printed.
12
Introduction to Computer Problem Solving
13
Introduction to Computer Problem Solving
The flowchart 1.5a is for the calling function. This function is also called as
Main function . The flowchart1.5b is called as the called function which is to add two
numbers a and b.The numbers are the input provided by the user .After adding the
result is stored in a variable c and it is printed.
Types of Flowcharts
Process Flowchart
Data Flowchart
Business Process Modeling Diagram
Step 1: Start
Step 2: Estimation of people below poverty line.
Step 3: Recommendation about the type and quantity of food from the food
committee
Step 4: Decide the cost
Step 5: Generation of Revenue to meet the food demand
Step 6: Provide food through public distribution system at all levels.
14
Introduction to Computer Problem Solving
15
Introduction to Computer Problem Solving
Iterations:
1. For loop
2. While loop
END
16
Introduction to Computer Problem Solving
The above flowchart is to print all natural numbers up to n .The user provides
the input value n. The first value, i is initialized as one. A loop is initialized. The i
value is checked with n, the user input. The numbers are printed until the value is less
than the user input value. When the condition becomes false the loop terminates.
17
Introduction to Computer Problem Solving
Recursion:
Example: Factorial
RETURN fact=n*factorial(n-1)
18
Introduction to Computer Problem Solving
The flowchart 1.9a is to call the function factorial. This function is called as
Main function or calling function. The flowchart1.5b is called as the called function
which is to find the factorial of the number passed from the main function. The loop
exists until the n value becomes one. Factorial is a recursive function where the
function itself is called again and again.
19
Introduction to Computer Problem Solving
The following are some of the factors which helps in an analyzing an algorithm
Efficiency
Simplicity
Generality
Range of Growth
Computing order of growth
Time Complexity
20
Introduction to Computer Problem Solving
Time complexity can be represented as a numerical function T(n), where T(n) can be
measured as the number of steps, provided each step consumes constant time.
For example
int sum(int a[],int n)
{
Int sum=0
For (int i=0;i<n;i++)
{
Sum=sum+a[i]
}
Return sum
}
21
Introduction to Computer Problem Solving
The program must be of lesser size in a multiuser system ,since there will be
multiple copies of the same program
To compute the space we require two components – Variable space and fixed space.
Where C is a Constant, that is the fixed space and it denotes the amount of space
taken by instructions, simple variables, constants and identifiers.
Space complexity can also be represented as the sum of instruction space, Data
space and Environment stack space.
Instruction space: The memory space required for storing the compiled
instructions. It depends on the Compiler used, compiler options and the target
computer
22
Introduction to Computer Problem Solving
Type Size
bool, char, unsigned char, signed char, int8 1 byte
int16, short, unsigned short 2 bytes
float, int32, int, unsigned int, long, unsigned long 4 bytes
double, int64, long double 8 bytes
Examples:
int c= a+b
return c
In the above example, variables a, b, and c are all integer types, so they will take up 4
bytes each, so total memory requirement will be (4(3) + 4) = 20 bytes, the additional
4 bytes is for return value. Since this space requirement is fixed for the above
example, it is called Constant Space Complexity.
Int sum=0
Sum=sum+a[i]
Return sum
23
Introduction to Computer Problem Solving
In the above example, 4*n bytes of space is required for the array a[]
n elements.
Hence the total memory requirement will be (4n + 12), which is increasing linearly
with the increase in the input value n, hence it is called as Linear Space Complexity.
Case Studies
Networked Healthcare
Smart Cities
24
Introduction to Computer Problem Solving
QUESTIONS :
1. Write an algorithm and flowchart for solving quadratic equation (of the
form ax2 + bx + c).
2. M1,M2, M3 are the marks scored in 3 tests. Write an algorithm to find the
average of best 2 marks.
3. Write an algorithm and pseudo code to check whether a given integer is
prime number or not.
4. Write a recursive function to add two numbers a and b.
5. Describe the algorithm for finding sum and average of n numbers.
6. Draw a flow chart for computing factorial of a given number.
7. Draw the flow chart for finding sum of first n natural numbers.
8. Draw a flow chart print all prime number between to intervals.
9. Explain the algorithm to find minimum in a list and Maximum in a list.
10. Develop an algorithm for conversion from Celsius to Fahrenheit and vice
versa.
25