Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Friday, April 15, 2016

C program to sort an array using bubble sort using recursive technique

Problem Statement:- we can an array of some integers/float datatype and we need to sort that
array using bubble sort recursively.
Bubble Sort- in bubble what we do is compare first two elements and sort them in sequence and then compare element number two and three and sort them we countinue this upto last element and we get largest element in last position in our first pass in next pass what we do is continue to do above process for the n-1 elements and we get second largest element in its place as we continue out sort and in nth pass we get our list sorted.
Recursive - the function which calls itself

#include<stdio.h>
#include<conio.h>
void sort(int arr_new[],int i,int n)
{
 int temp;
 if(i<n-1)
  {
   if(arr_new[i]>arr_new[i+1])
    {
     temp=arr_new[i];
     arr_new[i]=arr_new[i+1];
     arr_new[i+1]=temp;
    }
   sort(arr_new,++i,n);
   sort(arr_new,0,n-i);
  }
}
void main()
{
 clrscr();
 int arr[10]={3,8,7,6,0,77,3,90,3,13};
 int i;
 printf("Array elements before sorting");
 for(i=0;i<10;i++){
  printf(" %d",arr[i]);
 }
 sort(arr,0,10);
 printf("Array elements after sorting");
 for(i=0;i<10;i++)
  {
   printf(" %d",arr[i]);
  }
 getch();
}

output:-
Array elements before sorting
3 8 7 6 0 77 3 90 3 13
Array elements after sorting
0 3 3 3 6 7 813 77 90

Monday, April 11, 2016

c program to reverse the given array in minimum possible complexity

#include<stdio.h>
void main()
{
 int a[10]={7,9,6,8,3,9,6,1,9,5};
 int i;
 int n=10;
 int temp;
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
 for(i=0;i<n/2;i++)
  {
   temp=a[n-i-1];
   a[n-i-1]=a[i];
   a[i]=temp;
  }
 printf("\n");
 printf("Reverse array: ");
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}

output
7 9 6 8 3 9 6 1 9 5
Reverse array: 5 9 1 6 9 3 8 6 9 7

Tuesday, April 5, 2016

C program to shift all the negative values in left in array

#include<stdio.h>
void main()
{
 clrscr();
 int n;
 int a[10]={4,8,6,-9,0,6,11,-4,3,7};
 int i,j;
 int count=0;
 int temp;
 for(i=0;i<10;i++)
  {
   printf("%d ",a[i]);
  }
 if(a[0]<0)
  {
   count=1;
  }
 for(i=1;i<10;i++)
  {
   if(a[i]<0)
    {
     for(j=i;j>count;j--)
      {
       temp=a[j];
       a[j]=a[j-1];
       a[j-1]=temp;
      }
     count=count+1;
    }
  }
 printf("\n");
 for(i=0;i<10;i++)
  {
   printf("%d ",a[i]);
  }
}

output
-9 -4 4 8 6 0 6 11 3 7

Tuesday, March 29, 2016

C program to implement multiplication between two 2D matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,m,n,a[20][20],b[20][20],c[20][20],i,j,k,sum;
printf("enter dimension of first matrix");
scanf("%d%d",&m,&n);
printf("enter values of first matrix");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter dimension of second matrix");
scanf("%d%d",&p,&q);
printf("enter elements of 2nd matrix");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
sum=0;
for(k=1;k<=n;k++)
{
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("multiplication is ");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=q;j++)
{
printf("%d ",c[i][j]);
}
}
getch();
}
output
Enter dimension of first matrix 2 3
enter values of first matrix 1 2 3 4 5 6
Enter dimension of second matrix 3 2
enter values of 2nd matix 1 2 3 4 5 6
multiplication is
22 28
49 64

C program to implement merge sort using recursive technique

#include<stdio.h>
#include<conio.h>
void merge_sequence(int [],int,int,int);
void merge(int arr_new[],int p, int q)
{
 int mid;
 if(p<q)
  {
   mid=(q+p)/2;
   merge(arr_new,p,mid);
   merge(arr_new,mid+1,q);
   merge_sequence(arr_new,p,mid,q);
  }
}
void merge_sequence(int arr_temp[],int l, int m,int r)
{
 int n1,n2;
 int i,j,k;
 int l_arr[100],r_arr[100];
 n1=m-l+1;
 n2=r-m;
 for(i=0;i<n1;i++)
  {
   l_arr[i]=arr_temp[l+i];
  }
 for(j=0;j<n2;j++)
  {
   r_arr[j]=arr_temp[m+j+1];
  }
 i=0;
 j=0;
 k=l;
 while(i<n1&&j<n2)
  {
   if(l_arr[i]<=r_arr[j])
    {
     arr_temp[k]=l_arr[i++];
     k=k+1;
    }
   else
    {
     arr_temp[k]=r_arr[j++];
     k=k+1;
    }
  }
 while(i<n1)
  {
   arr_temp[k]=l_arr[i++];
   k=k+1;
  }
 while(j<n2)
  {
   arr_temp[k]=r_arr[j++];
   k=k+1;
  }
}
void main()
{
 clrscr();
 int arr[10]={5,9,7,0,3,8,9,66,90,2};
 int i,j;
 printf("input array :");
 for(i=0;i<10;i++)
  {
   printf(" %d",arr[i]);
  }
 merge(arr,0,9);
 printf("\n");
printf("sorted array:");
 for(i=0;i<10;i++)
  {
   printf(" %d",arr[i]);
  }
 getch();
}

output
input array :5 9 7 0 3 8 9 66 90 2
sorted array :0 2 3 5 7 8 9 9 66 90

Tuesday, March 15, 2016

C program to check whether a matrix exist in another matrix or not

#include<stdio.h>
void main()
{
 int const size =5;
 int i,j;
 int n=5;
 int a[size][size];
 int b[3][3]={{1,2,3},{6,7,8},{11,12,13}};
 int count;
 for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
    {
     a[i][j]=n*i+j;
    }
  }
 int k,l;
 int o,p;
 for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
    {
     count=0;
     for(o=0;o<3;o++)
      {
       for(p=0;p<3;p++)
{
if(((i+o)<n)&&((j+p)<n))
 {
 if(a[i+o][j+p]==b[o][p])
  {
   count++;
  }
 else
  {
   break;
  }
 }
else
 {
  break;
 }
}
      }
     if(count==9)
      {
       printf("Same pattern in the array found");
      }
    }
  }
}
output
Same pattern in the array found

Sunday, March 13, 2016

C program to implement binary search on array

#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[5][5];
 int i,j;
 int n=5,m=5;
 int mid,low,high,item;
 printf("Enter a number to search");
 scanf("%d",&item);
 printf("\nArray elements");
 for(i=1;i<=n;i++)
  for(j=1;j<=m;j++)
   {
    a[i-1][j-1]=(i-1)*5+j;
    printf("%d ",a[i-1][j-1]);
   }
 low=0;
 high=n*m-1;
 mid=(low+high)/2;
 i=mid/5;
 j=mid%5;
 while((low<high)&&(a[i][j]!=item))
  {
   if(a[i][j]<item)
    {
     low=i*5+j+1;
     mid=(low+high)/2;
     i=mid/5;
     j=mid%5;
    }
   else
    {
     high=i*5+j-1;
     mid=(low+high)/2;
     i=mid/5;
     j=mid%5;
    }
  }
 if(a[i][j]==item)
  {
   printf("\nitem found at %d",i*5+j+1);
  }
 else
  {
   printf("\nitem not fount");
  }
 getch();
}

output

Saturday, March 12, 2016

C program to dyncamically allocate and deallocate memory in an array.

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *a,*b;
 int x,y,i;
 printf("Please enter the size of array");
 scanf("%d",&x);
 a=(int*)malloc(x*sizeof(int));
 printf("\n Enter %d values in array",x);
 for(i=0;i<x;i++)
  {
   scanf("%d",(a+i));
  }
 printf("\nValue in the array is");
 for(i=0;i<x;i++)
  {
   printf("%d ",*(a+i));
  }
 free(a);
 }

output

Friday, March 11, 2016

C program to show add, append, sort, delete, display, search operation on array

#include<iostream.h>
#include<conio.h>
class List
 {
  int a[50];
  int c;
  public:
   void add(int x);
   void deleteall();
   int delete_index(int index);
   void delete_number(int x);
   int search(int x);
   void insert(int x,int index);
   void sort();
   void append(int x);
   void display();
   int get(int index);
   List();
 };
void List::List()
 {
  c=-1;
 }
void List::add(int x)
 {
  c++;
  a[c]=x;
 }
void List::deleteall()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    a[i]=NULL;
   }
  c=-1;
 }
int List::delete_index(int index)
 {
  if(index>c)
   {
    cout<<"\nIndex is out of bound";
    return -1;
   }
  else
   {
    int r=a[index];
    int i;
    for(i=index;i<c;i++)
     {
      a[i]=a[i+1];
     }
    c--;
    return r;
   }
 }
void List::delete_number(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    for(int k=i;k<c;k++)
     {
      a[k]=a[k+1];
     }
     c--;
     cout<<"\nNumber deleted at index "<<i;
   }
  else
   {
    cout<<"\nEntered number not present";
   }
 }
int List::search(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    return i;
   }
  else
   {
    return -1;
   }
 }
void List::insert(int x,int index)
 {
  int i;
  if(index>(c+1))
   {
    cout<<"\nList is shorter";
   }
  else
   {
    for(i=c;i>=index;i--)
     {
      a[i+1]=a[i];
     }
    a[index]=x;
    c++;
   }
 }
void List::sort()
 {
  int i;
  int j;
  int temp;
  for(i=0;i<c;i++)
   {
    for(j=0;j<c-i;j++)
     {
      if(a[j]>a[j+1])
       {
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
       }
     }
   }
 }
void List::append(int x)
 {
  c++;
  a[c]=x;
 }
void List::display()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    cout<<"\t"<<a[i];
   }
 }
int List::get(int index)
 {
  return (a[index]);
 }
void main()
 {
  int ch;
  int no,in,x;
  List List_object;
  do
   {
    cout<<endl<<"1.Add an item\n 2.Delete All \n3.Delete at perticular index\n4.Delete perticular number \n5.Search a number \n6.Insert a number at perticular index \n7.Sort List \n8.Append an item \n9.Display list \n10.Get item at perticular index";
    cin>>ch;
    switch(ch)
     {
      case 1:
       cout<<"\nEnter a number";
       cin>>no;
       List_object.add(no);
       break;
      case 2:
       List_object.deleteall();
       break;
      case 3:
       cout<<"\nEnter Index to delete";
       cin>>in;
       x=List_object.delete_index(in);
       cout<<"\nDeleted number="<<x;
       break;
      case 4:
       cout<<"\nEnter number to delete";
       cin>>no;
       List_object.delete_number(no);//Bool
       break;
      case 5:
       cout<<"\nEnter number to search";
       cin>>no;
       x=List_object.search(no);
       if(x==-1)
{
cout<<"\nNumber not present";
}
       else
{
cout<<"\nNumber present at "<<x;
}
       break;
      case 6:
       cout<<"\nEnter no and index";
       cin>>no>>in;
       List_object.insert(no,in);
      break;
     case 7:
       List_object.sort();
       List_object.display();
       break;
     case 8:
       cout<<"\nEnter a no to append";
       cin>>no;
       List_object.append(no);
       break;
     case 9:
       List_object.display();
       break;
     case 10:
       cout<<"\nEnter index to find number";
       cin>>in;
       x=List_object.get(in);
       cout<<"\nNumber is "<<x;
     }
   }while(ch!=11);
  getch();
 }

output
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 1
 Enter a number 6
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 8
 Enter a no to append 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 9
 6 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 5 
 Enter number to search 8
 Number not present
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 11