Beruflich Dokumente
Kultur Dokumente
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.
10
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 .
11
12
13
14
15
16
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 the
means.
17
18
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.
19
In a flowchart, we always need to ensure that all possible branches of a condition are
covered.
20
21
22
23
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.
24
25
; READ CHOICE
; BRANCH TO APPROPRIATE
; MODULE DEPENDING ON
; THE CHOICE
26
JZ VW_FILE
MOV AX, 4C00H
INT 21H
; EXIT PROGRAM
26
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.
27
28
29
30
31
32
33
34
35
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.
36
37
38
Answer:
Greedy algorithm
Transform-and-conquer
39
40
41
42
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
43
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.
44
45
46
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.
47
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.
48
49
50
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.
51
52
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 or b >= 20.
53
54
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 is
during the program design that we decide about such flow based on the
requirements of the program.
55
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 similar
situations.
'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.
56
57
58
59
60
61
62
63
64
65
66
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.
67
68
69
70
71
Use do .. while loop when you need to run through the loop at least once like
showing a list menu of menu items.
72
73
74
75
76
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.
77
78
79
80
Answer: No output
81
82
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 are
not 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.
83
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).
84
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 not
give 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.
85
86
87
88
89
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.
90
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.
91
92
93
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-user
using 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)
94
95
Answer: 10
96
97
98
99
100
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 a
change 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.
101
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.
102
103
104
Not all programming languages may have a separate subprogram type called
subroutine. However, subroutine and function are referred in a similar context in
general.
105
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.
106
107
108
109
110
Answer: false
111
112
113
114
115
116
117
118
119
120
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.
121
122
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.
123
124
125
126
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 the
problem 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.
127
128
129
130
131
132
133
134
Answer:
int c = a + b;
for (i = 0; i < c; i++)
myTbl[i] = yrTbl[i] * c;
135
Answer: break;
136
137
138
139
140