Selection sort
for(i=0;
i< n-1; i++)
for(k=i+1; k<n; k++)
if(x[i] > x[k])
swap( );
Bubble sort
for(i=0;
i< n; i++)
for(k=0; k<n-1; k++)
if(x[k] > x[k+1])
swap( );
Insertion sort
for(i=1;
i< n; i++)
for(k=0; k<i; k++)
if(x[i] < x[k])
swap( );
· Write a program to find
factorial of an input number using iterative
method
· Write a program to find
factorial of an input number using recursive
method
Steps of
Programming
1. Planning a C program
Before beginning actual
program, it is necessary to analyze detail of the problem to be solved. A
programmer should concentrate on general program logic without focusing on
syntactic details. This process may repeat many times with more programming
details. So initially, only module names are used to solve but then after such
modules are written in detail. Such method of solving problem is known as
top-down programming.
There is another method of
programming, in which useful self-contained modules (user defined functions)
are written first. After detail development of such modules, they are combined
together to form a program. This method of programming is known as bottom-top
programming approach.
2. Writing
outline of program
In order to solve a problem
using a computer, it must be expressed the solution to the problem in terms of
the instructions of the particular computer. A computer program is actually just
a collection of the instructions necessary to solve a specific problem. The
approach or method that is used to solve the problem is known as algorithm.
Such
an algorithm, is written with informal outline which consist of phrases or
sentences that are part in English and part in C language. In this stage of
development of program, amount of C codes are minimum used, only used for major
components such as function headings, function references, control statements
describing major program components. Additional details are described in
English. So the resulting outline of details of program is known as pseudocode.
Here is an example of pseudocode to find the square
root of an input number.
Step 1: Start the program.
Step 2: Declare x, sqrt and n as floating point
variable, initializing the value of n as 0.5.
Step 3: find the antilog for product of n and log
of x, then store it to sqrt variable.
Step 4: print the value of sqrt variable
Step 5: end of program.
Here is a pseudocode to print prime numbers from 1to
100.
Step 1: fill an array num[100] with numbers 1 to
100
Step 2: starting with the second entry in the
array, set all it multiple to zero.
Step 3: proceed to next non-zero element and set
all it’s multiple to zero.
Step 4: repeat step 3 till you have setup the
multiples of all the non zero elements to zero.
Step 5: at the conclusion of step 4, all the
non-zero entries left in the array would be prime numbers, so print out these
numbers.
Here is a pseudocode to print integer numbers from 1
to 10.
Step 1: start
the program
Step 2: initialize
the value of i to 1
Step 3: print
value of i and increase it by 1
Step 4: repeat
step 3 till value of i is less than 11
Step 5: end
of program
3. Flow
chart
After the development of
algorithm, the flow of control of instruction can be shown in pictorial form.
Such pictorial form of execution of instructions is known as flow
chart. It makes programmer and any other user can easily understand the
algorithm of program if the program is to be debugged or to be modified in
future.
Writing a C program (Coding)
Once an overall program
strategy has been formulated and program outline has been developed, it is to
be coded in programming language like C. At this stage, each component of
program is carefully to be translated into equivalent C instructions in
details. Each instruction should be written in right ordered (sequence),
correct and readable (use of indentation, comments and clearly labeled) with
proper declarations.
Compiling & Executing program
Once a
complete program has been correctly entered into the computer, it can be
compiled and executed. The program can either be compiled & run using
compile & run option in menu or using command prompt.
e.g
tcc
name
tcc test.cpp for both compile and run
tcc –c
test.cpp for only to compile
Error Diagnostics
While attempting to compile and run the program, it
may list some errors because of improperly declared variables, reference to undeclared
variables or improper use of punctuation. Such type of error is known as syntax
(grammatical) error or Compile time error. The compiler will
automatically generate diagnostic messages for such errors.
It may
display error after successfully compilation of program. Such type of error
occured during execution of program, because of boundary limit of type of
variable, division by zero, square roof of negative no. etc. Such error is known as execution error or Run time error.
After
successfully, compilation and execution of program, it may still display
incorrect result after determining the expression because of improper use of
logic during problem analysis, and such type of error is known as logical
error.
Overflow and underflow errors
Sometime working in a data
item in C, it may exceed beyond the capacity of the data item which is known as
overflow error. And similarly, it may goes down to its lower limit then it is
known as underflow error.
e.g. int x=32767;
if the value
of x is increased beyond it’s limit, then it again starts from -32768. This
type of error is known as overflow error.
Detecting error (Debugging)
It is
easy to find out syntax error in the program, since it is automatically
detected and listed while compiling the program. It displays error with
instruction line number.
Execution
and logical errors may occur at some circumstances only, so for detecting such
errors, the program must be thoroughly checked by supplying various data. Each
calculation is to be carried out for known answer or by calculating using
calculator.
If the
error cannot be located simply by inspection, the program should be modified to
print out certain intermediate results and then be re-run. This technique is
referred to as tracing.
Documentation
Once a
system is implemented and in full operation, it is to be examined to see if it
has met the objectives set out in original specification. Unforeseen problems
may overcome in future or the program is to be modified to fulfill future
requirement. So, it will be easier for such amendments in the program by any
programmer, if properly documented the program with necessary diagrams, algorithm,
flowcharts, coding, user guide and limitations are included.
Structured programming
In properly structured program, sets of actions are
grouped together in units that are, self contained syntactically for the
compiler and visually for human reader.
C encourages the division of a program into many
small routines, called function, with self defined, interfaces and an efficient
calling mechanism. Function can call other functions including themselves. Such
a method of programming that breaks a program into many modules with clarity is
known as structure programming.
Within each function, the code is structured into
blocks using { and }
as block markers. Blocks can be nested
to any level however large or small. The control enters only at the start of
block and emerges eventually only at the bottom.
Advantages of structure programming
Accuracy, readability and maintainability
Maintainability:
Most programs eventually require maintenance whether
or not they are maintainable. Programs that are different to understand are
hard to update without causing damage with the change. If unstructured, the
cost of maintenance of program becomes high. So it may require to discard such
program. If structured, the life of program prolong by maintaining it.
Clarity:
Structured program are more readable with compare
the unstructured. Any logical error can be easily debugged & solved. Since,
it is easy to check only with the module having error.
Accuracy:
Since the program can easily be debugged & is
free of any logical error, any one gets desired result with accuracy.
2. What are the components of a program? What is
the one component that must be present in every C program? (2055 -10)
A program written in C, consist of many components.
The major components of the program are:
1)
Comments :
Any text entered between /*
and */ or after // is ignored by the compiler. Comments are used to remark
certain programmer reference text such as version number, authorship, dates,
copyright as well as various explanations of different modules &
instructions.
2)
Include directive :
Generally it consists of
header file & library file such as stdio.h. It includes necessary codes of
any library functions used in the program from respective header/ library file.
3)
Defining symbolic constant
& macros :
In this section, constant is
defined as macros, which during preprocessing the preprocessor replaces every
occurrence with respective constants in the program.
e.g. # define PI 3.1415
printf(“ The Yankee virus”);
4)
Main function:
It
is the most important component of a program which must be present in every
program. There may be other self contained functions in a program. Such
function may be defined either before or after main function but the program
execution always begins from main function wherever it is positioned.
5)
Block markers (compound
statement) :
{ } curly braces are used to
signal the start and end of a block of codes. They play the same rose as BEGIN
and END in other structure language. There may be a number of block markers to
represent each block of statements. It is also known as compound statement. But
there must be equal number of opening & closing of block markers.
6)
Declarations :
At the beginning of main function, all
variables should be defined with the data type that may be stored during the
execution of program. Such declaration of variables, actually reserve space
into memory. The declaration of variable allocates number of bytes into memory
such as 2, 4, 8, 1 byte for int, float, double, & char respectively.
It also must be
declared a function if the function is defined after the main function which is
known as function prototype of the function. It contains type of function it
returns value and function name followed with number of arguments as it is
defined in function definition.
7)
Statements :
One or more statements can
be enclosed within main function. It may be used number of library functions,
user defined functions, expression statements etc. such statements will be
executed one after another. But such execution can be controlled by control
statements like if…else, for, while, do… while, switch etc.
Let’s see an example of program to find area of a
circle for an input radius using function.
#include<stdio.h>
#include<math.h>
#define PI 3.1415
void main()
{
int
rad;
float
a;
float
area (float r);
printf(“\n
Enter radius of circle”);
scanf(“%d”,&rad);
a=area(rad);
printf(“\n
Area of circle is : %f”,a);
}
float area(float r)
{
return(PI*pow(r,2));
}
0 Comments
Please Don't Post Spam Links Under Comment Section.