Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

python test 1

Uploaded by

Vidhya Gopinath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python test 1

Uploaded by

Vidhya Gopinath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING

Year/Sem :I / I TEST -1 QUESTION AND ANSWER

UNIT – 1 - COMPUTATIONAL THINKING AND PROBLEM SOLVING PART A


1 Write an algorithm to find smallest among three numbers. ( April/may 2022)
Step 1: Start
Step 2: Read the three numbers A, B, C Step 3: Compare A and B.
If A is minimum, go to step 4 else go to step 5.
Step 4: Compare A and C.
If A is minimum, output “A is minimum” else output “C is minimum”. Go to step 6.
Step 5: Compare B and C.
If B is minimum, output “B is minimum” else output “C is minimum”.
Step 6: Stop
2 Write an algorithm to accept two numbers, perform the sum and print the result.( Jan 2022)
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
3 List the symbol used in drawing the flowchart ( April/may 2019)

4 Distinguish between algorithm and program. (DEC/JAN 2019)


An algorithm is a systematic logical approach used to solve problems in a
computer while pseudo code is the statement in plain English that may be
translated later to a programming language. Pseudo code is the intermediary
between algorithm and program.
5 Write an algorithm to find the minimum number in the given list of number. (DEC/JAN
2019)
1. Assign the first value of the list as minimum
2. Compare this value to the other values starting from second value
3. When a value is smaller than the present minimum value, then it becomes the new
minimum.
4. Continue the process recursively
5. Print the minimum value.
6 What is an Algorithm
Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for
completing a task. It is an English-like representation of the logic which is used to
solve
the problem. It is a step- by-step procedure for solving a task or a problem. The steps
must be ordered, unambiguous and finite in number.
7 Write down the characteristics of algorithm.
 Algorithm should be precise and unambiguous.
 Instruction in an algorithm should not be repeated infinitely.
 Ensure that the algorithm will ultimately terminate.
 Algorithm should be written in sequence.
 Algorithm should be written in normal English.
 Desired result should be obtained only after the algorithm terminates.
8 Write an pseudo-code to two accept to numbers, add the numbers and print the result (Jan
17)
Begin.
WRITE “Please enter two numbers to add”
READ num1.
READ num2.
Sum = num1+num2.
WRITE Sum.
End.
9 How will you analyze the efficiency of algorithm ? (Nov/Dec 2019)
Suppose X is an algorithm and n is the size of input data, the time and space used by the
algorithm X are the two main factors, which decide the efficiency of X
 Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
 Space Factor − Space is measured by counting the maximum memory space required
by the algorithm.

10 What is the use of algorithm ? Flowchart and Pseudo code in the perspective of problem
solving (Nov/Dec 2019)
i. Simple to understand
ii. Easy to debug
iii. Independent of programming language
iv. Each step can be easy to written in high-level language.
11 What are the factors used to judge the quality of an algorithm ? (Nov/Dec 2020)
I. Accuracy
II. Memory
III. Time
IV. Sequence
V. Result
12 What is control flow? List and define the ways of execution of control. (Nov/Dec 2020)
Control flow (or flow of control) is the order in which individual statements, instructions or
function calls of an imperative program are executed or evaluated. A control flow statement
is a statement in which execution results in a choice being made as to which of two or
more paths to follow.
i. Sequential control flow
ii. Selection control flow
iii. Iterative control flow
13 List the building blocks of algorithm?
The building blocks of an algorithm are
 Statements
 Sequence
 Selection or Conditional
 Repetition or Control flow
 Functions
14. Give the rules for writing Pseudo codes.
 Write one statement per line.
 Capitalize initial keywords.
 Indent to show hierarchy.
 End multiline structure.
 Keep statements to be language independent.
15 Give the difference between flowchart and pseudo code.
Flowchart and Pseudo code are used to document and represent the algorithm. In other
words, an algorithm can be represented using a flowchart or a pseudo code. Flowchart
is a graphical representation of the algorithm. Pseudo code is a readable, formally styled
English like language representation of the algorithm.

PART-B
1 i. Define flow chart? Draw flowchart and write an algorithm to accept three
distinct numbers and find the greatest, print the result (Nov/Dec 2022)
A flowchart is a diagrammatic representation of the logic for solving a task. A
flowchart is drawn using boxes of different shapes with lines connecting them
to show the flow of control. The purpose of drawing a flowchart is to make the
logic of the program clearer in a visual form.

FLOECHART:

ALGORITHM:
Step 1: Start
Step 2: Read the three numbers A, B, C Step 3: Compare A and B.
If A is minimum, go to step 4 else go to step 5.
Step 4: Compare A and C.
If A is minimum, output “A is minimum” else output “C is minimum”. Go to step 6.
Step 5: Compare B and C.
If B is minimum, output “B is minimum” else output “C is minimum”.
Step 6: Stop
ii. Outline the algorithm to display the first n odd numbers(Apr/May 2019)
2 I. Draw a flowchart AND algorithm to find the sum of the series 1+2+3…..
+100 (Nov/Dec 2022)
II.What is an algorithm? Summarize the characteristics of a good algorithm.
(Apr/May 2019)
I. FLOWCHART

ALGORITHM:
Read n value as 100.
declare k variable.
Uss formula n*(n+1)/2 to get sum of first 100 natural numbers and hold it in the
variable k.
print k.

II. ALGORITHM:

Algorithm is an ordered sequence of finite, well defined, unambiguous


instructions for completing a task. It is an English-like representation of the
logic which is used to solve
the problem. It is a step- by-step procedure for solving a task or a problem. The
steps must be ordered, unambiguous and finite in number.

CHARECTERISTICS:
 Algorithm should be precise and unambiguous.
 Instruction in an algorithm should not be repeated infinitely.
 Ensure that the algorithm will ultimately terminate.
 Algorithm should be written in sequence.
 Algorithm should be written in normal English.
 Desired result should be obtained only after the algorithm terminates.
3 What is a programming language ? What are its types ? Explain in detail with
their advantages and disadvantages (Nov/Dec 2019)
• A programming language is a set of symbols and rules for instructing a computer
to perform specific tasks.
• The programmers have to follow all the specified rules before writing program
using programming language.
• The user has to communicate with the computer using language which it can
understand

Types of programming language

1. Machine language
2. Assembly language
3. High level language
MACHINE LANGUAGE:
The computer can understand only 0’s and 1’s.
• In machine language the different instructions are formed by taking different
combinations of 0’s and 1’s.
Advantages:
Translation free:
• For executing any program written in any programming language, the conversion
to machine language is necessary.
• The conversion process is not required.
High speed
• The machine language program is translation free.
• Conversion time is saved
the execution of machine language program is extremely fast
Disadvantage:
• It is hard to find errors in a program written in the machine language.
• Writhing program in machine language is a time consuming process.
Machine dependent:
• According to architecture used, the computer differs from each other.
• Machine language differs from computer to computer.
• Program developed for a particular type of computer may not run on other type
of computer.
ASSEMBLY LANGUAGE:
• To overcome the issues in programming language and make the programming
process easier, an assembly language is developed which is logically
equivalent to machine language but it is easier for people to read, write and
understand.
• Assembly languages are symbolic programming language that uses symbolic
notation to represent machine language instructions.
• They are called low level language because they are so closely related to the
machines.
ASSEMBLER
Assembler is the program which translates assembly language instruction in to a
machine language.
• Easy to understand and use.
• It is easy to locate and correct errors.

Disadvantage
1. Machine dependent
The assembly language program which can be executed on the machine depends
on the architecture of that computer.
2. Hard to learn
It is machine dependent, so the programmer should have the hardware
knowledge to create applications using assembly language.
3. Less efficient
Execution time of assembly language program is more than machine language
program.
Because assembler is needed to convert from assembly language to machine
language.
HIGH LEVEL LANGUAGE
High level language contains English words and symbols.
The specified rules are to be followed while writing program in high level
language.
The interpreter or compilers are used for converting these programs in to
machine readable form.
Translating high level language to machine language
Compiler:
• A compiler is a program which translates the source code written in a high level
language in to object code which is in machine language program.
• Compiler reads the whole program written in high level language and translates
it to machine language.
• If any error is found it display error message on the screen.
Interpreter
• Interpreter translates the high level language program in line by line manner.
• The interpreter translates a high level language statement in a source program to
a machine code and executes it immediately before translating the next
statement.
• When an error is found the execution of the program is halted and error message
is displayed on the screen.
Advantages
Readability
• High level language is closer to natural language so they are easier to learn and
understand
Machine independent
• High level language program have the advantage of being portable between
machines.
Easy debugging
• Easy to find and correct error in high level language
Disadvantages
Less efficient
• The translation process increases the execution time of the program. Programs
in high level language require more memory and take more execution time to
execute.
They are divided into following categories:
Interpreted programming languages
• Functional programming languages
• Compiled programming languages
• Procedural programming languages
• Scripting programming language
• Markup programming language
• Concurrent programming language
• Object oriented programming language
4 Outline the algorithm, flowchart and pseudocode to display the first n odd
numbers(Apr/May 2019)
ALGORITHM:
Step 1: Start

Step 2: Declare variable c of integer type

Step 3: Set c=0

Step 4: Repeat step 4.1 to 4.3 while (c<=100)

Step 4.1: if (c%2 != 0)

Step 4.2: then print c

Step 4.3 : c=c+1

Step 5: Stop

FLOWCHART:
PSEUDOCODE:
1. INPUT n.
2. remainder = n % 2.
3. IF remainder is not equal to 0.
4. answer = odd.
5. ELSE.
6. answer = even.
7. OUTPUT answer.

5 Discuss the building blocks of an algorithm(Nov/Dec 2019)


BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
Algorithms can be constructed from basic building blocks namely, sequence, selection
and iteration.
Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
Ø input data-information given to the program
Ø process data-perform operation on a given input
Ø output data-processed result
State:
Transition from one process to another process under specified condition with in a time
is called state.
Control flow:
The process of executing the individual statements in a given order is called control
flow.
The control can be executed in three ways
1. sequence
2. selection
3. iteration
Sequence:
All the instructions are executed one after another is called sequence execution.

Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop

Selection:
A selection statement causes the program control to be transferred to a specific part of
the program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop

Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called
looping or iteration.

Example

Write an algorithm to print all natural numbers up to n


Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop

Functions:

v Function is a sub program which consists of block of code(set of instructions) that


performs a particular task.
v For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.

Benefits of Using Functions

v Reduction in line of code


v code reuse
v Better readability
v Information hiding
v Easy to debug and test
v Improved maintainability
Example:

Algorithm for addition of two numbers using function


Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop

sub function add()


Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return

Def add(a,b):
Sum=a+b
Return sum
Num1=10
Num2=20
Print(‘the sum is’,add(num1,num2))
6 Draw flowchart to print the first n prime numbers(Apr/May 2017)
IS THIS NUMBER PRIME:

You might also like