Thursday, 10 August 2017

DAA PRACTICAL MANUAL: PRACTICAL 4

                                                                                           Date:10/08/2017
Experiment No: 04

1. Experiment vision: Write a program to study and implement Stack.
2. OBJECTIVE: Program to implement an object in C in a specific way to add the element and   
                              remove only top element.  
3. THEORY: Stack is a specialized data storage structure (Abstract data type). Unlike, arrays access of elements in a stack is restricted. It has two main functions push and pop. Insertion in a stack is done using push function and removal from a stack is done using pop function. Stack allows access to only the last element inserted hence, an item can be inserted or removed from the stack from one end called the top of the stack. It is therefore, also called Last-In-First-Out (LIFO) list. Stack has three properties: capacity stands for the maximum number of elements stack can hold, size stands for the current size of the stack and elements is the array of elements.
Functions:
1. createStack function– This function takes the maximum number of elements (maxElements) the stack can hold as an argument, creates a stack according to it and returns a pointer to the stack. It initializes Stack S using malloc function and its properties.
2. push function - This function takes the pointer to the top of the stack S and the item (element) to be inserted as arguments. Check for the emptiness of stack
3. pop function - This function takes the pointer to the top of the stack S as an argument.
4. top function – This function takes the pointer to the top of the stack S as an argument and returns the topmost element of the stack S.

4. Algorithm:

   1. Create a stack
   2. Enter a decimal number, which has to be converted into its equivalent binary form.
   3. iteration 1 (while number > 0)
         3.1 Push digit into the stack
         3.2 If the stack is full
              3.2.1 Print an error
              3.2.2 Stop the algorithm
         3.3 End the if condition
         3.4 Divide the number by 2
   4. End iteration1
  
   5. iteration 2 (while stack is not empty)
         5.1 Pop digit from the stack
         5.2 Print the digit
   6. End iteration2
   7. STOP




5.  PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
void main()
{
    //clrscr();
    top=-1;
    printf("\n enter the size of stack[max=100]:");
    scanf("%d",&n);
    printf("\n\t stack operations using array");
    printf("\n\t--------------------------------");
    printf("\n\t 1.push\n\t 2.pop\n\t 3.display\n\t 4.exit");
    do
    {
        printf("\n enter the choice:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
        {
            push();
            break;
        }
        case 2:
        {
            pop();
            break;
        }
        case 3:
        {
            display();
            break;
        }
        case 4:
        {
            printf("\n\t exit point ");
            break;
        }
        default:
        {
            printf ("\n\t please enter a valid choice(1/2/3/4)");
        }
      }
    }
    while(choice!=4);
}

void push()
{
    if(top>=n-1)
    {
        printf("\n\tstack is over flow");
       
    }
    else
    {
        printf(" enter a value to be pushed:");
        scanf("%d",&x);
        top++;
        stack[top]=x;
    }
}
void pop()
{
    if(top<=-1)
    {
        printf("\n\t stack is under flow");
    }
    else
    {
        printf("\n\t the popped elements is %d",stack[top]);
        top--;
    }
}
void display()
{
    if(top>=0)
    {
        printf("\n the elements in stack \n");
        for(i=top; i>=0; i--)
            printf("\n%d",stack[i]);
        printf("\n press next choice");
    }
    else
    {
        printf("\n the stack is empty");
    }
}
6. OUTPUT:
------------------------------------------
------------------------------------------

7. conclusion:
Thus a C Programs by using method of stack has been executed successfully.
8.  REMARK:
Program to implement in C in a specific way to add the element and remove only top element just like a stack procedure of LIFO.


  9. Discussion Questions:
1) what is stack?
_________________________________________________________________________________________________________________________________________________________
2) How is stack working, explain with example? (diagrammatically)
________________________________________________________________________________________________________________________________________________________
3) explain example of tower of Hanoi using stack?
____________________________________________________________________________________________________________________________________________________________

                                    

Thursday, 3 August 2017

DAA PRACTICAL MANUAL: PRACTICAL 3

                                                                                           Date:  03/08/2017
Experiment No: 03

1. Experiment vision: Write a program to study and implement Bubble sort.
2. OBJECTIVE: Program to implement an array of object in C and to sorting all the   
                              elements in array.  
3. THEORY: Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.
        The smallest element is bubbled from the unsorted list and moved to the sorted sub list.
     After that, the wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones.
   Each time an element moves from the unsorted part to the sorted part one sort pass is completed.
       Given a list of n elements, bubble sort requires up to n-1 passes to sort the data.
4.  PROGRAM:
#include<stdio.h>

void main()
{
   int array[10],n,i,j,swap;
  printf("Enter the Number of Elements for an array\n");
   scanf("%d",&n);

  printf("\nEnter the Number of Elements");
  for(i=0;i<n;i++)
{   scanf("%d",&array[i]);}

   for(i=0;i<n;i++)
     {
       for(j=0;j<n;j++)
        {
           if(array[i]<array[j])
          {  
              swap=array[i];
              array[i]=array[j];
              array[j]=swap;
             }
     }   }

 printf("Sorted elements are\n");
  for(i=0;i<n;i++)
 printf("%d\n", array[i]);
}

5. OUTPUT:
------------------------------------------------------------------------------------
6. conclusion:
Thus a C Programs by using array of object for bubble sorting method has been implemented and executed successfully.
7.  REMARK:
Array of element is the concepts are used for printing the values of number element and to sort the element using implementing bubble sorting method.

8. Discussion Questions:

1) what is bubble sort?
_________________________________________________________________________________________________________________________________________________________
2) How is bubble sort working explain with example? (Diagrammatically)
________________________________________________________________________________________________________________________________________________________
3) What is the complexity of bubble sort method?
____________________________________________________________________________________________________________________________________________________________

                                    

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.