Thursday 4 August 2016

Arrays Bubble sort

Sorting an Array using Bubble Sort

#include<stdio.h>
void main()
{
         int i, j, n, a[10], temp;
         printf("\nEnter the value of n:");
         scanf("%d", &n);

         printf("\nEnter array elements:");
         for(i=0;  i<n;  i++)
                   scanf("%d", &a[i]);

         for(i=0; i<n-1; i++)
        {
              for(j=0; j<n-1-i; j++)
              {
                    if(a[j+1]<a[j])
                    {
                             temp = a[j+1];
                             a[j+1] = a[j];
                             a[j] = temp;
                     }
                }
         }

          printf("\nBubble sort: array elements:");
          for(i=0;i<n;i++)
                   printf("%d ", a[i]);
 }

Output

Enter the value of n: 7
Enter array elements: 3 2 1 5 6 9 1

Bubble sort: array elements: 1 1 2 3 5 6 9

Arrays Selection Sort

Sorting an Array using Selection Sort

#include<stdio.h>
void main()
{
          int i, j, n, a[10], temp;
          printf("\nEnter the value of n:");
          scanf("%d", &n);

          printf("\nEnter array elements:");
          for(i=0;i<n;i++)
                  scanf("%d", &a[i]);

          for(i=0; i<n-1; i++)
         {
               for(j=i+1; j<n; j++)
               {
                     if(a[j]<a[i])
                    {
                            temp = a[j];
                            a[j] = a[i];
                            a[i] = temp;
                    }
               }
          }

          printf("\nSelection sort: array elements:");
          for(i=0;i<n;i++)
                  printf("%d ", a[i]);
}

Output:
Enter the value of n:7
Enter array elements:3 2 1 5 6 9 1
Selection sort: array elements:1 1 2 3 5 6 9

Union within Structure

Union within Structure



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
            int marks;
            union
            {
                        char name[20];
                        int usn;
            } u;
} s;
main()
{
            int  ch;
            printf("\n Enter marks:");
            scanf("%d", &s.marks);
            printf("\nName or USN choice:");
            printf("\n 1.Name 2.USN: ");
            scanf("%d", &ch);
            switch(ch)
            {
                        case 1:             printf("\nEnter name:");
                                                scanf("%s", s.u.name);
                                                printf("\nYour name is:%s", s.u.name);
                                                break;
                        case 2:             printf("\nEnter usn:");
                                                scanf("%d", &s.u.usn);
                                                printf("\nYour usn is:%d", s.u.usn);
                                                break;
            }
            printf("\nYour marks is:%d", s.marks);
}
Output:
Enter marks: 80
Name or USN choice:
1.Name 2.USN:            1
Enter name: AAAAA
Your name is: AAAAA

Your marks is: 80 

Structure and Union Initialization

Structure and Union Initialization


#include<stdio.h>
struct first
{
            int x, y;
};

union second
{
            int x, y;
};
main()
{
            struct first f1 = {1, 2};
            printf("\nf1.x = %d \t f1.y = %d", f1.x, f1.y);

            union second s1;
            s1.x = 3;
            printf("\ns1.x = %d\t", s1.x);
            s1.y = 4;
            printf("\ns1.y = %d", s1.y);

            union second s2 = {6, 7};
            printf("\ns2.x = %d\t s2.y = %d", s2.x, s2.y);

            union second s3;
            s3.x = 10;
            s3.y = 9;
            printf("\ns3.x = %d\t s3.y = %d", s3.x, s3.y);
}
Output:
f1.x = 1   f1.y = 2
s1.x = 3   s1.y = 4
s2.x = 6   s2.y = 6
s3.x = 9   s3.y = 9

Arrays Binary Search

Search for key Element in an Array using Binary Search


#include<stdio.h>
void main()
{
        int a[100], i, n, low, high, mid, 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);

       low = 0;
       high = n-1;
       while(low<=high)
       {
                        mid = (low+high)/2;
                        if(key == a[mid])
                        {
                                        printf("\nKey element is found at position = %d",mid+1);
                                       break;
                        }
                        else  if(key<a[mid])
                                        high=mid-1;
                        else
                                        low=mid+1;
       }
       if(low>high)
                        printf("\nKey is not found");
}
Output:

Case 1:
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


Case 2:
Enter value of n: 6
Enter the array elements: 11 22 33 44 55 66
Enter key: 88

Key is not found

Unions

The Size and Address of Unions

The Size and Address of Structure and Unions

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

struct stag
{
            char c;
            int i;
            float f;
            double d;
};
union utag
{
            char c;
            int i;
            float f;
            double d;
};
main()
{
            struct stag s;
            printf("\n size of structure=%u", sizeof(struct stag));
            printf("\n size of structure=%u", sizeof(s));
            printf("\n address of structure=%u", &s);
            printf("\n address of structure members: %u %u %u %u", &s.c, &s.i, &s.f, &s.d);

            union utag u;
            printf("\n size of union=%u",sizeof(union utag));
            printf("\n size of union=%u",sizeof(u));
            printf("\n address of union=%u",&u);
            printf("\n address of union members: %u %u %u %u",&u.c,&u.i,&u.f,&u.d);
}

Output:
 size of structure=24
 size of structure=24
 address of structure=2686752
 address of structure members: 2686752      2686756          2686760          2686768

 size of union=8
 size of union=8
 address of union=2686744
 address of union members: 2686744            2686744          2686744          2686744

Pointers

Pointer to Pointer

Pointer to Pointer


#include<stdio.h>
void main()
{
                int a = 5;
                int *p;
                int **q;
                p = &a;
                q = &p;

                printf("\n a = %d", a);
                printf("\n &a = %p", &a);

                printf("\n  p = %p",  p);
                printf("\n  *p = %d",  *p);
                printf("\n  &p = %p", &p);

                printf("\n q = %p", q);
                printf("\n  *q = %p", *q);
                printf("\n  **q = %d", **q);
                printf("\n &q = %p", &q);
}

Output:
 a            = 5
 &a         = 0028FF44

 p            = 0028FF44
 *p          = 5
 &p         = 0028FF40

 q            = 0028FF40
 *q          = 0028FF44
 **q        = 5
 &q         = 0028FF3C

Pointer Arithmetic

Pointer Arithmetic


#include<stdio.h>
void main()
{
                char c = 'x', *pc;
                int i = 11, *pi;
                float f = 35.6, *pf;
                pc = &c;
                pi = &i;
                pf = &f;
                printf("\nValue of pc = Address of c = %p ", pc);
                printf("\nValue of pi = Address of i = %p ", pi);
                printf("\nValue of pf = Address of f = %p ", pf);

                pc++;
                pi++;
                pf++;

                printf("\nNow Value of pc = %p", pc);
                printf("\nNow Value of pi = %p", pi);
                printf("\nNow Value of pf = %p", pf);
}

Output:
Value of pc  =     Address of c       = 0028FF47
Value of pi   =     Address of i        = 0028FF3C
Value of pf   =    Address of f       = 0028FF34
Now Value of pc = 0028FF48
Now Value of pi = 0028FF40
Now Value of pf = 0028FF38

Pointers and address basics

Pointers and Address


#include<stdio.h>
void main()
{
                 int a = 10;
                float b = 35.6;
                int *p1;
                float *p2;
                p1 = &a;
                p2 = &b;
                printf("\nAddress of a = %p ", &a);
                printf("\nAddress of b = %p ", &b);

                printf("\nValue of a = %d %d %d", a, *p1, *(&a));
                printf("\nValue of b = %.1f %.1f %.1f", b, *p2, *(&b));

                printf("\nValue of p1 = Address of a = %p ", p1);
                printf("\nValue of p2 = Address of b = %p ", p2);

                printf("\nAddress of p1 = %p ", &p1);
                printf("\nAddress of p2 = %p ", &p2);
}

Output:
Address of a = 0028FF44
Address of b = 0028FF40
Value of a = 10    10   10
Value of b = 35.6   35.6   35.6
Value of p1 = Address of a = 0028FF44
Value of p2 = Address of b = 0028FF40
Address of p1 = 0028FF3C
Address of p2 = 0028FF38

Wednesday 3 August 2016

Lab Program 5b Tower of Hanoi 15CSL38 Data Structures in C Lab

Lab Program 5b:

5b. Solving Tower of Hanoi problem with n disks 
(using recursion)


#include<stdio.h>
#include<math.h>
void tower(int n, char from_peg,  char aux_peg, char to_peg);

void main()
{
                int n;
                printf("\nEnter the number of disks: ");
                scanf("%d", &n);
                tower(n, 'A', 'B', 'C');                                         // A-> from_peg B->aux_peg C->to_peg
    printf("\nTotal number of moves = %0.0f", pow(2,n)-1 );
}

void tower(int n, char from_peg,  char aux_peg, char to_peg)    
// A-> from_peg B->aux_peg C->to_peg
{
                if(n == 1)
                {
                                printf("\nMove disk %d from %c peg to %c peg", n,  from_peg,  to_peg);
                                return;
                }

                // move n-1 disks from A(from_peg) to B(to_peg) using C(aux_peg) as auxiliary
                tower(n-1, from_peg,  to_peg,  aux_peg); 

                printf("\nMove disk %d from peg %c to %c peg", n, from_peg,  to_peg);

                // move n-1 disks from B(aux_peg) to C(to_peg) using A(from_peg) as auxiliary
                tower(n-1, aux_peg, from_peg, to_peg);
}

Output:
Enter the number of disks: 3
Move disk 1 from A peg to C peg
Move disk 2 from peg A to B peg

Move disk 1 from C peg to B peg
Move disk 3 from peg A to C peg
Move disk 1 from B peg to A peg
Move disk 2 from peg B to C peg
Move disk 1 from A peg to C peg
Total number of moves  = 7


Also Credits to: 
Manoj Taleka  (manoj89biet@gmail.com)

Yashaswini Jogi (jogi.yash@gmail.com)

Lab Program 1 Array 15CSL38 Data Structures in C Lab

Lab Program 1:   



Design, Develop and Implement a menu driven Program in C for the following Array operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations.


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

int a[MAX], pos, elem;
int n = 0;

void create();
void display();
void insert();
void delete();

void main()
{
            int choice;
            while(1)
            {
                        printf("\n\n~~~~MENU~~~~");
                        printf("\n=>1. Create an array of N integers");
                        printf("\n=>2. Display of array elements");
                        printf("\n=>3. Insert ELEM at a given POS");
                         printf("\n=>4. Delete an element at a given POS");
                         printf("\n=>5. Exit");
                         printf("\nEnter your choice: ");
                        scanf("%d", &choice);
                        switch(choice)
                        {
                                    case 1:             create();
                                                            break;
                                    case 2:             display();
                                                            break;
                                    case 3:             insert();
                                                            break;
                                    case 4:             delete();
                                                            break;
                                    case 5:             exit(1);
                                                            break;
                                    default:            printf("\nPlease enter a valid choice:");
                        }
            }
}

void create()
{
            int i;
            printf("\nEnter the number of elements: ");
            scanf("%d", &n);
            printf("\nEnter the elements: ");
            for(i=0; i<n; i++)
            {
                        scanf("%d", &a[i]);
            }
}

void display()
{
            int i;
            if(n == 0)
            {
                        printf("\nNo elements to display");
                        return;
            }
            printf("\nArray elements are: ");
            for(i=0; i<n;i++)
                        printf("%d\t ", a[i]);
}

void insert()
{
            int i;

            if(n == MAX)
            {
                        printf("\nArray is full. Insertion is not possible");
                        return;
            }

            do
            {
                        printf("\nEnter a valid position where element to be inserted:    ");
                        scanf("%d", &pos);
            }while(pos > n);

            printf("\nEnter the value to be inserted:   ");
            scanf("%d", &elem);

            for(i=n-1; i>=pos ; i--)
            {
                        a[i+1] = a[i];
            }
            a[pos] = elem;
            n = n+1;
            display();
}
void delete()
{
            int i;
           
            if(n == 0)
            {
                        printf("\nArray is empty and no elements to delete");
                        return;
            }

            do
            {
                        printf("\nEnter a valid position from where element to be deleted:    ");
                        scanf("%d", &pos);
            }while(pos>=n);

            elem = a[pos];

            printf("\nDeleted element is : %d \n", elem);
            for( i = pos; i< n-1; i++)
            {
                        a[i] = a[i+1];
            }
            n = n-1;
            display();
}

Output:

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 1

Enter the number of elements: 3
Enter the elements: 10 20 30

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 2

Array elements are: 10   20      30

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 3

Enter a valid position where element to be inserted:    5
Enter a valid position where element to be inserted:    4
Enter a valid position where element to be inserted:    3
Enter the value to be inserted:   40

Array elements are: 10   20      30      40

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 3

Enter a valid position where element to be inserted:    4
Enter the value to be inserted:   50

Array elements are: 10   20      30      40      50

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 3

Array is full. Insertion is not possible

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 4

Enter a valid position from where element to be deleted:    5
Enter a valid position from where element to be deleted:    6
Enter a valid position from where element to be deleted:    4
Deleted element is: 50

Array elements are: 10   20      30      40

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 4

Enter a valid position from where element to be deleted:    2
Deleted element is: 30

Array elements are: 10   20      40

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 4

Enter a valid position from where element to be deleted:    1
Deleted element is: 20

Array elements are: 10   40

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 4

Enter a valid position from where element to be deleted:    0
Deleted element is: 10

Array elements are: 40

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 4

Enter a valid position from where element to be deleted:    0
Deleted element is: 40

No elements to display
~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 4

Array is empty and no elements to delete

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 5


Also Credits to: 
Manoj Taleka  (manoj89biet@gmail.com)

Yashaswini Jogi (jogi.yash@gmail.com)