Sie sind auf Seite 1von 141

1

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

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'.

12

13

The order of steps in an algorithm is also very important.

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

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.

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

; Example: Assembly language


; AX, DS, etc. are CPU registers
START :
MOV AX, @DATA
MOV DS, AX
BEGIN :
MOV AX, 3H
; CLEAR SCREEN BY
INT 10H
; CHANGING VIDEO MODE
CALL DISP2
OPTION:
LEA DX,MSG32
CALL DISP1
CALL READCH
AND AL, 0FH
CMP AL, 1
JNZ A
JMP CR_FILE
A : CMP AL, 3

; 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

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 (Java
Virtual 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.

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

Answer: Keep changing the problem statement

37

Answer: Includes set of implicit steps

38

Answer:

Greedy algorithm
Transform-and-conquer

39

Answer: Non-procedural language

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

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

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

Answer: Its life span

78

Answer:what data gets stored in it

79

Answer: it is an infinite loop

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

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 various
subjects in different examinations. However, going beyond this will make the program
more complex and very difficult to maintain and understand.

87

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]).

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

Answer: countArray[4][3] = 43;

95

Answer: 10

96

Answer: Pointer is uninitialized

97

98

99

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.

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

Answer: none of these.

110

Answer: false

111

Answer: pass by reference

112

113

114

115

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.

116

117

Answer: All of these

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

Answer: Make sure that the design is correct

137

138

139

140

Das könnte Ihnen auch gefallen