Introduction to C
Prof. Partha Saha
Integrated Programme in Management
TOP DOWN STRATEGY
Overall
program strategy be completely mapped
out before detailed programming
Concentrate on the general program logic,
without syntactic details of actual instructions
Developing an informal outline, consisting of
phrases or sentences (part English and part C)
Resulting outline referred to as PSEUDOCODE.
2
EXAMPLE (COMPOUND INTEREST)
A common
problem in personal finance is that of
determining how much money will accumulate in a
bank account after n years if a known amount, P, is
deposited initially and the account collects interest at a
rate of r percent per year, compounded annually.
The answer to this question can be determined by the
well known formula
F= P (1 + i)n
where F represents the future accumulation of money
(including the original sum, P, which is known as the
principal) and
i is the decimal representation of the interest rate; i.e.,
i = r/100 (for example, an interest rate of r = 5%
would correspond to i = 0.05)
GENERAL OUTLINE
Declare
the required program variables
Read in values for the principal (P),the
interest rate (r)and the number of years (n).
Calculate the decimal representation of the
interest rate (i),using the formula i = r/100
Determine the future accumulation (F)using
the formula F= P (1 + i)n
Display the calculated value for F
PROGRAM OUTLINE (PSEUDOCODE)
/* compound i n t e r e s t calculations */
main( )
{
/* declare the program variables */
/* read i n values f o r P, r and n */
/* calculate a value f o r i*/
/* calculate a value f o r F */
/* display the calculated value f o r F */
}
5
PROGRAM OUTLINE (PSEUDOCODE)
/* compound i n t e r e s t calculations */
main( )
{
/* declare p, r, n, i and f to be floating-point variables */
/* write a prompt for p and then read in its value */
/* write a prompt for r and then read i n its value */
/* write a prompt for n and then read in its value */
/* calculate i= r/100 */
/ * calculate f = p (1 + i ) " as follows:
f = p * pow((l+i),n)
where pow is a library function for exponentiation */
/* display the value for f, with an accompanying label */ 6
}
BOTTOM-UP STRATEGY
Useful
for self-contained program modules
(e.g., user-defined functions)
It involves the detailed development of these
program modules early in the planning process.
The overall program development is then based
upon the known characteristics of these
available program modules
7
HYBRID STRATEGY
In
practice we often use Hybrid approaches
Top-down approach for the overall program
designing and planning.
Bottom-up in developing individual modules
before the main part of the program
Again top-down approach with respect to the
development of each individual module their
8
inter-modular connections & functionality
SYNTACTIC
ERROR
SYNTACTIC ERROR
Syntactic
(or Grammatical) errors apparent
once the Run command has been issued
Compilation/Execution Error
Common Error
. Improperly declared variables
. Reference to an undeclared variable
. Incorrect punctuation
10
CODE WITH SYNTACTIC ERRORS
#include <stdio.h>
include <math.h>
main( )
{
float p, r, n, i,f ;
/* read input data (including prompts) */
printf("P1ease enter a value for the principal (P): " ) ;
scanf ( "%f , &p) ;
printf("P1ease enter a value for the interest rate ( r ) : ) ;
scanf ( "%f ,&r );
printf("P1ease enter a value for the number of years (n): " ) ;
scanf ( "%'f , &n)
/* calculate i, then f */
i= r/100;
f = p * pow(1 + i ) , n ) ;
/* write output /*
printf("\n The final value (F) is : % . 2 f \ n " , f ) ;
}
11
HOW MANY
SYNTACTIC
ERRORS ??
FIRST ERROR MESSAGES
13
FIRST SYNTACTIC ERRORS
The
first message refers to the missing # sign in
line 4 (the line numbers include empty lines)
The second message refers to the missing
double quote (") at the end of the second printf
statement (line 15)
Third message refers to the improper ending of
the last comment (line 25)
14
SYNTACTIC ERRORS (CORRECTIONS
#include <stdio.h>
#include <math.h>
main( )
{
float p, r, n, i,f ;
/* read input data (including prompts) */
printf("P1ease enter a value for the principal (P): " ) ;
scanf ( "%f , &p) ;
printf("P1ease enter a value for the interest rate ( r ) : ) ;
scanf ( "%f ,&r );
printf("P1ease enter a value for the number of years (n): " ) ;
scanf ( "%'f , &n)
/* calculate i, then f */
i= r/100;
f = p * pow(1 + i ) , n ) ;
/* write output */
printf("\n The final value (F) is : % . 2 f \ n " , f ) ;
}
15
SECOND ERROR MESSAGES
16
SECOND SYNTACTIC ERRORS
The
first error message refers to the missing
semicolon at the end of the last scanf statement
(which actually occurs in line 18, not line 22).
The second message refers to the missing left
parenthesis in second assignment statement (line
23).
The following two warnings and the third error
message are also a result of this one error
17
SYNTACTIC ERRORS (CORRECTIONS
#include <stdio.h>
#include <math.h>
main( )
{
float p, r, n, i,f ;
/* read input data (including prompts) */
printf("P1ease enter a value for the principal (P): " ) ;
scanf ( "%f , &p) ;
printf("P1ease enter a value for the interest rate ( r ) : ) ;
scanf ( "%f ,&r );
printf("P1ease enter a value for the number of years (n): " ) ;
scanf ( "%'f , &n);
/* calculate i, then f */
i= r/100;
f = p * pow((1 + i ) , n ) ;
/* write output */
printf("\n The final value (F) is : % . 2 f \ n " , f ) ;
}
18
RECTIFIED SYNTACTIC ERRORS
The
second include statement does not begin
with a # sign.
The control string in the second p r i n t f
statement does not have a closing quotation
mark
The last scanf statement does not end with a
semicolon
The assignment statement for f contains
unbalanced parentheses
The last comment closes improperly (it ends
with / * instead of * /).
19
CLEAN CODE
CLEAN CODE
#include <stdio.h>
#include <math.h>
main( )
{
float p, r, n, i,f;
/* read input data (including prompts) */
printf(P1ease enter a value for the principal (P): " ) ;
scanf ( "%fn, &p);
printf("P1ease enter a value for the interest rate ( r ) : " ) ;
scanf ("%f" , &r );
printf("P1ease enter a value for the number of years (n): " ) ;
scanf ( " % f " , &n) ;
/* calculate i,then f */
i= r/100;
f = p * pow((1 + i ) , n ) ;
/* display the output */
p r i n t f ( " \ n The final value (F) i s : %.2f\n,f ) ;
}
21
EDITING WINDOW IN INSERT MODE
22
EDITING WINDOW IN INSERT MODE
23
COMPILING AND EXECUTING THE PROGRAM
24
COMPILING AND EXECUTING THE PROGRAM
25
COMPILING AND EXECUTING THE PROGRAM
26
COMPILING AND EXECUTING THE PROGRAM
27
EXECUTION
ERROR
EXECUTION/RUN TIME ERROR
ERROR
Execution
Errors occur during program
execution, after a successful compilation
Numerical overflow of underflow (exceeding the largest or
smallest permissible number, stored in the computer)
Division by Zero
Compute the Logarithm / Square root of a Negative Number
29
REAL ROOTS OF A QUADRATIC EQUATION
30
REAL ROOTS OF A QUADRATIC EQUATIO
31
REAL ROOTS OF A QUADRATIC EQUATION
Suppose, for example, the program is run using the
following input values:
a= 1.0
b=2.0
c=3.0
The program compiles without any difficulty.
When the object program is executed, however, the
following error message is generated, after the input
values have been entered into the computer.
s q r t : DOMAIN error
32
DOMAIN ERROR
33
REAL ROOTS OF A QUADRATIC EQUATION
Suppose, for example, the program is run using the
following input values:
a=1E-30
b=1 El 0
c=1E36
The program compiles without any difficulty.
When the object program is executed, however, the
following error message is generated, after the input
values have been entered into the computer.
Floating P o i n t : Overflow
34
FLOATING POINT OVERFLOW
35
DEBUGGING
TECHNIQUE
ERROR
ISOLATION (SUBCODE COMMENTING
Useful for locating error resulting in diagnostic message
Often general location of the error is not known
Found by temporarily deleting a portion of the program
and rerunning the program to see if the error disappears.
Temporary deletion is accomplished by surrounding the
instructions with comment markers (/ * and * /)
If the error message then disappears, the deleted portion
of the program contains the source of the error
37
ERROR ISOLATION (INSERTING
COMMENTS)
Inserting several unique printf statements, such as
printf ("Debugging - l i n e 1\ n " ) ;
printf ("Debugging - l i n e 2 \ n " ) ;
etc. at various places within the program.
When the program is executed, the debug messages will
indicate the approximate location of the error.
Thus, the source of the error will lie somewhere between
the last printf statement whose message did appear, and
the first printf statement whose message did not appear
38
TRACING (VARIABLES/ VALUES)
Use of printf statements to display the values assigned
to certain key variables
To display the values that are calculated internally at
various locations within the program
Advantage
Values actually assigned vs. values should have been
assigned
Monitor the progress of the computation as the program
executes.
39
FLOATING POINT OVERFLOW
40
OUTPUT
When the altered program was executed with the
same three input values, the error message did not
appear (though the displayed values for x1 and x2
did not make any sense).
Thus, it is clear that the source of the original
error message lies in one of these three statements
41
MODIFIED PROGRAM
42
OUTPUT
Hence, we conclude that the overflow
occurred in the last assignment
statement, since this statement follows
the third printf statement
43
SECOND MODIFIED PROGRAM
44
OUTPUT
45
WATCH VALUE
Value of a variable / expression which is displayed
continuously as the program executes.
Monitoring changes in a watch value as they occur,
in response to the program logic.
By monitoring a few carefully selected watch
values, determine where the program begins to
generate Incorrect or Unexpected values and
Degenerate
46
BREAKPOINTS
Temporary Stopping Point within a program.
Each breakpoint is associated with a particular
instruction within the program.
Program execution will temporarily stop at the
breakpoint, before the instruction is executed.
The execution may then be resumed, until the next
breakpoint is encountered.
Breakpoints are often used in conjunction with
47
Watch Values
STEPPING
Execution of one instruction at a time ( typically
by pressing a function key to execute each
instruction).
Stepping through an entire program, one can
determine which instructions produce erroneous
results or generate error messages.
Stepping is often used with Watch Values
(allowing you to trace the entire history of a
program as it
Executes).
Observe changes to watch values as they happen.
48
DEBUGGING
49
DEBUGGING
50
DEBUGGING
51
Questions
52
Questions
53
Thank You!