Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Thursday 24 October 2013

[C Program] To Implement Selection Sort

//Program for selection sort
#include<stdio.h>
#include<conio.h>

//Main function
void main()
{
int a[10],n,i,temp,j;
clrscr();
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter the elements into array:");
i=0;
while(i<n)
{
scanf(" %d ",&a[i]);
i++;
}
printf("\nEntered array: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nSorted list:   ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}//end of main

/*Output

Enter size of array:5
                                                                               
Enter the elements into array:54 23 99 11 87

Entered array: 54       23      99      11      87

Sorted list:   11       23      54      87      99 */

[C Program] To Implement and Perform Various Operations on Symmetric Matrix.


//Program for symmetric matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,m,n,flag=0;
clrscr();
printf("Enter the order for square matrix:");
scanf("%d %d",&m,&n);
printf("\n\nEnter the elements into matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==a[j][i])
continue;
else
{
flag=1;
goto xyz;
}
}
}

xyz:
if(flag==0)
printf("\nMatrix is Symmetric.");
else
printf("\nMatrix is not symmetric.");
getch();
}

/*Output of Program

Enter the order for square matrix:3 3

Enter the elements into matrix:
1 2 3
2 7 0
3 0 3

Matrix is Symmetric. */

[C Program] for Implementing and performing Various Operations on Stack.


//Program for implementing Stack.

#include<stdio.h>
#include<conio.h>
#define MAX 3

//Defining structure
struct stack
{
int a[MAX];
int top;
};
struct stack s;

//Initializing stack.
void initialize()
{
s.top=-1;
}

//Push function for inserting elements onto stack.
void push(int ele)
{
s.top++;
s.a[s.top]=ele;
}

//Pop function for deleting elements from stack.
int pop()
{
int e;
if(s.top==-1)
{
return(-1);
}
else
{
e=s.a[s.top];
s.top--;
return(e);
}
}

//Displaying the stack.
void display()
{
int i=s.top;
if(s.top==-1)
{
printf("\nStack is empty");
}
else
{
printf("\nElements of stack:\n");
while(i>=0)
{
printf("%d\n",s.a[i]);
i--;
}
}

}

//Main function.
void main()
{
int ch,ele,e;
clrscr();
printf("\n 1.Push 2.Pop 3.Display 4.Exit \n");
initialize();
do
{
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: if(s.top==(MAX-1))
printf("\nStack is full");
else
{
printf("\nEnter element to be inserted in the stack:");
scanf("%d",&ele);
push(ele);
}
break;
case 2: e=pop();
if(e>-1)
printf("\nDeleted element:%d",e);
else
printf("\nStack is Empty");
break;
case 3: display();
break;
case 4: exit(0);
}
}while(ch<4);
getch();
}//end of main.

/*Output of Program:

1.Push 2.Pop 3.Display 4.Exit

Enter your choice:1
Enter element to be inserted in the stack:12

Enter your choice:1
Enter element to be inserted in the stack:65

Enter your choice:3
Elements of stack:
65
12

Enter your choice:1
Enter element to be inserted in the stack:34

Enter your choice:3
Elements of stack:
34
65
12

Enter your choice:1
Stack is full

Enter your choice:2
Deleted element:34

Enter your choice:3
Elements of stack:
65
12

Enter your choice:2
Deleted element:65

Enter your choice:2
Deleted element:12

Enter your choice:2
Stack is Empty

Enter your choice:4*/

[C Program] To Perform Various Operations on Queue.



//Program for operations on queue.

#include<stdio.h>
#include<conio.h>

//Defining the structure.
struct queue
{
int data[3];
int front;
int rear;
}q;

//Initializing the queue.
void initialize()
{
q.rear=-1;
q.front=-1;
}

//Insertion of elements into queue.
void enqueue()
{
if (q.rear==2)
{
printf("\n Queue is full");
}
else
{
int ele;
printf("\nEnter the element:");
scanf("%d",&ele);
if(q.front==-1)
{
q.front++;
}
q.rear++;
q.data[q.rear]=ele;
}
}

// Deletion of elements from queue.
int dequeue()
{
int v;
if(q.front==-1||q.front>q.rear)
{
printf("\n Queue is empty:");
return(-1);
}
else
{
v=q.data[q.front];
q.front++;
return(v);
}
}

//Displaying the queue.
void display()
{
int i=q.front;
if(q.front>q.rear)
printf("\n Queue is empty:");
else
{
while(i<=q.rear)
{
printf("  %d",q.data[i]);
i++;
}
}
}

//Main Function
void main()
{
int i,data[3],ch,v;
clrscr();
initialize();
printf("\n 1.Insert 2.Delete 3.Display 4.Exit:");
do
{
printf("\n\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{

case 1: enqueue();
break;
case 2: v=dequeue();
   if(v==-1)
   break;
   printf("Deleted element:%d",v);
   break;
case 3: display();
   break;
case 4:  exit(0);
}
}while(ch<4);
getch();
}
//end of main

/*Output of Program:

 1.Insert 2.Delete 3.Display 4.Exit:

Enter the choice:1
Enter the element:10

Enter the choice:1
Enter the element:20

Enter the choice:1
Enter the element:30

Enter the choice:1
Queue is full

Enter the choice:3
Elements in queue:  10  20  30

Enter the choice:2
Deleted element:10

Enter the choice:3
Elements in queue:  20  30

Enter the choice:1
Queue is full

Enter the choice:2
Deleted element:20

Enter the choice:2
Deleted element:30

Enter the choice:2
Queue is empty

Enter the choice:3
Queue is empty

Enter the choice:4*/

[C Program] to Implement Pointer to Function

//Program to implement pointer to function

#include<stdio.h>
#include<conio.h>

void copy(int arr[],int b)
{
int i;
for(i=0;i<b;i++)
{
if(i%2==0)
arr[i]=0;
}
}
void main()
{
int j,a[10],n;
void(*ptr)(int[],int);
clrscr();
ptr=&copy;
printf("Enter the size of an array:");
scanf("%d",&n);
printf("\n\nEnter %d elements into array:",n);
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
printf("\n\nEntered array: ");
for(j=0;j<n;j++)
{
printf("%d\t",a[j]);
}
ptr(a,n);
printf("\n\nUpdated array:  ");
for(j=0;j<n;j++)
{
printf("%d\t",a[j]);
}
getch();
}

/*Output of Program

Enter the size of an array:5
                                                                     
Enter 5 elements into array:12 23 34 45 67                                    
                                                                               
Entered array: 12       23      34      45      67                            
                                                                               
Updated array:  0       23      0       45      0 */

[C Program] to Pass Pointer to the Function


//Program to pass pointer to the function.
#include<stdio.h>
#include<conio.h>

//Declaring the swap function.
void swap(int *ptr,int *ptr1);

//Main function.
void main()
{
int a,b;
clrscr();
printf("Enter value of a:");
scanf("%d",&a);
printf("\nEnter value of b:");
scanf("%d",&b);
swap(&a,&b);
printf("\nAfter swapping, values of a & b:%d & %d respectively\n",a,b);
getch();
}//end of main

//Definition of swap function.
void swap(int *ptr,int *ptr1)
{
int temp;
temp=*ptr;
*ptr=*ptr1;
*ptr1=temp;
}

/*Output of Program

Enter value of a:67

Enter value of b:89

After swapping, values of a & b:89 & 67 respectively */                                   

[C Program] To Perform Insertion Sort

//Program for insertion sort

#include<stdio.h>
#include<conio.h>
void insertion(int [], int size);
int main()
{
int a[30],i,size;
clrscr();
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf("Enter elements:");
for(i=0; i<size; i++)
{

scanf("%d",&a[i]);
}
insertion(a,size);
printf("\nSorted elements:\n\n");
for(i=0; i<size; i++)
printf(" %d",a[i]);
getch();
return 0;
}

void insertion(int a[], int n)
{
int i,j,tmp;
for(i=0;i<n;i++)
{

for(j=i-1;j>-1;j--)
{

if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
else
break;
}
}
}

/*Output of Program:

Enter the array size:6

Enter the elements into array:31  56  80  111  65  71

Entered array:  31      56      80      111     65      71

Sorted list:       31      56      65      71      80      111*/

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine