Upload
mono-hari
View
226
Download
0
Embed Size (px)
Citation preview
7/25/2019 1.Structured Programming(141)
1/141
7/25/2019 1.Structured Programming(141)
2/141
7/25/2019 1.Structured Programming(141)
3/141
We often think that a computer program is nothing but a set of instructions. But it may not be true. It
does not do justice to the involved complexity of this simple idea. The idea of a program exists outside
any particular programming language, but we only get to see and experience it when it is expressed.
This makes it hard to separate the fundamental ideas from language features.
Given that a program is merely a list of instructions, one might think that all the fuss we make about it
is a little unjustified. However this is to underrate the complexity of this apparently simple idea. List of
instructions start out being simple. All you have to do is write down what is to be done at each step.
You start at step one, obey it and then move to step two. When step two is complete you move to
step three and so on.
The language, using which the program is written, is usually a little more precise than standard English
but it is nothing special in most cases it could usually do with being more precise but thats a
problem of another sort. The instructions are the program, the model builder is the computer and you
could even think of the actual model itself as the data that the program manipulates.
7/25/2019 1.Structured Programming(141)
4/141
7/25/2019 1.Structured Programming(141)
5/141
7/25/2019 1.Structured Programming(141)
6/141
7/25/2019 1.Structured Programming(141)
7/141
7/25/2019 1.Structured Programming(141)
8/141
7/25/2019 1.Structured Programming(141)
9/141
7/25/2019 1.Structured Programming(141)
10/141
7/25/2019 1.Structured Programming(141)
11/141
One of the most important aspects of problem solving is to clearly understand the
problem so that we can provide a correct solution. Asking the right set of questions is
the first step in getting the problem statement clear.
Once the problem is understood clearly, we can look for similar problems solved
elsewhere earlier so that the current problem also can be solved easily and quickly.
Analogy is an inference that, if things agree in some respects they probably agree in
others. Drawing a comparison in order to show similarity in some respect. E.g. the
working of the brain presents an interesting analogy to understand the operation of a
computer.
Means-ends analysis involves application of systems thinking .
7/25/2019 1.Structured Programming(141)
12/141
Means-Ends Analysis (MEA): Application of systems thinking to planning whereby
the overall goal is broken down into objectives which in-turn are broken down into
individual steps or actions. MEA is based on the concept that 'every attainable end is
in itself a means to a more general end'.
7/25/2019 1.Structured Programming(141)
13/141
7/25/2019 1.Structured Programming(141)
14/141
The order of steps in an algorithm is also very important.
7/25/2019 1.Structured Programming(141)
15/141
7/25/2019 1.Structured Programming(141)
16/141
7/25/2019 1.Structured Programming(141)
17/141
Note algorithm and flowchart are a different means to solve a problem. Since
flowchart is diagrammatic, users may find it more easier to understand than its
equivalent algorithm. Either an algorithm or flowchart is required to solve the
problem, not both. Objective is to solve the problem effectively rather than themeans.
7/25/2019 1.Structured Programming(141)
18/141
7/25/2019 1.Structured Programming(141)
19/141
Imagine a situation where step 1, 2 & 3 are mixed up. Will you get the expected
output? No. Hence it's very important that the steps are written in a proper sequence
to get the required output.
7/25/2019 1.Structured Programming(141)
20/141
In a flowchart, we always need to ensure that all possible branches of a condition are
covered.
7/25/2019 1.Structured Programming(141)
21/141
7/25/2019 1.Structured Programming(141)
22/141
General approaches to the construction of efficient solutions to problems are of
interest because:
> They provide templates suited to solving a broad range of diverse problems.> They can be translated into common control and data structures provided by most
high-level languages.
> The temporal and spatial requirements of the algorithms which result can be
precisely analyzed.
Although more than one technique may be applicable to a specific problem, it is
often the case that an algorithm constructed by one approach is clearly superior to
equivalent solutions built using alternative techniques.
7/25/2019 1.Structured Programming(141)
23/141
7/25/2019 1.Structured Programming(141)
24/141
While algorithms are used for solving the problems from the user's perspective, it
cannot be directly fed or programmed into the computer for asking it to provide the
solutions. They are written in plain English which a normal human can understand.
In order to make a computer understand the algorithm, we need to program it in its
language. A programming language is a high-level language used by programmers to
write programs which then gets converted into machine language that the computer
can understand and execute.
7/25/2019 1.Structured Programming(141)
25/141
7/25/2019 1.Structured Programming(141)
26/141
; Example: Assembly language
; AX, DS, etc. are CPU registers
START :
MOV AX, @DATAMOV DS, AX
BEGIN :
MOV AX, 3H ; CLEAR SCREEN BY
INT 10H ; CHANGING VIDEO MODE
CALL DISP2
OPTION:
LEA DX,MSG32
CALL DISP1
CALL READCH ; READ CHOICE
AND AL, 0FH
CMP AL, 1 ; BRANCH TO APPROPRIATE
JNZ A ; MODULE DEPENDING ON
JMP CR_FILE ; THE CHOICE
A : CMP AL, 3
7/25/2019 1.Structured Programming(141)
27/141
JZ VW_FILE
MOV AX, 4C00H ; EXIT PROGRAM
INT 21H
7/25/2019 1.Structured Programming(141)
28/141
Power of high-level language (E.g. C, Java) is that they provide a few set of keywords
using which any complex problem can be solved.
7/25/2019 1.Structured Programming(141)
29/141
7/25/2019 1.Structured Programming(141)
30/141
Java is compiled to an intermediate "byte code" at compilation time. This is in
contrast to a language like C that is compiled to machine language at compilation
time. The Java byte code cannot be directly executed on hardware the way that
compiled C code can. Instead the byte code must be interpreted by the JVM (JavaVirtual Machine) at runtime in order to be executed. The primary drawback of a
language like C is that when it is compiled, that binary file will only work on one
particular architecture (e.g. x86). However with Java, given byte code can be
executed on any machine architecture provided appropriate is JVM is installed on that
computer.
7/25/2019 1.Structured Programming(141)
31/141 3
7/25/2019 1.Structured Programming(141)
32/141 3
7/25/2019 1.Structured Programming(141)
33/141 3
7/25/2019 1.Structured Programming(141)
34/141 3
7/25/2019 1.Structured Programming(141)
35/141 3
7/25/2019 1.Structured Programming(141)
36/141 3
7/25/2019 1.Structured Programming(141)
37/141
During the maintenance phase, apart from enhancing already implemented programs
which may arise due to changing business scenarios, some defects which might have
gone undetected during the testing phase also may be fixed.
3
7/25/2019 1.Structured Programming(141)
38/141
Answer: Keep changing the problem statement
3
7/25/2019 1.Structured Programming(141)
39/141
Answer: Includes set of implicit steps
3
7/25/2019 1.Structured Programming(141)
40/141
Answer: Greedy algorithm
Transform-and-conquer
3
7/25/2019 1.Structured Programming(141)
41/141
Answer: Non-procedural language
4
7/25/2019 1.Structured Programming(141)
42/141 4
7/25/2019 1.Structured Programming(141)
43/141 4
7/25/2019 1.Structured Programming(141)
44/141
Identifiers or variable names can contain alphabets, digits and few special characters
like $ and _ (underscore). Some programming languages may allow other special
characters also. COBOL allows minus (-) sign in variable names. However, no variable
name can start with a number and have white space (blank, tab or newline character)in them. Variable names may have maximum length which is again dependent on the
programming language. Some valid variable names:
account_number
fixed_price1
customerName
Invalid variable names:
1password
product id
ticket-price
4
7/25/2019 1.Structured Programming(141)
45/141
First step in creating appropriate data is to know what kind of data to create i.e. data
literacy. Create variables based on the need. Creating too many and not using them
later is not going to help solve the problem easily.
4
7/25/2019 1.Structured Programming(141)
46/141 4
7/25/2019 1.Structured Programming(141)
47/141 4
7/25/2019 1.Structured Programming(141)
48/141
The main problem with persistence arises when you assume that a variable has a
longer persistence than it really does. Shorter the persistence the better it is for the
program.
4
7/25/2019 1.Structured Programming(141)
49/141
The time at which the variable and its value are bound together is the binding time of
the variable. Later the binding time, better the programming. Binding while writing
the code should be avoided for it hard-codes the values in the program; it induces
inflexibility into the program.Assignment during compile time e.g. Assignment of macro values, constants are done
during compile time.
4
7/25/2019 1.Structured Programming(141)
50/141 4
7/25/2019 1.Structured Programming(141)
51/141 5
7/25/2019 1.Structured Programming(141)
52/141
Naming a variable too short like i, j, k, etc. should be avoided; on the other hand
having too long a name also (e.g. customers_savings_bank_account_number) would
pose difficulty during programming and maintenance phase later.
It is very important that we follow appropriate naming convention (as set by the
project) for better programming.
5
7/25/2019 1.Structured Programming(141)
53/141 5
7/25/2019 1.Structured Programming(141)
54/141
Some compilers like C and Java are smart enough while evaluating expression
involving such logical operators. For e.g. in comparisons like "if a > 10 AND b < 20", if
the value of a is 10 or less, it will not go to evaluate the expression if b < 20 because,
one 'false' condition in an AND operator, will result in whole expression evaluating to'false'. It's like multiplying many numbers with zero, answer is always zero.
Similar explanation holds good for OR also. In expression, if a > 10 OR b < 20, if value
of a is more than 20, the whole expression becomes true irrespective of whether b = 20.
5
7/25/2019 1.Structured Programming(141)
55/141 5
7/25/2019 1.Structured Programming(141)
56/141
It is quite unlikely that a program will always have a sequential flow i.e. executing
each statement starting from line #1 one-by-one till the end. Based on the various
conditions and situations, few statements get executed many times, few of them may
get skipped, few of them may never get executed (e.g. rare error conditions). It isduring the program design that we decide about such flow based on the
requirements of the program.
5
7/25/2019 1.Structured Programming(141)
57/141
Conditional constructs are one of the very important constructs in a program that
make a make-or-break kind of decision. Some examples that might require if else
ladder are deciding the grade based on the marks range, deciding the tax based on
the income range, increment calculation based on no. of years of service and similarsituations.
'case' statement replaces multiple 'if' statements provided they use equality
operators (if a = 1 , if a = 2, ). Some examples include: menu selection, salary
decision based on employee grade, tax calculation based on state code , etc.
5
7/25/2019 1.Structured Programming(141)
58/141 5
7/25/2019 1.Structured Programming(141)
59/141 5
7/25/2019 1.Structured Programming(141)
60/141 5
7/25/2019 1.Structured Programming(141)
61/141
7/25/2019 1.Structured Programming(141)
62/141
7/25/2019 1.Structured Programming(141)
63/141
7/25/2019 1.Structured Programming(141)
64/141
7/25/2019 1.Structured Programming(141)
65/141
7/25/2019 1.Structured Programming(141)
66/141
7/25/2019 1.Structured Programming(141)
67/141
7/25/2019 1.Structured Programming(141)
68/141
Looping construct is the one which makes the users to get the best use of power of a
computer. Based on the given value the program can be made to execute the same
set of instructions (possibly with different values) 10, 100, 1000, 10000 and even
beyond!
However, one side effect of looping is that it may go for 'infinite loop' if the loop exit
condition is not properly checked so that the loop never ends. This however can be
solved with little more analysis, debugging the program and taking experts' help.
7/25/2019 1.Structured Programming(141)
69/141
7/25/2019 1.Structured Programming(141)
70/141
7/25/2019 1.Structured Programming(141)
71/141 7
7/25/2019 1.Structured Programming(141)
72/141 7
7/25/2019 1.Structured Programming(141)
73/141
Use do .. while loop when you need to run through the loop at least once like
showing a list menu of menu items.
7
7/25/2019 1.Structured Programming(141)
74/141 7
7/25/2019 1.Structured Programming(141)
75/141 7
7/25/2019 1.Structured Programming(141)
76/141 7
7/25/2019 1.Structured Programming(141)
77/141
Entering the loop:
Enter from one location
Put initialization code before the loop
Decide between for and while loops
Inside the loop
Use bracket sets to group the loop statements even if there is only one statement
in the loop
Avoid empty loops
Keep loop housekeeping part either at the beginning or end of the loop (E.g.
increment, etc.)
Assure that the loop ends!
Do not play around with for loop index values
Exiting the loop
Use break statements if it has to be exited early
May have to use multiple break statements in case of nested loops
Use continue to skip the loop for certain iterations
Use extra caution while using break and continue statements
7
7/25/2019 1.Structured Programming(141)
78/141
Since 'goto' makes the control to flow unconditionally to any location inside the
program, it's considered as an unprofessional programming practice in languages like
C, C++, etc. However, it would be very difficult to write FORTRAN programs without
using gotos.
7
7/25/2019 1.Structured Programming(141)
79/141
Answer: Its life span
7
7/25/2019 1.Structured Programming(141)
80/141
Answer:what data gets stored in it
7
7/25/2019 1.Structured Programming(141)
81/141
Answer: it is an infinite loop
8
7/25/2019 1.Structured Programming(141)
82/141
Answer: No output
8
7/25/2019 1.Structured Programming(141)
83/141 8
7/25/2019 1.Structured Programming(141)
84/141
Arrays are used for storing homogeneous data. E.g. marks of students, employee
numbers, salaries of employees, names, etc. Array name is the index to the first
element of the array. Such arrays are called static arrays. They have limitation that
their sizes needs to be specified while declaring them. If all locations of an array arenot used, the memory space consumed by them is going to be wasted and cannot be
used by other programs or variables till the program exits. Hence the program has to
be designed in such a way to make efficient use of all elements of an array.
Programs involving arrays usually have looping statements to process the array
elements. Loop will execute as many no. of times as the no. of elements in the array,
to process each element in every iteration.
8
7/25/2019 1.Structured Programming(141)
85/141
It's during the declaration of the array, the memory space is allocated to it. An integer
of 100 elements would require 200 bytes of memory space (assuming each integer
requires 2 bytes of space).
8
7/25/2019 1.Structured Programming(141)
86/141
In this case numArray with 5 elements will be initialized with those values. Note that
in most of the programming languages the array subscript i.e. the index to access
individual array element, starts from 0. Hence array of size N, will have the max index
N 1. Accessing the array element beyond this index may result in an error or will notgive the expected result.
Initializing the array during declaration itself is a good practice for it requires a
looping structure to initialize it at later time. Programming language C, Java, etc. have
library routines to initialize array elements without using loop statements. However,
initializing the array during declaration is better.
8
7/25/2019 1.Structured Programming(141)
87/141 8
7/25/2019 1.Structured Programming(141)
88/141
As an example, a single-dimensional array may be used to store marks of a student
stores scored in various subjects. Similarly, a two-dimensional array may be used to
store marks of multiple students in various subjects. Continuing with this example, a
three-dimensional array can be created to store marks of multiple students in varioussubjects in different examinations. However, going beyond this will make the program
more complex and very difficult to maintain and understand.
8
7/25/2019 1.Structured Programming(141)
89/141
Though multi-dimensional array is shown in a tabular structure, its elements however
are stored in sequential manner in the memory. Specific element can be accessed by
an arithmetic expression using its row and column size.
Element (i, j) can be accessed using expression, i * column size + j.
For e.g. referring to the 2-dimensional array above whose column size is 4,
numArray[3][2] = 3 * 4 + 2 = 14th element starting from 0 i.e. 5 (last row, column
subscript [2]).
8
7/25/2019 1.Structured Programming(141)
90/141 8
7/25/2019 1.Structured Programming(141)
91/141
While static arrays require their size to be decided during compile time (i.e. while
creating the program), a pointer makes it dynamic so that the size can be decided
during run time.
The limitation of static arrays, their size to be decided during declaration, is overcome
by pointers. They can be allocated allocate memory dynamically during run time (late
binding). Once the work is complete, the memory can be de-allocated i.e. released
back to the system. This again is a limitation in static arrays, which cannot be done
unless the program exits.
9
7/25/2019 1.Structured Programming(141)
92/141
Pointer can be similar to a librarian having an index of book titles along with their
location i.e. shelf no. on which it is kept. If somebody asks for a particular book, she
can refer to the index, go to the exact shelf where the book is placed to access it. It
will save a lot of time that would otherwise have spent in searching for it arbitrarily.In this case, each index is pointing to exactly one location. If there is any change in
the library layout, librarian will have to change the index values of books relocated.
9
7/25/2019 1.Structured Programming(141)
93/141 9
7/25/2019 1.Structured Programming(141)
94/141 9
7/25/2019 1.Structured Programming(141)
95/141
You would typically ask a plumber who fixed your piping problem in your home, the
exact cause of the problem. It would help avoid the same problem recurring in future.
Just an explanation of 'there was some problem' would not satisfy you. Similarly, it's
very important that we communicate the exact error in the program to the end-userusing appropriate language. (Just like a doctor explaining the patient about an
operation that he is going to perform on the patient without using complex medical
terms, still making the patient understand what is the actual problem and why it's
required)
9
7/25/2019 1.Structured Programming(141)
96/141
Answer: countArray[4][3] = 43;
9
7/25/2019 1.Structured Programming(141)
97/141
Answer: 10
9
7/25/2019 1.Structured Programming(141)
98/141
Answer: Pointer is uninitialized
9
7/25/2019 1.Structured Programming(141)
99/141 9
7/25/2019 1.Structured Programming(141)
100/141 9
7/25/2019 1.Structured Programming(141)
101/141
Library routines provided by high-level languages hide implementation details;
programmers do not get to see their implementation details (which is certainly not
required). Appropriate inputs and outputs are defined so as to make their appropriate
use.
10
7/25/2019 1.Structured Programming(141)
102/141
Think of a routine as a piece in a jig-saw puzzle. There are common pieces and there
are specific ones which fit the corners. A routine is independent of the caller i.e. any
changes done in the calling program will not result in changes to the routine.
Similarly, changes in the implementation details of a routine may not require achange in the calling program.
Routines should be robust enough to handle error situations like invalid inputs or data
errors. Errors in the routines should not abruptly end the program. They should be
appropriately communicated to the calling program so that the calling program would
take appropriate action based on the severity of the error.
10
7/25/2019 1.Structured Programming(141)
103/141
In languages like C, C++, Java, etc. every subprogram is a function (or method). They
have to return a value at the end or they should be declared as 'void functionName()'
if they do not return any value.
10
7/25/2019 1.Structured Programming(141)
104/141 10
7/25/2019 1.Structured Programming(141)
105/141 10
7/25/2019 1.Structured Programming(141)
106/141
Not all programming languages may have a separate subprogram type called
subroutine. However, subroutine and function are referred in a similar context in
general.
10
7/25/2019 1.Structured Programming(141)
107/141
Parameters provide the interface to the function i.e. they make the function more
generic. Carrying out same set of operations but on a different data set. For e.g.
salary calculation method is same for all employees; however, inputs will differ for
every employee.
Having too many parameters to a function would make it difficult to call and
maintain, on the other hand having too less or no parameters may make it more
specific. This decision has to be taken during design time based on the requirements
of the function.
10
7/25/2019 1.Structured Programming(141)
108/141 10
7/25/2019 1.Structured Programming(141)
109/141 10
7/25/2019 1.Structured Programming(141)
110/141 10
7/25/2019 1.Structured Programming(141)
111/141
Answer: none of these.
1
7/25/2019 1.Structured Programming(141)
112/141
Answer: false
1
7/25/2019 1.Structured Programming(141)
113/141
Answer: pass by reference
1
7/25/2019 1.Structured Programming(141)
114/141 1
7/25/2019 1.Structured Programming(141)
115/141 1
7/25/2019 1.Structured Programming(141)
116/141 1
7/25/2019 1.Structured Programming(141)
117/141
An interactive Calculator program may be designed to have independent functions to
carry out required mathematical function. Advantage with this is that, functions can
be independently modified without causing major impact to any other modules.
Addition of new function (for e.g. square root) can also done easily.
1
7/25/2019 1.Structured Programming(141)
118/141 1
7/25/2019 1.Structured Programming(141)
119/141
Answer: All of these
1
7/25/2019 1.Structured Programming(141)
120/141 1
7/25/2019 1.Structured Programming(141)
121/141 1
7/25/2019 1.Structured Programming(141)
122/141
Naming convention: Using variable name minimum_balance rather than mb. Using
function name calculate_interest rather than calcint
Indentation: Aligning code lines as per the block / loop they belong to. (Refer to
example programs)Comments: Enough lines of appropriate comments to make the program more
understandable. Assume it's part of the code as if without it compiler may give an
error.
Declarations: Logically grouping declarations and initializations.
Statements: Logically grouping the statements. Not too long statement lines. Wrap to
next line if the line goes beyond 80-90 characters to avoid horizontal scrolling.
Whitespaces: Space, tab & new lines. To logical group things in the program. Do not
too many unnecessary whitespaces.
1
7/25/2019 1.Structured Programming(141)
123/141 1
7/25/2019 1.Structured Programming(141)
124/141
Useful error messages: As an example, if the user has entered invalid CVV (Card
Verification Value) code for his credit card no., it's logical to display error message
saying, "You've entered invalid CVV code" rather than saying "Error in credit card no.".
Dead code are such code lines which never get executed. While most of the
compilers nowadays detect them and give warning messages about it, they may not
be able to do so if it's due to some logical error. For e.g. condition if (loggedInUserId
= "") may never be true if this condition is checked in a module which would get
executed once the user logs into the application.
1
7/25/2019 1.Structured Programming(141)
125/141 1
7/25/2019 1.Structured Programming(141)
126/141 1
7/25/2019 1.Structured Programming(141)
127/141 1
7/25/2019 1.Structured Programming(141)
128/141
In order to reach your destination very quickly in a metropolitan city rushing through
a heavy traffic, you need to start early or plan accordingly. None of the tricks of
showing different driving stunts, honking, overtaking, blocking others, etc. will take
you to the destination on time if you've started late. In fact, it would make theproblem much worse.
This applies to code tuning also. Clear and specific requirements, followed by an
effective design, are a good prerequisites to proper coding. The programmer should
make the best of use of good design in writing the effective code. It's the
programmer's responsibility to convert this design into a correct, tuned code.
1
7/25/2019 1.Structured Programming(141)
129/141 1
7/25/2019 1.Structured Programming(141)
130/141 1
7/25/2019 1.Structured Programming(141)
131/141 13
7/25/2019 1.Structured Programming(141)
132/141 13
7/25/2019 1.Structured Programming(141)
133/141 13
7/25/2019 1.Structured Programming(141)
134/141 13
7/25/2019 1.Structured Programming(141)
135/141 13
7/25/2019 1.Structured Programming(141)
136/141
Answer:int c = a + b;
for (i = 0; i < c; i++)
myTbl[i] = yrTbl[i] * c;
13
7/25/2019 1.Structured Programming(141)
137/141
Answer: break;
13
7/25/2019 1.Structured Programming(141)
138/141
Answer: Make sure that the design is correct
13
7/25/2019 1.Structured Programming(141)
139/141 13
7/25/2019 1.Structured Programming(141)
140/141 13
7/25/2019 1.Structured Programming(141)
141/141