Tuesday, October 12, 2010

Structure of a C Program with an example


STRUCTURE OF A C PROGRAM:
The basic structure of a C program is as shown below –
Documentation Section
----------------------------------------------
Link Section                                   
----------------------------------------------
Definition Section
----------------------------------------------
Global Declaration Section
main() Function Section
{
           Declaration Part
           ------------------------
           Executable Part
}
Subprogram section
            Function 1
            Function 2
                    .
                    .
                    .
            Function n

Description:
·         The documentation section consists of a set of comment lines giving the name of the program, the author’s name and other details, which the programmer would like to use later.
·         The link section provides instructions to the compiler to link functions from the system library.
·         The definition section allows symbolic constants.
·         There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section.
·         Every C program must have one main() function section.
o   This section contains two parts declaration part and executable part.
o   The declaration part declares all the variables used in the executable part.
o   There should be at least one statement in the executable part.
o   These two parts must appear between the opening and  the closing braces.
o   The program execution begins at the opening brace and ends at the closing brace.
o   The closing brace of the main function section is logical end of the program.
·         All statements in the declaration and executable parts end with a semicolon.
·         The subprogram section contains all  the user-defined functions that are called in the main function. The main function is very important compared to other sections.

Example:
main()
{
            /* Printing Begins */
            printf(“Hello World!\n”);
            /* Printing Ends */
}
This program displays Hello World! on the screen.
Description-
·         The first line is where the execution begins. There must be exactly one main() in every program.
o   The empty brackets () indicate that the function does not have any arguments.
o   ‘main’ can be written in any of the following ways –
main()                        int main()                               void main()  
main(void)                int main(void)                      void main(void)
o   Here int means main returns an integer and if void returns nothing.

·         The function is followed by { indicating the beginning of function body.
·         The first line inside ‘main’ function is a comment line (which begins with /* and ends with */). Such comment lines are used to increase the readability of a program.
o   Comments are non-executable.
o   They cannot be nested                   /*...... /* ....... */ ....... */ is not valid.
·         The second line has a function printf, which is a predefined C function for printing output.
o   This function causes everything between the starting and ending quotation marks to be printed out.
·         The \n is a newline character, which instructs the computer to go to next (new) line. It is similar to the return key of the keyboard.
o   No space is allowed between \ and n.
·         ‘printf’ function is defined under the pre-processor directive stdio.h
o   ‘stdio.h’ is a header file used for calling functions for input and output.



READING DATA FROM KEYBOARD:
Data can be inputted and stored into a variable using a predefined function called scanf.
·         ‘scanf’ is also a part of the directive ‘stdio.h’.
·         Its syntax is –
scanf(“control string”, &variable1, &variable2, ...);
Example: -                 scanf(“%d”, &n);
While executing a program, this statement makes the cursor to blink on the screen until an integer is inputted from the keyboard and return key is pressed.

After an integer is keyed in, that entered value will be saved in the variable n, so that keyed in value can be accessed later using the variable ‘n’.
·         Thus scanf makes the program interactive and makes it user friendly.

History of C

INTRODUCTION TO C:
·         C is a programming language developed at AT&T’s Bell laboratories of USA in 1972.
·         It was designed and written by a system programmer Dennis Ritchie.

HISTORY OF C:
The following flow diagram shows the evolution of C –



·         The root of all modern languages is ALGOL, introduced in 1960s. It was the first computer language to use block structure.
·         In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) mainly for writing system software.
·         In the year 1970, using the concepts of BCPL, Ken Thompson developed a language called B. It was used to create earlier versions of UNIX operating System at Bell Laboratories.
o   Both B and BCPL were typeless system programming languages
·         C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell Laboratories in 1972.
o   C added the concept of data types
o   Since C was developed along with the UNIX operating system, it is strongly associated with UNIX.
·         The language became more popular after the publication of the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie in 1978. The book was so popular that the language came to be known as”K & R C”.
·         Later on it was made an American Standard (ANSI C) and then International (ISO).
·         C99 is a standardized version of C which was done in 1999; so the name C99.

C language became popular because of the following reasons –
§  C is a robust language with a rich set of built-in functions and operators.
§  The C compiler combines the capabilities of an assembly language with the features of a High Level Language and therefore it is well suited for writing both system software and business packages.
§  C language is highly Portable – This means that C programs written for one computer can be run on another with little or no modification.
§  C language is well suited for Structured programming, thus user needs to think a program in terms of function modules or blocks. – This modular structure makes program debugging, testing and maintenance easier.
§  A C program is basically a collection of functions that are supported by the C library. We can continuously add our own functions to C library.

Monday, September 27, 2010

Flowcharts

WHAT IS A FLOWCHART?:

It is an organized combination of shapes, lines and text which graphically illustrate a process.
  • A flowchart can be used as an alternative for an algorithm
SYMBOLS USED IN FLOWCHARTS:

The following table shows a listing of different symbols used in flowcharts along with their meaning and purpose –

EXAMPLES:

1. Draw a flowchart to print the sum of two numbers.
2.  Draw a flowchart to print the average of any three numbers.
3. Draw a flowchart to print Area and Perimeter of a Square.
4. Draw a flowchart to print biggest of two numbers.
5. Draw a flowchart to check whether a number is positive, negative or zero.
6. Draw a flowchart to print biggest of three numbers.
7. Draw a flowchart to print a number ‘n’ 10 times.
8. Draw a flowchart to print all the odd numbers between 1 and n.
9. Draw a flowchart to print all the even numbers between 1 and n.

Algorithms

USING ALGORITHMS TO SOLVE PROBLEMS:

What is an Algorithm?
An algorithm is a step-by-step procedure to solve a given problem.
Steps in algorithm writing:
Ø  Identify all the inputs and outputs required for solving a problem.
Ø  Identify additional data and constants needed.
Ø  Computation steps
Ø  Print / Display all the required results.

DIFFERENT PATTERNS IN ALGORITHMS:
There are mainly three different patterns of algorithms. They are as listed below-
  1. Sequential – The steps represent sequence of occurrence of processing.
  2. Conditional – Based on one or more conditions the instructions are divided
  3. Iterational – Same set of instructions are repeated more than once until an end condition. These are also called Repetitive patterns.



EXAMPLES:
A. On Sequential Patterns
1.    Write an algorithm to find the sum of any two numbers.
Sol-                 Step 1: Start
                        Step 2: Read two numbers n1 and n2
                        Step 3: Sum = n1 + n2
                        Step 4: Print ‘Sum’
                        Step 5: Stop

2.    Write an algorithm to find the average of three numbers.
Sol-                 Step 1: Start
                        Step 2: Read three numbers n1, n2 and n3
                        Step 3: Avg = (n1 + n2 + n3) /3
                        Step 4: Print ‘Avg’
                        Step 5: Stop

3.    Write an algorithm to find the volume of a cylinder.
Sol-                 Step 1: Start
                        Step 2: Read radius (r) and height (h)
                        Step 3: Initialize constant,  P = 3.142
                        Step 4: Volume = Pr2h
                        Step 5: Print ‘Volume’
                        Step 6: Stop

4.    Write an algorithm to find the area and perimeter of a square.
Sol-                 Step 1: Start
                        Step 2: Read side of a square (a)
                        Step 3: Area = a2      and     Perimeter = 4a
                        Step 4: Print ‘Area’ and ‘Perimeter’
                        Step 5: Stop

5.    Write an algorithm to convert celcius into farenheit.
Sol-                 Step 1: Start
                        Step 2: Read temperature in celsius (c)
                        Step 3: f = 9c / 5 + 32
                        Step 4: Print temperature in farenheit (f)
                        Step 5: Stop

B. On Conditional Patterns
1.    Write an algorithm to find biggest of two numbers.
Sol-                 Step 1: Start
                        Step 2: Read two numbers (n1 and n2)
                        Step 3: if n1 > n2, go to Step 5
                        Step 4: Print “n2 is biggest” and go to Step 6
                        Step 5: Print “n1 is biggest”
                        Step 6: Stop

2.    Write an algorithm to check whether a given number is +ve, -ve or a zero.
Sol-                 Step 1: Start
                        Step 2: Read a number (n)
                        Step 3: if n > 0, go to Step 6
                        Step 4: if n < 0, go to Step 7
                        Step 5: Print “n is Zero” and go to Step 8
                        Step 6: Print “n is a +ve number” and go to Step 8
                        Step 7: Print “n is a –ve number”
                        Step 8: Stop

3.    Write an algorithm to check whether a given number is even or odd.
Sol-                 Step 1: Start
                        Step 2: Read a number (n)
                        Step 3: if remainder of n/2 is = 0, go to Step 5 
                                     or if n % 2 = 0, go to Step 5
                        Step 4: Print “n is odd” and go to Step 6
                        Step 5: Print “n is even”
                        Step 6: Stop

4.    Write an algorithm to find a person’s eligibility for voting, based on his/her age.
Sol-                 Step 1: Start
                        Step 2: Read the age of a person (age)
                        Step 3: if age > 18, go to Step 5
                        Step 4: Print “Person is not eligible for voting” and 
                                     go to Step 6
                        Step 5: Print “Person is eligible for voting”
                        Step 6: Stop

C. On Iterational Patterns
1.    Write an algorithm to print a number (5) 10 times.
Sol-                 Step 1: Start
                        Step 2: Initialize Counter to 1 (Count =1)
                        Step 3: Print ‘5’
                        Step 4: Count = Count + 1
                        Step 5: if Count < = 10 go to Step 3
                        Step 6: Stop

2.    Write an algorithm to print all the odd numbers between 1 and 10.
Sol-                 Step 1: Start
                        Step 2: Initialize Counter to 1 (Count =1)
                        Step 3: Print ‘Count’
                        Step 4: Count = Count + 2
                        Step 5: if Count < = 10 go to Step 3
                        Step 6: Stop

3.    Write an algorithm to print all the even numbers between 1 and 10.
Sol-                 Step 1: Start
                        Step 2: Initialize Counter to 2 (Count =2)
                        Step 3: Print ‘Count’
                        Step 4: Count = Count + 2
                        Step 5: if Count < = 10 go to Step 3
                        Step 6: Stop