Saturday, 3 August 2013

Facebook Page of Innovators With C

After huge demand of students, we have recently created Facebook Page of Innovators With C for C/C++ programming. On this page we will be updating the information and posts about this website. If you are on facebook, you can like this page just by clicking on LIKE button on the right side of Facebook Page. Innovators With C Programming Facebook Page will provide you material about Programming Language and other related references about C/C++ programming and concepts

Join us on Facebook Now :)

break and continue Statement

There are two statement built in C, break; and continue; to interrupt the normal flow of control of a program. Loops performs a set of operation repeatedly until certain control variable fails but, it is sometimes desirable to skip some statements inside loop and terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used.

C break statement:
In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement.

Syntax of break statement:

break; 
 
The break statement can be used in terminating all three loops for, while and do...while loops.


For better understanding of how break statements works in C programming loops. Analyze the illustration of exiting a loop with break statement below.


C continue statement:
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used. Continue statements are only used in while, do...while and for loops in C programming.

Syntax of continue statement:
continue; 
 
Just like break, continue is also used with conditional if statement.

 For better understanding of how continue statements works in C programming. Analyze the illustration of bypassing and continuing in loops below.



Program to Implement Bubble Sort

#include<stdio.h>
#include<conio.h>
void main()
{           
int a[10],i,j,n,temp;
clrscr();
printf(“Enter the size of arrary\n”);
scanf(“%d”,&n);
printf(“Enter %d elements \n”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j>i;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
printf(“The sorted Array is\n”);
for(j=1;j<=n;j++){
printf(“%d”,a[j]);
}
getch();

}

Program to Implement Binary Search

#include<stdio.h>
#include<conio.h>
void main()
{
int beg, mid, end,a[20],item,loc,i,n;
clrscr();
printf(“Enter the size\n”);
scanf(%d”,&n);
printf(“Enter the elements\n”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
beg=1;
end=n;
mid=(beg+end)/2;
printf(“Enter the item to be searched\n”);
scanf(“%d”,&item);
while((beg<=end)&&(a[mid]!=item))
{
if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
printf(“The item is at mid = %d”,mid);
if(beg>end)
{
loc=0;
}
else
{
loc=mid;
}
printf(“The location of the item\n”);
printf(“%d”, loc);
getch();

Program to Implement the Link List

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct Node
{
int info;
struct Node *link;
};
struct Node *first;
void main()
{
create();
traverse();
insert();
}
void create()
{
struct Node *ptr, *cpt;
char ch;
ptr=(struct Node *) malloc(size of(struct Node));
printf(“Enter the first Node”);
scanf(“%d”,&ptr->info);
first=ptr;
}
do
{
cpt=(struct Node*) malloc(size of(struct Node));
printf(“Enter the next node”);
scanf(“%d”,&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf(“Enter Y to continue”);
scanf(“%c”,&ch);
}
while(ch==’Y’);
ptr->link=first;
}
void traverse()
{
struct Node *ptr;
printf(“Traverse is”);
ptr=first;
while(ptr!=first);
{
printf(“%d”,ptr->info);
ptr=ptr->link;
}
}
void insert()
{
struct Node *ptr;
ptr=(struct Node*)malloc(size of(struct Node));
if(ptr==NULL)
}
printf(“Overflow”);
return;
}
printf(“Enter the info into new”);
scanf(“%d”,&ptr->info);
ptr->link=start;
start=ptr;

}

Algorithm for Searching

This algorithm finds the location LOC of the node where ITEM first appear in the LIST.
  1. PTR=START
  2. Repeat Step 3 and 4 while PTR !=NULL
  3. If ITEM=INFO[PTR]Set LOC = PTR and ExitElsePTR=LINK[PTR]Set LOC=NULL
  4. Exit

Algorithm for Traversing a Link List

Let LIST be a link list, START point to first node in the link list and NULL should be the end of LIST. A pointer variable PTR which point to the node i.e. currently being in process, LINK[PTR] point to the nest node to be process i.e. PTR=LINK[PTR] moved the pointer to the next node in the list.
  1. PTR=START
  2. Repeat Step 3 and 4 while PTR != NULL
  3. Apply process to INFO[PTR]
  4. PTR=LINK[PTR]
  5. Exit

Program to perform an operation on Queue

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,F=0,R=0,n,m,item;
clrscr();
do
{
printf(“Enter your choice 1-Insertion\n 2-Deletion\n 3-Display\n”);
scanf(“%d”,&n);
switch(n)
{
case 1:
{
if(R==0)
{
printf(“Overflow”);
exit (0);
}
else if(F==0 && R==0)
{
F=1;
R=1;
printf(“Enter the element to Insert\n”);
scanf(“%d”,&item);
a[R]=item;
}
else
{
printf(“Enter the element to insert\n”);
scanf(“%d”,&item);
R=R+1;
a[R]=item;
}
break;
}
case 2:
{
if(R==0)
{
printf(“Underflow”);
}
else
{
item=a[F];
F=F+1;
printf(“The deleted element is \n %d\n”,item);
}
break;
}
case 3:
{
for(i=R;i>=F;i--)
{
printf(“%d\n”,a[i]);
}
break;
}
default:
{
printf(“Invalid Input”);
break;
}
}
printf(“Press 5 to continue:\n”);
scanf(“%d”,&n);
}
while(m==5)
getch();

}

Friday, 19 July 2013

Program to Print Even Number Between (1 to 10)

#include <stdio.h>
void main()
{
int a;
clrscr ();
printf("Enter the Value");
for (a=1;a<=10;++a)
{
if (a%2==0)
printf("\n%d",a);
}
getch();

}

Program to Print all Number Divided by 3 (1 to 50)

#include <stdio.h>
void main()
{
int a;
clrscr ();
printf("Enter the Value\n");
for (a=1;a<=50;++a)
{
if (a%3==0)
printf("\n%d",a);
}
getch();

}

Program to Implement Quick Sort

#include <stdio.h>
#include <conio.h>
void quicksort (int a[], int low, int high);
int partition(int a[], int low, int high);
int a[5] = {55, 1, 78, 13, 45};
void main( )
{
clrscr();
int i, n;
printf("\nOriginal array");
for(i=0; i < 5; i++)
printf("%4d", a[i]);
quicksort(a, 0, 4);
printf("\nThe sorted array is \n");
for(i=0; i < 5; i++)
printf("%4d", a[i]);
getch();
}
void quicksort (int a[], int low, int high)
{
int j;
if (low < high)
{
j = partition(a,low, high);
quicksort (a, low, j-1);
quicksort(a, j+1, high);
}
}
int partition(int a[], int low, int high)
{
int i, j, temp, key;
key = a[low];
i = low + 1;
j = high;
while (1)
{
while (i < high && key >= a[i])
i++;
while (key < a[j])
j--;
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
temp = a[low];
a[low] = a[j];
a[j] = temp;
}
return j;
}

}

Program in C to Implement Linear Search Techniques

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, n, item, loc=-1;
clrscr();
printf(“Enter the number of element”);
scanf(“%d”,&n);
printf(“Enter the number\n”);
for(i=0; i<=n-1;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter the number to be selected ”);
scanf(“%d”,&item);
for(i=0;i<=n-1;i++)
{
if(item==a[i])
{
loc=i;
break;
}
}
if(loc>=0)
printf(“\n%d is found in position %d”,item,loc+1);
else
printf(“\nItem does not exist”);
getch();

}

Algorithm for Binary Search

  1. Input an array A of an element an “data” to be sort
  2. LB=0,UB=n; mid = int((LB+UB)/2)
  3. Repeat Step 4 and 5 while(LB<=UB) and (A[Mid])!= data UB= mid-1
  4. If(data<A[mid])UB=mid-1ElseLB=mid+1
  5. Mid=int((LB+UB)/2)
  6. If(A[mid]==data)Printf(“The data is found”);ElsePrintf(“The data is not found”);
  7. Exit

Program in C to Implement Insertion Sort

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j , n, temp,a[10];
clrscr();
printf(“Enter the size of array\n”);
scanf(“%d”,&n);
printf(“Enter %d elements \n”)
for(i=1;i<=n;i++)
{
scanf(%d”, &a[i]);
}
for(i=1;i<=n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j]&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf(“The sorted array is\n”);
for(i=1;i<=n;i++)
{
printf(“%d”,a[i]);
}
getch();

}

Algorithm for Linear Search

  1. Set data[n+1]=item
  2. Set loc=1
  3. Repeat while data[loc]=item
  4. Set loc=loc+1
  5. If loc=n+1, then set loc=0
  6. Exit

Algorithm for Deletion an Element in Queue

  1. If front=Rear=NULLThen print Underflow And Exit
  2. Item =Queue[Rear]
  3. If(Front=Rear=1)Then Set Front=0     Set Rear=0ElseFront= front+1
  4. Exit

Thursday, 18 July 2013

Algorithm for Insertion a Element in Queue

  1. If Front==1&&Rear==Max or Front=Rear+1Then print Overflow and Exit
  2. If Front=NullThen Set Front=Rear=1Else If Rear=Max thenSet Rear=1ElseRear=Rear+1
  3. Queue[Rear]=item
  4. Exit

Program in C to perform PUSH and POP on Stack

#include<stdio.h>
#include<conio.h>
void main()
{
int S[10],MAXSTK,TOP=0,item,n,i,D;
clrscr();
do
{
printf(“Enter the size\n”);
scanf(“%d”,&n);
printf(“Enter the choice \n1-Push \n2-Pop\n3-Display”);
scanf(“%d”,&n);
switch(n)
{
case 1:
{
if(TOP==MAXSTK)
{
printf(“Overflow”);
break;
}
else
{
printf(“Enter the item you want to insert ”);
scanf(“%d”,&item);
TOP=TOP+1;
S[TOP]=item;
break;
}
}
case 2:
{
if(TOP==0)
{
printf(“Underflow”);
}
else
{
item=S[TOP];
TOP=TOP-1;
printf(“Delete the item”,item);
bbreak;
}
}
case 3:
{
for(i=TOP;i>0;i--)
{
scanf(“%d”,S[i]);
break;
}
}
printf(“Enter 4 to continue\n”);
scanf(“%d”,&D);
}
while(D==4);
getch();

}

Algorithm for POP an Element from Stack

POP(S,MAXSTK,TOP,item)
  1. If TOP=NULL thePrint Underflow and Return
  2. Item =S[TOP]
  3. TOP=TOP-1
  4. Exit 

Algorithm for Pushing an Element into Stack

PUSH(s,MAXSTK,TOP,item)
  1. If TOP=MAXSTK thenPrint Overflow and Return
  2. TOP=TOP+1
  3. S[TOP]=item
  4. Exit

Program in C to Merge Two Arrays into Sorted Array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],m,n,i,j,temp=0;
clrscr();
printf(“Entrer the size of array A\n”);
scanf(%d”,&m);
printf(“Enter the sorted element of Array A\n”);
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter the size of array B\n”);
scanf(“%d”, &n);
printf(“Enter the sorted element of Array B\n”);
for(i=0;i<n;i++)
{
scanf(%d”,&b[i]);
}
for(i=0;i<5;i++)
{
c[i]=a[i];
c[i+5]=b[i];
}
printf(“Merge Array is \n”);
for(i=0;i<n;i++)
{
for(j=i+1;j<10;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
for(i=0;i<10;i++)
{
printf(“%d”, c[i]);
}
getch();

}

Algorithm for Merging Two Arrays

  1. Create three arrays a1[100],a2[100],a3[200]
  2. Enter the elements p and q in ascending order in a1 and a2
  3. Initialize m and n(lower bound to 0)
  4. Check if(a[m]<=b[n])[Assign elements of array a1 to a3] a3[c]=a2[n+1]Set c= c+1Else[Assign elements of a2 to a3] a3[c]= a2[n++]Set c= c+1[End of If Structure]
  5. Assign remaining elements of a1 and a2 to a3
  6. Display the elements of a3
  7. End

Program in C to Traverse an Array

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10];
clrscr();
printf(“Enter the length of array\n”);
scanf(“%d”,&n);
printf(“Enter the element of array\n”);
for(i=0;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Traversing of the array\n”);
for(i=0;i<=n-1;i++)
printf(“\n%d”,a[i]);
getch();

Algorithm for Traversing of Array

Let LB be the lower bound and UB be the upper bound of linear array a
  1. [Initialize counter] Set I at lower bound LB
  2. Repeat for i=LB to UB
  3. [visit element] Display a[i]
  4. [End of loop]
  5. Exit

Program in C to Insert an Element at the Specified Position in Array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,n,k,item,pos;
clrscr();
printf(“Enter the element of array=\n”);
scanf(“%d”,&n);
printf(“Enter the element of array=\n”);
for(i=1;i<=n;i++)
{
scanf(%d”,&a[i]);
}
printf(“Enter the position”);
scanf(“%d”,&k);
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
printf(“Enter the value you want to insert\n”);
scanf(“%d”,&item);
for(i=1;i<=n;i++)
{
printf(“Element is %d”,a[i]);
}
getch();

}