Wednesday, 28 March 2012

Type of functions, Built in functions, User defined function


Type of functions:-
          Functions are of two type:-
  • Built in function or Library Functions
  • User defined functions

Built in functions:-
          Built in functions are the functions that are provided by C library. Many activities in C are carried out using library functions. These functions perform file access, mathematical computations, graphics, memory management etc.
          A library function is accessed simply by writing the function name, followed by an optional list of arguments and header file of used function should be included with the program.
          Definition of built in functions are defined in a special header file. A header file can contain definition of more than one library function but the same function can not be defined in two header files.

User defined function:-
          Functions provided by library are not enough for user so to complete their needs user has to define some functions themselves, these are called user defined functions.
          Means except built in functions user can also define and write small programs as functions to do a task relevant to their programs, there functions should be codified by the user, so that such functions can perform the task as desired by user.

Function, Advantages of using function


Function:- 
          A function is a sub-program or set of instructions, that performs a specific task and can be processed independently. When the program passes control to a function, the control to the instruction following the calling instruction.
          The most important reason to use functions is to make program handing easier as only a small part of the program is dealt with at a time.

Advantages of using function:-
          The following are some advantages of using functions:-
The functions are easier to write, understand and debug.
A function can be shared by other programs by compiling this function separately and loading and linking them together.
Reduction is size of program due to program code of a function can be used again and again, by calling it.

Tuesday, 27 March 2012

String Variables, Initializing a String, String Manipulation, Strlen(), Strcat(), Strcpy(), Strcmp()


String Variables:-
          Variables of type char can hold a single character, so they have limited usefulness. We also need a way to store strings, which are sequence of characters. An array of characters representing a string is defined as follows:-
          Char   array_name[size]
          String variables are used to hold text data, which is comprised of letters, numerals, punctuation_marks, and other symbols.

Initializing a String:-
                   char  ram[]  =  {‘s’, ‘I’, ‘t’, ‘a’, ‘10’}
                                                Or
                   char  ram[]  =  “sita”

String Manipulation:-
          The <string.h> header file has several built in functions for string manipulation. Some of them are described below:-

          Strlen():-
                   This function is used to find the length of the string or the number of characters in a given string, excluding the end of string character (null).
          Strcat():-
                   String concatenation function concatenates (joins) the two string resulting in a single string. It takes the two arguments, which are the destination and source strings.
          Strcpy():-
                   String copy function copies the contents of one string to another strcpy(s1,s2) copies string s2 to string s1.
Strcmp():-
                    The string comparison function compares two strings character by character. It accepts any two strings as parameters and returns an integer value. Two strings are equal if their contents are identical or exactly the same.

Two Dimensional Array, Initializing a two dimensional Array


Two Dimensional Array:-
          A two dimensional array is suitable for processing table. For two dimensional array we use two subscripts enclosed in square brackets. Two dimensional arrays are declared in the same manner as one dimensional array, except that a separate pair of square brackets is required for each additional dimension of the array. To declare a two dimensional array, the first value we specify indicates the number of rows and the second value the number of columns.
          Syntax:-  data_type   array_var  [index]  [index];
          Ex:-    int  x[3][5];
          Is the declaration of an array x which has 3 rows and 5 columns.
          Note:-
                   All arrays, number matter how many dimensions they have, are stored sequentially in memory.

Initializing a two dimensional Array:-
          While initializing a two dimensional array, we should enclosed each row in braces.
          Ex:-    int   magic[5][5]  =  {    {17,24,1,8,15},
                                                          {23,5,7,14,16}
                                                          {4,6,13,2,22}
                                                          {10,12,19,21,3}
                                                          {11,18,25,2,9}
                                                    };

Monday, 26 March 2012

Traversal with Linear, Binary and Sorting Search


Traversal:-  Traversal is the act of processing each element of the array one after the other.

          Note:- 
                 We consider a number of search techniques for finding large amount of data elements to retrieve a particular piece of information. There are some simple techniques for searching. These are-

Linear Search:-
          In linear search, each element of an array element is compared one by one with the given ITEM to ne searched. Until either the desired element is found or the end of the list is reached. This method, which traverses the array sequentially to locate the given ITEM is called linear search.

Binary Search:-
          Binary search is a searching technique in which each comparison either locates an item for the target value or divides the remaining list in half. The array in a binary search must be first put in order.

Sorting:-
          Arrays are convenient for storing many forms of data, because they allow the access of the entry using its index.

Saturday, 24 March 2012

Arrays, Array Declaration, Operating on Array


Arrays:-

          An array is a collection of two or more adjacent memory locations containing same type of data. These memory locations are called array elements which are given a particular symbolic name.

          Note:-
                   An array is a collection of similar data types or elements addressed by a common variable name.

Array Declaration:-

          To set up an array, we need to declare both the name of the array and number of cells or size of this array.

          Syntax:-     data type  vari[index];

          In order to manipulate data stored in an array. We reference each individual element of the array by specifying the array name and identify the element desired by a subscript (number index). The index set consist of integer 0,1,2,3,--------etc.

Operating on Array:-

          The operations performed on linear structure (array) are:-
  • Traversal:-  Processing each element in the array or list.
  • Search:-    Finding the location of the element with a given value with a given key.
  • Insertion:-  Adding a new element to the list.
  • Deleting:-   Removing an element from list.
  • Sorting:-  Arranging the element in some type of order, ascending or descending.
  • Merging:-  Combining two lists of array within a single list or array.


Tuesday, 20 March 2012

The Break Statement, The Continue Statement, The Goto statement, Exit() Function,


The Break Statement:-

          The break statement causes an immediate exit from the innermost loop structure. If we need to come out of a running program immediately without letting it to perform any further operation in a loop, then we can use break control statement.
          Syntax:-
                             break;

The Continue Statement:-

          The continue statement forces the next iteration of the loop to take place, skipping any statement(s) following the continue statement in the body of the loop.

          Syntax:-
                             continue;

The Goto statement:-

          The goto statement is used to transfer the control in a program from one point to another point unconditionally. This is also called unconditional branching.

          Syntax:-

                             goto label;

          Where label os a valid identifier to indicate the destination wher a control can be transferred. Syntax of a lable is lable;.

Exit() Function:-

          The function exit() is defined in the header file <stdio.h> and is used to termicate the program execution immediately.

          Syntax:-
                             exit (status);

          Where ‘status’ is the termination value returned by the program and is an integer. Normal termination usually returns 0.

          Note:-

                   Some times exit() function is confused with the break statement, though it works in a different way. Break just terminates the execution of loop (or switch) where as exit() terminates the execution of the program itself.

The Loop Constructs


The Loop Constructs:- 

          The loop directs a program to perform a set of operation again and again until a specified condition is true.
          Looping statement is used when we want to execute statement or statements until a condition is true. ‘C’ contains following type of looping statement-
  • The for loop
  • The while loop
  • The do-while loop

For Loop:-

          For loop is used to execute a set of statement(s) for a given number of times, means if we want to execute statement(s) till a certain number of time, for loop is used.
          Syntax:-   
                             for (start value; end value; increment/decrement)
                             {
                                      Statement1;
                                      Statement2;
                             }

While Loop:-

          While loop statement contains the condition first. If the condition is satisfied, the control executes the statements of the while loop else, it ignores these statements.
          Syntax:-  
                              while (condition)
                             {
                                      Statement1;
                                      Statement2;
                             }

          Example:-
                                      #include <stdio.h>
int main()
{
int i;
i=1;
while(i<6)
{
printf(“Ouch! Please stop!\n”);
i++;
}
return(0);
}
                    Here’s what the output looks like:
                   Ouch! Please stop!
Ouch! Please stop!
Ouch! Please stop!
Ouch! Please stop!
Ouch! Please stop!

Do-While Loop:-

          Do-while loop statement is another method used in C. Do-while loop ensures that the program is executed at least once and checks whether the condition at the end of the do-while loop is true or false. As long as the test condition is true, the statements will be repeated.
          Syntax:- 
                             do
                             {
                                      Statement1;
                                      Statement2;
                             }
                             while (condition);

          Example:-
                                      #include <stdio.h>
void main()
{
int start;
printf(“Please enter the number to start\n”);
printf(“the countdown (1 to 100):”);
scanf(“%d”,&start);
/* The countdown loop */
do
{
printf(“T-minus %d\n”,start);
start--;
}
while(start>0);
printf(“Zero!\nBlast off!\n”);
return(0);
}
         
          Run the program.
Please enter the number to start
the countdown (1 to 100):
Be a traditionalist and type 10. Press Enter:
T-minus 10
T-minus 9
et cetera . . .
T-minus 1
Zero!
                             Blast off!

Monday, 19 March 2012

The Switch Statement

The Switch Statement:-
         Instead of using the if-else-if ladder, the switch statement is available for handling multiple choices.
          Systax:-  Switch(variable)
                          {
                                 case constant 1;
                                 statement (s);
                                 break;
                                 case constant 2;
                                       statement(s);
                                 break;
                               case constant 3;
                                       statement(s);
                               break;
                               default
                                      statement(s);
                         }

          Variable between the parentheses following the switch keyword is used to test the conditions and is called as the control variable. If result is evaluated as “constant1”, the “case constant1” is executed. If the value evaluated as “Constant2”, the “case constant2;” is executed, and so on. If the value of the variable does not correspond to any case, the default case is executed.
          The ‘break’ statement is important to transfer the control out of the switch statement.

Example:-

#include <stdio.h>
int main()
{
     char c;
     printf(“Please make your treat selection:\n”);
      printf(“1 - Beverage.\n”);
      printf(“2 - Candy.\n”);
      printf(“3 - Hot dog.\n”);
      printf(“4 - Popcorn.\n”);
      printf(“Your choice:”);
/* Figure out what they typed in. */
      c=getchar();
    switch(c)
   {
       case ‘1’:
               printf(“Beverage\nThat will be $8.00\n”);
       break;
       case ‘2’:
               printf(“Candy\nThat will be $5.50\n”);
        break;
       case ‘3’:
               printf(“Hot dog\nThat will be $10.00\n”);
        break;
       case ‘4’:
              printf(“Popcorn\nThat will be $7.50\n”);
       break;
default:
            printf(“That is not a proper selection.\n”);
            printf(“I’ll assume you’re just not hungry.\n”);
            printf(“Can I help whoever’s next?\n”);
  }
      return(0);
     }

Friday, 16 March 2012

Conditional Statement, Conditional Statement, if statement, If-else statement, If-else-if statement

Conditional Statement:-


          Some time while programming we want to execute some statement when some specified condition is true,for this type of condition we use conditional statement.
         conditional statements are following:-

  • The If Statement
  • The Switch statement



The if statement-


          If is not a command. It's another keyword in the C programming language, one that you can use in your program to make decisions- although it really makes comparisons, not decisions. It's the program that decides what to do based on the results of the comparison.
         The if() statement is a control statement that tests a particular condition. When ever the condition comes out to be true, then the action or the set of actions are carried out. Otherwise, the given set of action(s) are ignored.
        
        If statement is of following type-

  • Simple If statement
  • If-else statement
  • If-else-if statement

 If the contents of variable X are greater than variable Y, scream like

they’re twisting your nose.

If the contents of the variable calories are very high, it must taste very 
good. 
If it ain’t broke, don’t fix it. 
If Doug doesn’t ask me out to the prom, I’ll have to go with Charley.

      Syntax:-    (i)-    if (expressions)
                                          statement(s);
                                          
                            (ii)-   if (expression)
                                           {    
                                               statement;
                                               statement;
                                               -----------
                                               -----------
                                             }
                                       else
                                             {
                                                  statement;
                                                  statement;
                                                   ----------
                                                  ----------
                                              }

                             (iii)-   if (expression)
                                              {
                                                  statement;
                                                 statement;
                                                  ----------
                                                  ----------
                                                 }
                                            else
                                                   if (expression)
                                                         {
                                                              statement;
                                                              statement;
                                                             ------------
                                                             ------------
                                                         }
                                                     else
                                                             {         
                                                                   statement;
                                                                   statement;
                                                                  -----------
                                                                 -------------
                                                               }
Example of Program:-

                          
                            #include <stdio.h> 
                            #include <stdlib.h> 
                                      
                                int main() 
                                { 
                                     
                                      char num[2]; 
                                      int number; 

                                      printf(“I am your computer genie!\n”); 
                                      
                                      printf(“Enter a number from 0 to 9:”); 
                                      gets(num); 
                                      number=atoi(num); 

                                      if(number<5) 
                                      { 
      
                                                 printf(“That number is less than 5!\n”); 

                                       } 

                                       printf(“The genie knows all, sees all!\n”); 
                                       return(0); 
                                }