Friday, 24 May 2013

POP operation on stack


#include<stdio.h>
#include<conio.h>
void main()
{
int stack[10]={1,2,3,4,5};
int i,n,item,top=4;
do
{
if(top<0)
{
printf("Underflow");
getch();
break;
}
else
{
top=top-1;
printf("\n\nStack elements are");
for(i=top;i>=0;i--)
{
printf("\n%d",stack[i]);
}
}
printf("\n\nDo you want to delete more elements from stack(1 for yes 2 for no)");
scanf("%d",&n);
}
while(n==1);
}

PUSH Operation on stack


#include<stdio.h>
#include<conio.h>
void main()
{
int stack[10];
int i,n,max=9,top=-1,item;
clrscr();
do
{
if(top==max)
{
 printf("\nOVERFLOW");
 printf("Press any key to continue");
 getch();
 break;
}
else
{
 top=top+1;
 printf("Enter the element to be inserted");
 scanf("%d",&item);
 stack[top]=item;
 printf("Stack elements are");
 for(i=top;i>=0;i--)
 {
 printf("\n%d",stack[i]);
 }
}
printf("\n\nDo you want to insert more element(Press 1 for Yes 2 for No");
scanf("%d",&n);
}
while(n==1);
}

Multiplication of two matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],mul[3][3];
printf("Enter the elements of array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the elements of array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array A are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,a[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array B are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,b[i][j]);
}
printf("\n");
}


printf("\n\nMultiplication of two matrices is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
mul[i][j]=a[i][j]*b[i][j];
printf("\na[%d][%d] = %d ",i,j,mul[i][j]);

}
printf("\n");
}

getch();
}

Diffference of two matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],diff[3][3];
printf("Enter the elements of array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the elements of array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array A are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,a[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array B are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,b[i][j]);
}
printf("\n");
}


printf("\n\ndiffrence of two matrices is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
diff[i][j]=a[i][j]-b[i][j];
printf("\na[%d][%d] = %d ",i,j,diff[i][j]);

}
printf("\n");
}

getch();
}

Addition of two Matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],sum[3][3];
printf("Enter the elements of array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the elements of array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array A are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,a[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array B are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,b[i][j]);
}
printf("\n");
}


printf("\n\nSum of two matrices is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("\na[%d][%d] = %d ",i,j,sum[i][j]);

}
printf("\n");
}

getch();
}
                               

Thursday, 23 May 2013

Binary Search

Binary search technique is more efficient than the linear search. In binary search each and every element is not traversed to find the required element.
The main limitation of binary search technique is that it can process only sorted data.The data need to be in sorted form so that binary search can be applied to it.If data is not sorted then it needs to be sorted first.
In Binary search the middle element is found first. Basically there are three values required.
Upper_bound (ub),Lower_bound(lb),Middleelemnt(mid)
mid=(ub+lb)/2;
if the middle element is less than the element to be searched then
lb=mid+1..
 and id middle element is greater than the element to be searched then.
ub=mid-1;

Program for Binary Search

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int flag,n,i,item,mid,lb,ub;
clrscr();
printf("Enter the number of elements you wnat to enter");
scanf("%d",&n);
printf("\nEnter the elements of arrayin sorted form\n");
for(i=0;i<n;i++)
{
 printf("a[%d] =",i);
 scanf("%d",&a[i]);
}

printf("\nThe elements of array are");
for(i=0;i<n;i++)
{
 printf("\na[%d] = %d",i,a[i]);
}

printf("\nEnter the elements of array to be searched");
scanf("%d",&item);

lb=0;
ub=n-1;
while(ub>=lb)
{
mid=(ub+lb)/2;

if(item==a[mid])
{
flag=1;
break;
}
else
{
if(item>a[mid])
{
 lb=mid+1;
}
else
{
ub=mid-1;
}
}
}
if(flag==1)
{
printf("\n Item %d found at location a[%d]",item,mid);
}
else
{
printf("\nItem not found");
}
getch();
}

                                 

Wednesday, 22 May 2013

Linear search

Linear search is one of the searching technique using a simple logic of matching each element of the array with the item to be searched.Linear search can be performed on any type of array.. the array may be sorted or unsorted..it works on both.
This method s not efficient when the number of elements is very large. When processing huge amount of data this method gets very slow because it is very time and resource  consuming as each and every element is traversed thus is not used for processing huge data.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int n,i,item,k=0,j;
clrscr();
printf("Enter the number of elements you wnat to enter");
scanf("%d",&n);
printf("\nEnter the elements of array\n");
for(i=0;i<n;i++)
{
 printf("a[%d] =",i);
 scanf("%d",&a[i]);
}

printf("\nThe elements of array are");
for(i=0;i<n;i++)
{
 printf("\na[%d] = %d",i,a[i]);
}

printf("\nEnter the elements of array to be searched");
scanf("%d",&item);
for(k=0;k<n;k++)
{
if(a[k]==item)
{
j=1;
break;
}
}
if(j==1)
printf("success");
else
printf("no success");
getch();
}

Monday, 20 May 2013

Bubble Sort


#include<stdio.h>
#include<conio.h>
void main()
{
int a[50];
int i,n,j,k,x,temp;
clrscr();
printf("Enter the number of elements you want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d] =",i);
scanf(" %d",&a[i]);
}

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

 printf("\n\nSORTED ARRAY IS:\n\n");
 for(x=0;x<n;x++)
 {
 printf("\na[%d] = %d",x,a[x]);
 }
getch();
}



Selection Sort


#include<stdio.h>
#include<conio.h>
void main()
{
int a[50];
int i,n,j,k,x,temp;
printf("Enter the number of elements you want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d] =",i);
scanf(" %d",&a[i]);
}

for(j=0;j<n-1;j++)
 {
 for(k=j+1;k<n;k++)
  {
  if(a[j]>a[k])
   {
   temp=a[j];
   a[j]=a[k];
   a[k]=temp;
   }
  }
 }
 printf("\n\nSORTED ARRAY IS:\n\n");
 for(x=0;x<n;x++)
 {
 printf("\na[%d] = %d",x,a[x]);
 }
getch();
}

Insert an element at desired position in array



#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[10]={22,33,44,55,77,88};
 int i,j,k,n,t,x,z=0;
 printf("The elements of array are");
 for(i=0;i<=5;i++)
 {
   printf("\na[%d]= %d",i,a[i]);
 }
 printf("\nenter the location where you want to insert the element in the array");
 scanf("%d",&k);

 for(j=i-1;j>=k;j--)
  {
   a[j+1]=a[j];
   printf("\nNow a[%d] contains %d",j+1,a[j]);
  }
 printf("\nNow a[%d] contains %d",j+1,z);

 for(j=k-1;j>=0;j--)
 {
  printf("\nNow a[%d] contains %d",j,a[j]);
 }
 printf("enter the element to be inserted into the array");
 scanf("%d",&n);
 a[k]=n;
 t=i+1;
 printf("\n\nNew array after insertion is\n\n");
 for(i=0;i<t;i++)
  {
  printf("\nNow a[%d] contains %d",i,a[i]);
 }
 getch();
}





Saturday, 18 May 2013

Insertion of an element at last of array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={1,2,3,4,5};
int n;
printf("\nAt present elements of array are");
for(int i=0;i<=4;i++)
{
printf("\n%d",a[i]);
}
printf("\nEnter the element you want to insert");
scanf("%d",&n);
a[i]=n;
printf("\n New array after insertion is");
for(i=0;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}

Thursday, 16 May 2013

Calculate number of students having marks more than 60


#include<stdio.h>
#include<conio.h>
void main()
{
int marks[10],i,n=0,m;
clrscr();
printf("CALCULATE NUMBER OF STUDENTS HAVING MARKS >60");

for(i=0;i<=9;i++)
{
printf("\nEnter marks of student number %d",i+1);
scanf("%d",&marks[i]);
}
for(i=0;i<=9;i++)
{
if(marks[i]>60)
{
n++;
}
}
printf("\nNumber of students having marks more than 60 are : %d",n);
getch();
}

Accessing array elements using base address


#include<stdio.h>
#include<conio.h>
void main()
{
int data[5]={1,2,3,4,5};
int i;
int *base_add;
base_add=data;
clrscr();
printf("\t ACCESSING ARRAY ELEMENT USING BASE ADDRESS\N");
printf("\nHere base address is %u",base_add);
for(i=0;i<=4;i++)
{
printf("\nAt address= %u",base_add);
printf("\nWe have data[%d] i.e %d",i,*base_add);
base_add++;
}
getch();
}

Traversing an Array

Traversing an array means to access each and very element of array .It can be accessed using for loops.The data stored in array can be travesred by using for loop and the elements can be accessed by incerement the for loop. The loop starts to traverse array from the start of the array and continue to traverse array until the for lopp continues its execution until the for loop ends.UIn the program below the elements are stored in an array and are travesred using for loop . The loop prints all the elements of array using traversing.
 

Program for traversing an array.

#include<iostream.h>
#include<conio.h>
void main()
{
int data[5]={1,2,3,4,5};
int i;
clrscr();
cout<<""Elements of array Data are";
for(i=0;i<=4;i++)
{
cout<<data[i];
}
getch();
}

 

Find average of two numbers

#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,average;
cout<<"Enter two numbers";
cin>>a>>b;
average=(a+b)/2;
cout<<"\n Average of two numbers is"<<average;
getch();
}

Wednesday, 15 May 2013

A simple "Hello World" program

#include<iostream.h>  //Header file
#include<conio.h>
void main()                // main function
{
cout<<"Hello World";
getch();
}



See also:
Find average of two numbers


                                          

Swapping value of two variables without third variable


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<"enter the value of a";
cin>>a;
cout<<"\n Enter b";
cin>>b;  
a=a+b;  
b=a+b;  

a=b-a;    
b=-1*(a-b)-a;

cout<<"\n a is"<<a;
cout<<"\n b is"<<b;
getch();
}

full details in json
Proudly Powered by Blogger.
back to top