Thursday, 6 October 2016

Implement the stack operations

Implement the stack operations for the stack of books, for the followings:

a) Push 5 book information.

b) Pop 2 book information.

c) Peep the 1st book.

d) Display all books from stack.

#include<stdio.h>
#define max 5
struct stack
{
            int stc[max];
        int top;
}s;

/* ~~~~~~~~~~~ PUSH~~~~~~~~~*/
void push()
{
            int x;
            if(s.top==(max-1))
            {
                        printf("The stack is overflow :");
                return;
            }
            else
            {
                        printf("Enter the element you want to stack=");
                        scanf("%d",&x);
                        s.top=s.top+1;
                        s.stc[s.top]=x;

            }
}
/*~~~~~~~~~~~~POP~~~~~~~~~~~~*/
void pop()
{
        int a;
            a=s.stc[s.top];
            if(s.top==-1)
            {
                        printf("The stack is underflow on pop :");
      
            }

            else
            {
                        s.top=s.top-1;
                        printf("The popped element is= %d",a);
            }
}
/*~~~~~~~~~~~PEEP~~~~~~~~~*/
void peep()
{
            int i,a;
            printf("Enter the position of element you want to peep from top :");
            scanf("%d",&i);
            /*if((s.top-i)+1<=0)
            {
                        printf("The stack is underflow");
                return;
            }
            else
            {
                        if(i==s.top)
                        {
                                    printf("The top most element is=",s.stc[s.top]);
                        }
                        else
                        {
                                    printf("The elemnet you peeped is=",s.stc[s.top-i+1]);
                        }
            }*/
        if((s.top-i+1)<=0)
            {
                        printf("The stack is underflow on pop :");
            }
            else
            {
                        a = s.stc[s.top-i+1];
                        printf("The searched elemement is:%d",a);
            }
        
}
/*~~~~~~~~~~DISPLAY~~~~~~~~~~~*/
void display()
{
            int i;
            if(s.top==-1)
            {
                        printf("Stack is empty :");
             
            }
            else
            {
                        for(i=s.top;i>=0;i--)
                {   
                        printf("value is :");
                                    printf("%d",s.stc[i]);
                        printf("\n");
                        }
            }         
}
/*~~~~~~~~~~ MAIN~~~~~~~~*/
void main()
{
            int b,i;
            s.top=-1;
            do
            {
                        printf("\n..............................................\n");
                        printf("Enter your choice\n");
                        printf("Enter 1 for push\n");
                        printf("Enter 2 for pop\n");
                        printf("Enter 3 for peep\n");
                        printf("Enter 4 for display\n");
                        printf("\n..............................................\n");
                        scanf("%d",&i);
                        switch(i)
                        {
                                    case 1 : push();
                                                 break;
                                    case 2 : pop();
                                                 break;
                                    case 3 : peep();
                                                 break;
                                    case 4 : display();
                                                 break;
                                    default : printf("You entered wrong choice :");
                        }
                        printf("Enter 0 to continue and enter 9 for exit :");
                        scanf("%d",&b);
            }while(b==0);
}

 Output:-






Sunday, 25 September 2016

C Program Structure

C - Program Structure


A C program basically consists of the following parts −
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments
Let us look at a simple code that would print the words "Hello World" 
#include <stdio.h>

int main() {
   /* my first program in C */
   printf("Hello, World! \n");
   
   return 0;
}
Let us take a look at the various parts of the above program −
  • The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.
  • The next line int main() is the main function where the program execution begins.
  • The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
  • The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.
  • The next line return 0; terminates the main() function and returns the value 0.
  • Compile and Execute C Program

Let us see how to save the source code in a file, and how to compile and run it. Following are the simple steps −


  • Open a text editor and add the above-mentioned code.
  • Save the file as hello.c
  • Open a command prompt and go to the directory where you have saved the file.
  • Type gcc hello.c and press enter to compile your code.
  • If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
  • Now, type a.out to execute your program.
  • You will see the output "Hello World" printed on the screen.
  • $ gcc hello.c
    $ ./a.out
    Hello, World!
  • Make sure the gcc compiler is in your path and that you are running it in the directory containing the source file hello.c.
  • Call By Value And Call By Reference In C.

    Function call by Value in C


    The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
    By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function. Consider the function swap() definition as follows.
    /* function definition to swap the values */
    void swap(int x, int y) {
    
       int temp;
    
       temp = x; /* save the value of x */
       x = y;    /* put y into x */
       y = temp; /* put temp into y */
      
       return;
    }
    Now, let us call the function swap() by passing actual values as in the following example −
    #include <stdio.h>
     
    /* function declaration */
    void swap(int x, int y);
     
    int main () {
    
       /* local variable definition */
       int a = 100;
       int b = 200;
     
       printf("Before swap, value of a : %d\n", a );
       printf("Before swap, value of b : %d\n", b );
     
       /* calling a function to swap the values */
       swap(a, b);
     
       printf("After swap, value of a : %d\n", a );
       printf("After swap, value of b : %d\n", b );
     
       return 0;
    }
    Let us put the above code in a single C file, compile and execute it, it will produce the following result −
    Before swap, value of a :100
    Before swap, value of b :200
    After swap, value of a :100
    After swap, value of b :200
    It shows that there are no changes in the values, though they had been changed inside the function.

    Function call by reference in C


    The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
    To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.
    /* function definition to swap the values */
    void swap(int *x, int *y) {
    
       int temp;
       temp = *x;    /* save the value at address x */
       *x = *y;      /* put y into x */
       *y = temp;    /* put temp into y */
      
       return;
    }
    Let us now call the function swap() by passing values by reference as in the following example −
    #include <stdio.h>
     
    /* function declaration */
    void swap(int *x, int *y);
     
    int main () {
    
       /* local variable definition */
       int a = 100;
       int b = 200;
     
       printf("Before swap, value of a : %d\n", a );
       printf("Before swap, value of b : %d\n", b );
     
       /* calling a function to swap the values.
          * &a indicates pointer to a ie. address of variable a and 
          * &b indicates pointer to b ie. address of variable b.
       */
       swap(&a, &b);
     
       printf("After swap, value of a : %d\n", a );
       printf("After swap, value of b : %d\n", b );
     
       return 0;
    }
    Let us put the above code in a single C file, compile and execute it, to produce the following result −
    Before swap, value of a :100
    Before swap, value of b :200
    After swap, value of a :200
    After swap, value of b :100
    It shows that the change has reflected outside the function as well, unlike call by value where the changes do not reflect outside the function.

    Wednesday, 21 September 2016

    Basics Formula of Algebra

    Algebra

    1. (a + b)2 = a2 + 2ab + b2; a2 + b2 = (a+b)2 −2ab
    2. (a − b)2 = a2 − 2ab + b2; a2 + b2 = (a−b)2 + 2ab
    3. (a + b + c)2 = a2 + b2 + c2 + 2(ab + bc + ca)
    4. (a + b)3 = a3 + b3 + 3ab(a + b); a3 + b3 = (a+b)3 −3ab(a + b)
    5. (a − b)3 = a3 − b3 − 3ab(a − b); a3 − b3 = (a−b)3 + 3ab(a − b)
    6. a2 − b2 = (a+b)(a − b)
    7. a3 − b3 = (a−b)(a2 + ab + b2)
    8. a3 + b3 = (a+b)(a2 − ab + b2)
    9. an − bn = (a−b)(an−1 + an−2b + an−3b2 + _ _ _ +bn−1)
    10. an = a:a:a : : : n times
    11. am:an = am+n
    12. am
    an = am−n if m >n
    = 1 if m= n
    =
    1
    an−m if m< n;a 2 R; a 6= 0
    13. (am)n = amn = (an)m
    14. (ab)n = an:bn
    15. _a
    b _n
    = an
    bn
    16. a0 = 1 where a 2 R; a 6= 0
    17. a−n =
    1
    an ; an =
    1
    a−n
    18. ap=q = pq ap
    19. If am = an and a 6= _1; a 6= 0 then m=n
    20. If an = bn where n 6= 0, then a = _b
    21. If px;py are quadratic surds and if a + px = py, then a = 0 and x = y
    22. If px;py are quadratic surds and if a+px = b+py then a = b and x = y
    23. If a;m; n are positive real numbers and a 6= 1, then loga mn = logam+loga n
    24. If a;m; n are positive real numbers, a 6= 1, then loga _m
    n _= logam−loga n
    25. If a and m are positive real numbers, a 6= 1 then logamn = nlogam
    26. If a; b and k are positive real numbers, b 6= 1; k 6= 1, then logb a =
    logk a
    logk b
    27. logb a =
    1
    loga b
    where a; b are positive real numbers, a 6= 1; b 6= 1
    28. if a;m; n are positive real numbers, a 6= 1 and if logam = logan, then
    m=n

    Formulas of Differentation and Integration

    Add caption


    Sunday, 18 September 2016

    What is a heap?

    It is a binary tree with the following properties:
    •    Property 1: it is a complete binary tree
    •    Property 2: the value stored at a node is greater or equal to the  values stored at the children 

       What is a heap? (cont.)


       Largest heap element
           From Property 2, the largest value of the heap is always stored at the root 

    Heap Specification
    template<class ItemType>
    struct HeapType {
      void ReheapDown(int, int);
      void ReheapUp(int, int);
      ItemType *elements;
      int numElements; // heap elements
    };