Wednesday, 7 September 2016

Sum and Average of array elements using pointer

Write a C program to read 10 integers and store them in an array using pointers. Print their sum and average


#include<stdio.h>
#include<math.h>
void main()
{
                int a[10],sum=0;
                float avg=0;
                int i;
               
                printf("\nEnter the n array elements:");
                for(i=0;i<10;i++)
                                scanf("%d",(a+i));

                for(i=0;i<10;i++)
                                sum=sum+*(a+i);
   
                avg = (float)sum/10;

                printf("\nSum is = %d", sum);
                printf("\nMean is = %f", avg);
}

Output:
Enter the n array elements:1
2
3
4
5
6
7
8
9
10

Sum is = 55

Mean is = 5.500000

String Palindrome without Library function

Write a function that accepts a string and returns 1 if the string is palindrome else 0 if string is not a palindrome without using any built in functions.

#include<stdio.h>
int palindrome(char str[10])
{
                int i, j;
                i=0;
                while(str[i]!='\0')
                {
                                i++;
                 }
                for(j=i-1, i=0;   j>=0;    j--,i++)
                {
                                if(str[i] != str[j])
                                return 0;
                }
                return 1;
}
void main()
{
                char str[10];
                int p;
                printf("\nEnter a string:   ");
                gets(str);

                p=palindrome(str);
                if(p)
                                printf("\nString is palindrome");
                else
                                printf("\nString is not palindrome");
}
Output:
Enter a string:   abcde
String is not palindrome

Enter a string:   abcba
String is palindrome


Method 2:
#include <stdio.h>
int main()
{
                char string[20];
                int i, length;
                int flag = 0;
                printf("Enter a string:");
                scanf("%s", string);

                length=0;
                while(string[length]!='\0')
                                length++;

                for(i=0;i < length ;i++)
                {
                                if(string[i] != string[length - i - 1])
                                {
                                                flag = 1;
                                                break;
                                }
                }
                if (flag)
                                  printf("\nstring is not a palindrome");
                else
                                  printf("\nstring is a palindrome");
  

}

Recursive Binary Search

Binary Search Using Recursion

#include<stdio.h>

void binary_search(int a[100], int low, int high, int key)
{
                int mid;
                if(low>high)
                {
                                printf("\nKey not found");
                                return;
                }
                mid = (low+high)/2;
                if(key == a[mid])
                {
                                printf("\nKey element is found at position = %d", mid);
                }
                else  if(key<a[mid])
                                binary_search(a, low, mid-1, key);
                else
                                binary_search(a, mid+1, high, key);
}
void main()
{
                int a[100], i, n, pos, key;
                printf("\nEnter value of n: ");
                scanf("%d", &n);
                printf("\nEnter the array elements: ");
                for(i=0; i<n; i++)
                                scanf("%d", &a[i]);

                printf("\nEnter key: ");
                scanf("%d", &key);
                binary_search(a, 0, n-1, key);
}

Output:
Enter value of n: 6
Enter the array elements: 11 22 33 44 55 66
Enter key: 55
Key element is found at position = 5


Enter value of n: 6
Enter the array elements: 11 22 33 44 55 66
Enter key: 88
Key is not found

Solar system using structure

  Develop a structure to represent a planets in the solar system. Each planet has the field for the planets name, its distance from the sun in miles and the number of moon it has. Write a program to read the data for each planet and store. Also print the name of the planet that has the highest number of moons.

#include<stdio.h>
struct SolarSystem
{
            char name[30];
            int distance;
            int moons;
}planet[9];

void main()
{
                 int i, max_moons,  pos;
                printf("\nEnter planets records: \n");
                for(i=0;i<9;i++)
                {
                                printf("\nEnter the name of the planet: ");
                                scanf("%s",  planet[i].name);
                                printf("\nEnter the distance of the planet from sun: ");
                                scanf("%d", &planet[i].distance);
                                printf("\nEnter the number of moons for planet: ");
                                scanf("%d", &planet[i].moons);
                }
                printf("\nPlanets records: \n");
                printf("\n---------------------------------------");
                printf("\nName\tDistance\tMoon");
                printf("\n---------------------------------------");
                for(i=0;i<9;i++)
                {
                                printf("\n%s\t%d\t%d", planet[i].name,planet[i].distance, planet[i].moons);
                }
                max_moons = planet[0].moons;
                pos=0;
                for(i=0;i<9;i++)
                {
                                if(planet[i].moons> max_moons)
                                {
                                                max_moons = planet[i].moons;
                                                pos=i;
                                }
                }
                printf("\nThe planet having highest number of moons is %s",  planet[pos].name);
}

Output:
(Note:  Distance in million miles)
Enter planets records:
Enter the name of the planet: Mercury
Enter the distance of the planet from sun: 36
Enter the number of moons for planet: 0

Enter the name of the planet: Venus
Enter the distance of the planet from sun: 67
Enter the number of moons for planet: 0

Enter the name of the planet: Earth
Enter the distance of the planet from sun: 93
Enter the number of moons for planet: 1

Enter the name of the planet: Mars
Enter the distance of the planet from sun: 142
Enter the number of moons for planet: 2

Enter the name of the planet: Jupiter
Enter the distance of the planet from sun: 483
Enter the number of moons for planet: 62
Enter the name of the planet: Saturn
Enter the distance of the planet from sun: 888
Enter the number of moons for planet: 53

Enter the name of the planet: Uranus
Enter the distance of the planet from sun: 1784
Enter the number of moons for planet: 21

Enter the name of the planet: Neptune
Enter the distance of the planet from sun: 2794
Enter the number of moons for planet: 13

Enter the name of the planet: Pluto
Enter the distance of the planet from sun: 2647
Enter the number of moons for planet: 3

Planets records:
----------------------------------------------------------------------
Name                    Distance               Moon
-----------------------------------------------------------------------
Mercury                 36                          0
Venus                     67                          0
Earth                      93                          1
Mars                      142                         2
Jupiter                   483                         62
Saturn                   888                         53
Uranus                 1784                        21
Neptune               2794                        13
Pluto                    2647                         3
The planet having highest number of moons is Jupiter


Tuesday, 6 September 2016

Multiple stacks in 1 Dimensional Array

Multiple stacks in 1 Dimensional Array


#include<stdio.h>
#include<stdlib.h>
#define MAX 12

int stack[MAX];
int m=MAX, n;
int top[10], boundary[10];

void push(int i, int item )
{
          if(top[i] == boundary[i+1])
          {
                    printf("\nStack%d overflow", i);
          }
          else
         {
                   top[i] = top[i] +1;
                   stack[top[i]] = item;
         }
}
void pop(int i)
{
         int item;
         if(top[i] == boundary[i])
         {
                  printf("\nStack%d underflow", i);
         }
         else
        {
                 item = stack[top[i]];
                 printf("\nItem that got deleted from stack%d is %d", i, item);
                 top[i] = top[i] - 1;
         }
}

void display(int i)
{
          int j;
          if(top[i] == boundary[i])
          {
                     printf("\nStack%d is empty", i);
                     return;
          }
          else
         {
                    printf("\n");
                    for(j=top[i]; j>=boundary[i]+1; j--)
                               printf("| %d |\n",stack[j]);
          }
}

void main()
{
          int ch, stackno, item,i;
          printf("\nEnter the number of stacks: ");
          scanf("%d",&n);
          for(i=0;i<n;i++)
          {
                  top[i] = boundary[i] = (i*(m/n)) - 1;
                  printf("\ntop[%d] = %d and boundary[%d] = %d",i,top[i], i, boundary[i]);
          }
          boundary[i] = MAX-1;
   
          while(1)
          {
                  printf("\n\n~~~MENU~~~");
                  printf("\n1.Push operation");
                  printf("\n2.Pop operation");
                  printf("\n3.Display");
                  printf("\n4.Exit");
                  printf("\nPlease enter your choice: ");
                  scanf("%d", &ch);
                  switch(ch)
                 {
                            case 1: printf("\nEnter a item to be pushed: ");
                                        scanf("%d",&item);
                                        printf("\nEnter the stack number: ");
                                        scanf("%d",&stackno);
                                        push(stackno, item);
                                        break;

                           case 2: printf("\nEnter the stack number: ");
                                       scanf("%d",&stackno);
                                       pop(stackno);
                                       break;

                          case 3: for(stackno=0;stackno<n;stackno++)
                                     {
                                              printf("\nStack%d contents are: ", stackno);
                                              display(stackno);
                                     }
                                     break;

                          case 4:exit(0);
                          default: printf("\nPlease enter a valid choice");
                                       break;
                }
       }
}

Output:

Enter the number of stacks: 3

top[0] = -1 and boundary[0] = -1
top[1] = 3  and boundary[1] = 3
top[2] = 7  and boundary[2] = 7

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 11
Enter the stack number: 0

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 12
Enter the stack number: 0

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 13
Enter the stack number: 0

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 14
Enter the stack number: 0

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 15
Enter the stack number: 0
Stack0 overflow

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 21
Enter the stack number: 1

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 22
Enter the stack number: 1


~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 23
Enter the stack number: 1

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 31
Enter the stack number: 2

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 32
Enter the stack number: 2

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 1
Enter a item to be pushed: 33
Enter the stack number: 2

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 3
Stack0 contents are:
| 14 |
| 13 |
| 12 |
| 11 |

Stack1 contents are:
| 23 |
| 22 |
| 21 |

Stack2 contents are:
| 33 |
| 32 |
| 31 |


~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 23

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 3

Stack0 contents are:
| 14 |
| 13 |
| 12 |
| 11 |

Stack1 contents are:
| 22 |
| 21 |

Stack2 contents are:
| 33 |
| 32 |
| 31 |

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 22

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Item that got deleted from stack1 is 21

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 2
Enter the stack number: 1
Stack1 underflow

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 3

Stack0 contents are:
| 14 |
| 13 |
| 12 |
| 11 |

Stack1 contents are:
Stack1 is empty

Stack2 contents are:
| 33 |
| 32 |
| 31 |

~~~MENU~~~
1.Push operation
2.Pop operation
3.Display
4.Exit
Please enter your choice: 4

Monday, 5 September 2016

Lab Program 8 Doubly Linked List 15CSL38 Data Structures in C Lab

Lab program 8:

Design, Develop and Implement a menu driven Program in C for the following operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo

a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit

#include<stdio.h>
#include<stdlib.h>

struct node
{
         char ssn[25],name[25],dept[10],designation[25];
         int sal;
         long int phone;
         struct node *llink;
         struct node *rlink;
};
typedef struct node* NODE;

NODE first = NULL;
int count=0;


NODE create()
{
          NODE enode;
          enode = (NODE)malloc(sizeof(struct node));
          if( enode== NULL)
          {
                    printf("\nRunning out of memory");
                    exit(0);
           }
           printf("\nEnter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: \n");
         scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept, enode->designation, &enode->sal, &enode->phone);
           enode->llink=NULL;
           enode->rlink=NULL;
           count++;
           return enode;
}

NODE insertfront()
{
         NODE temp;
         temp = create();
          if(first == NULL)
         {
                 return temp;
          }
          temp->rlink = first;
          first->llink = temp;
          return temp;
}

void display()
{
          NODE cur;
          int nodeno=1;
          cur = first;
          if(cur == NULL)
                      printf("\nNo Contents to display in DLL");
          while(cur!=NULL)
         {
        printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone no:%ld", nodeno, cur->ssn, cur->name,cur->dept, cur->designation, cur->sal, cur->phone);
                   cur = cur->rlink;
                    nodeno++;
         }
          printf("\nNo of employee nodes is %d",count);
}

NODE deletefront()
{
         NODE temp;
         if(first == NULL)
         {
                   printf("\nDoubly Linked List is empty");
                   return NULL;
         }
         if(first->rlink== NULL)
        {
                  printf("\nThe employee node with the ssn:%s is deleted", first->ssn);
                 free(first);
                 count--;
                  return NULL;
         }
         temp = first;
         first = first->rlink;
         temp->rlink = NULL;
         first->llink = NULL;
         printf("\nThe employee node with the ssn:%s is deleted",temp->ssn);
         free(temp);
         count--;
         return first;
}

NODE insertend()
{
          NODE cur, temp;
          temp = create();

          if(first == NULL)
          {
                    return temp;
          }
         cur= first;
         while(cur->rlink!=NULL)
        {
                  cur = cur->rlink;
         }

         cur->rlink = temp;
         temp->llink = cur;
          return first;
}

NODE deleteend()
{
         NODE prev,cur;
         if(first == NULL)
        {
                 printf("\nDoubly Linked List is empty");
                 return NULL;
        }

        if(first->rlink == NULL)
        {
                   printf("\nThe employee node with the ssn:%s is deleted",first->ssn);
                   free(first);
                   count--;
                   return NULL;
        }

         prev=NULL;
         cur=first;

        while(cur->rlink!=NULL)
        {
                  prev=cur;
                  cur = cur->rlink;
         }

         cur->llink = NULL;
         printf("\nThe employee node with the ssn:%s is deleted",cur->ssn);
         free(cur);
         prev->rlink = NULL;
         count--;
         return first;
}

void deqdemo()
{
       int ch;
       while(1)
      {
              printf("\nDemo Double Ended Queue Operation");
      printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n 3:InsertQueueRear\n 4:DeleteQueueRear\n 5:DisplayStatus\n 6: Exit \n");
              scanf("%d", &ch);

              switch(ch)
             {
                     case 1: first=insertfront();
                                 break;
                    case 2: first=deletefront();
                               break;
                   case 3: first=insertend();
                               break;
                   case 4: first=deleteend();
                              break;
                    case 5: display();
                               break;
                   default : return;
           }
     }
}

void main()
{
    int ch,i,n;
    while(1)
    {
        printf("\n\n~~~Menu~~~");
        printf("\n1:Create DLL of Employee Nodes");
        printf("\n2:DisplayStatus");
         printf("\n3:InsertAtEnd");
         printf("\n4:DeleteAtEnd");
         printf("\n5:InsertAtFront");
         printf("\n6:DeleteAtFront");
         printf("\n7:Double Ended Queue Demo using DLL");
         printf("\n8:Exit \n");
         printf("\nPlease enter your choice: ");
        scanf("%d",&ch);

        switch(ch)
        {
        case 1 : printf("\nEnter the no of Employees:   ");
                   scanf("%d",&n);
                   for(i=1;i<=n;i++)
                   first = insertend();
                   break;

        case 2:  display();
                   break;

          case 3: first = insertend();
                  break;

          case 4: first = deleteend();
                  break;

          case 5: first = insertfront();
                  break;

          case 6: first = deletefront();
                break;

          case 7: deqdemo();
                  break;

          case 8 : exit(0);
        default: printf("\nPlease Enter the valid choice");
        }
    }
}

Output:
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 1
Enter the no of Employees:   2

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
111
aaa
dept1
des1
1000
11111

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
222
bbb
dept2
des2
2000
22222

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2

ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone no:22222
No of employee nodes is 2

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 3

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
333
ccc
dept3
des3
3000
33333

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2

ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone no:22222
ENode:3||SSN:333|Name:ccc|Department:dept3|Designation:des3|Salary:3000|Phone no:33333
No of employee nodes is 3

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 5

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
444
ddd
dept4
des4
4000
44444

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 2

ENode:1||SSN:444|Name:ddd|Department:dept4|Designation:des4|Salary:4000|Phone no:44444
ENode:2||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone no:11111
ENode:3||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone no:22222
ENode:4||SSN:333|Name:ccc|Department:dept3|Designation:des3|Salary:3000|Phone no:33333
No of employee nodes is 4

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 4

The employee node with the ssn:333 is deleted

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 6

The employee node with the ssn:444 is deleted

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2

ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone no:22222
No of employee nodes is 2

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 7

Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
2

The employee node with the ssn:111 is deleted

Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
4

The employee node with the ssn:222 is deleted

Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
2

Doubly Linked List is empty

Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
6

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 8

Also Credits to,
Manoj T