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