Thursday, September 27, 2012

C program to delete the specified integer from the Array.

#include <stdio.h>

void main()
{
int vectx[10];
int i, n, pos, element, found = 0;

printf("Enter how many elements\n");
scanf("%d", &n);

printf("Enter the elements\n");
for(i=0; i<n; i++)
{
scanf("%d", &vectx[i]);
}

printf("Input array elements are\n");
for(i=0; i<n; i++)
{
printf("%d\n", vectx[i]);
}

printf("Enter the element to be deleted\n");
scanf("%d",&element);

for(i=0; i<n; i++)
{
if ( vectx[i] == element)
{
found = 1;
pos = i;
break;
}
}

if (found == 1)
{
for(i=pos; i< n-1; i++)
{
vectx[i] = vectx[i+1];
}

printf("The resultant vector is \n");
for(i=0; i<n-1; i++)
{
printf("%d\n",vectx[i]);
}
}
else
printf("Element %d is not found in the vector\n", element);

}

RECURSION determine if two arrays are identic


#include <stdio.h>
int kontrollo(int a[], int b[], int n)
{
if(n==1)
{
    if(a[0]=b[0])
    return 1;
    else return 0;
}
else    if(a[n-1]==b[n-1])
    return (kontrollo(a, b, n-1));
else
    return 0;

}
int main()
{
    int a[10]={2, 25, 75};
    int  b[10]={2, 25, 75};
    int u=3;
    int x;
    x=(kontrollo(a, b, u));
    printf("%d", x);



    return 0;

}

Sunday, September 23, 2012

C PROGRAMM TO FIND MAXIMUM SUM OF COLUMN


#include <stdio.h>
int main()
{
    int v[20][20], i, j, m, n, max;
    int a=1, b[20];
    printf("GIVE NUMBER OF  ROWS AND COLUMNS");
    scanf("%d %d",&m, &n);
    printf("elementet e matr");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    scanf("%d", &v[i][j]);
    for(j=0;j<n;j++)
    {
        for(i=0;i<m;i++)
        a=a*v[i][j];
        b[j]=a;
    }
    max=b[0];
    for(j=1;j<n;j++)
    {
        if(b[j]>max)
        max=b[j];
    }
    printf("MAX IS %d" ,max);

}

Recursion how to Find the Shortest distance of the flight


/* Hill-climbing */
#include <stdio.h>
#include <string.h>
#define MAX 100
/* structure of the flight database */
struct FL {
char from[20];
char to[20];
int distance;
char skip; /* used for backtracking */
};
struct FL flight[MAX]; /* array of db structures */
int f_pos = 0; /* number of entries in flight db */
int find_pos = 0; /* index for searching flight db */
int tos = 0; /* top of stack */
struct stack {
char from[20];
char to[20];
int dist;
} ;
struct stack bt_stack[MAX]; /* backtrack stack */
void setup(void), route(char *to);
void assert_flight(char *from, char *to, int dist);
void push(char *from, char *to, int dist);
void pop(char *from, char *to, int *dist);
void isflight(char *from, char *to);
int find(char *from, char *anywhere);
int match(char *from, char *to);

int main(void)
{
char from[20], to[20];
setup();
printf("From? ");
gets(from);
printf("To? ");
gets(to);
isflight(from,to);
route(to);
return 0;
}
/* Initialize the flight database. */
void setup(void)
{
assert_flight("New York", "Chicago", 1000);
assert_flight("Chicago", "Denver", 1000);
assert_flight("New York", "Toronto", 800);
assert_flight("New York", "Denver", 1900);
assert_flight("Toronto", "Calgary", 1500);
assert_flight("Toronto", "Los Angeles", 1800);
assert_flight("Toronto", "Chicago", 500);
assert_flight("Denver", "Urbana", 1000);
assert_flight("Denver", "Houston", 1500);
assert_flight("Houston", "Los Angeles", 1500);
assert_flight("Denver", "Los Angeles", 1000);
}
/* Put facts into the database. */
void assert_flight(char *from, char *to, int dist)
{
if(f_pos < MAX) {
strcpy(flight[f_pos].from, from);
strcpy(flight[f_pos].to, to);
flight[f_pos].distance = dist;
flight[f_pos].skip = 0;
f_pos++;
}
else printf("Flight database full.\n");
}
/* Show the route and the total distance. */
void route(char *to)
{
int dist, t;
dist = 0;
t = 0;
while(t < tos) {
printf("%s to ", bt_stack[t].from);
dist += bt_stack[t].dist;
t++;
}
printf("%s\n", to);
printf("Distance is %d.\n", dist);
}
/* If flight between from and to, then return
the distance of flight; otherwise, return 0. */
int match(char *from, char *to)
{
register int t;
for(t=f_pos-1; t > -1; t--)
if(!strcmp(flight[t].from, from) &&
!strcmp(flight[t].to, to)) return flight[t].distance;
return 0; /* not found */
}
/* Given from, find the farthest away "anywhere". */
int find(char *from, char *anywhere)
{
int pos, dist;
pos=dist = 0;
find_pos = 0;
while(find_pos < f_pos) {
if(!strcmp(flight[find_pos].from, from) &&
!flight[find_pos].skip) {
if(flight[find_pos].distance>dist) {
pos = find_pos;
dist = flight[find_pos].distance;
}
}
find_pos++;
}
if(pos) {
strcpy(anywhere, flight[pos].to);
flight[pos].skip = 1;
return flight[pos].distance;
}
return 0;
}
/* Determine if there is a route between from and to. */
void isflight(char *from, char *to)
{
int d, dist;
char anywhere[20];
if(d=match(from, to)) {
/* is goal */
push(from, to, d);
return;
}
/* find any connection */
if(dist=find(from, anywhere)) {
push(from, to, dist);
isflight(anywhere, to);
}
else if(tos > 0) {
pop(from, to, &dist);
isflight(from, to);
}
}
/* Stack Routines */
void push(char *from, char *to, int dist)
{
if(tos < MAX) {
strcpy(bt_stack[tos].from, from);
strcpy(bt_stack[tos].to, to);
bt_stack[tos].dist = dist;
tos++;
}
else printf("Stack full.\n");
}
void pop(char *from, char *to, int *dist)
{
if(tos > 0) {
tos--;
strcpy(from, bt_stack[tos].from);
strcpy(to, bt_stack[tos].to);
*dist = bt_stack[tos].dist;
}
else printf("Stack underflow.\n");
}

Saturday, September 22, 2012

Swapping numbers using call by reference



//Swapping numbers using call by reference

#include <stdio.h>

void swap(int*, int*);

int main()
{
   int x, y;

   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(&x, &y);

   printf("After Swapping\nx = %d\ny = %d\n", x, y);

   return 0;
}

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b = *a;
   *a = temp;
}

C Program To Sort An Array Using Bubble Sort


#include<stdio.h>
int main() {
 int a[50], n, i, j, temp = 0;

 printf("Enter how many numbers you want:\n");
 scanf("%d", &n);
 printf("Enter the %d elements:\n", n);
 for (i = 0; i < n; i++) {
  scanf("%d", &a[i]);
 }
 printf("\n\t\tThe given array is:\n");
 for (i = 0; i < n; i++) {
  printf("\n\t\t%d", 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\n\n\t\tThe sorted array using Buble sort is:\n");
 for (i = 0; i < n; i++) {
  printf("\n\t\t%d", a[i]);
 }
 return 0;
}

Friday, September 21, 2012

c program to get ip address of a computer


#include<stdlib.h>

main()
{
   system("C:\\Windows\\System32\\ipconfig");
   system("pause");

   return 0;
}

selection sort in c


#include<stdio.h>

main()
{
   int array[100], n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

   return 0;
}

Thursday, September 20, 2012

Swapping numbers using call by reference


#include <stdio.h>

void swap(int*, int*);

int main()
{
   int x, y;

   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(&x, &y);

   printf("After Swapping\nx = %d\ny = %d\n", x, y);

   return 0;
}

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b = *a;
   *a = temp;  
}

c program to find frequency of characters in a string


#include<stdio.h>
#include<string.h>

main()
{
   char string[100], ch;
   int c = 0, count[26] = {0};

   printf("Enter a string\n");
   gets(string);

   while ( string[c] != '\0' )
   {
      /* Considering characters from 'a' to 'z' only */

      if ( string[c] >= 'a' && string[c] <= 'z' )
         count[string[c]-'a']++;

      c++;
   }

   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }

   return 0;
}

Wednesday, September 19, 2012

c program to reverse a number

//C Program to reverse a number :- This program reverse the number entered by the user and then prints the reversed number on the screen.

#include <stdio.h>
 main()

{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;

reverse = reverse + n%10; n = n/10;

}

printf("Reverse of entered number is = %d\n", reverse);

return 0;

}

SORTING MATRIX


//Sorting Matrix

#include <stdio.h>
void main ()
{
 int ma[10][10],mb[10][10];
int i,j,k,a,m,n;
printf ("Enter the order of the matrix \n");
scanf ("%d %d", &m,&n);
printf ("Enter co-efficients of the matrix \n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
mb[i][j] = ma[i][j];
}
}
printf ("The given matrix is \n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
printf ("After arranging rows in ascending order\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
for (k=(j+1);k<n;++k)
{
if (ma[i][j] > ma[i][k])
{
a = ma[i][j];
ma[i][j] = ma[i][k];
ma[i][k] = a;
}
}
}
}
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
printf ("After arranging the columns in descending order \n");
for (j=0;j<n;++j)
{
for (i=0;i<m;++i)
{
for (k=i+1;k<m;++k)
{
if (mb[i][j] < mb[k][j])
{
a = mb[i][j];
mb[i][j] = mb[k][j];
mb[k][j] = a;
}
}
}
}
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("\n");
}
}

C programm print Pyramid of Stars


#include<stdio.h>

main()
{
   int row, c, n, temp;

   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);

   temp = n;


   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

   return 0;
}

Tuesday, September 18, 2012

C program to compare two strings without using library function

// c program to compare two strings without using library functions
#include <stdio.h>
int main()

{
char a[50];
char b[50];
int i, j, nr1=0, nr2=0;
printf("enter the two strings you want to compare\n");
scanf("%s %s", a, b);
//counting the length of each string
while(a[i]!=\0)
{
nr1++;
i++;
}
while(b[j]!=\0)
{
nr2++;
j++;
}
if(nr1 != nr2)
{
printf("NO different lengths");
return 0;
}

for(i=0;i<nr1;i++)
{if(a[i]!=b[j])
{ printf("NO");
return0;
}
}
printf("the strings are identic");

}



Checking if a string is palindrome Recursion


#include <stdio.h>
#include <string.h>



int isPalindrome (char str[],int  length)
{

    if (length<=0)
        return 1;
    if (str[0] == str[length-1])
    {

        return isPalindrome (str+1, length-2);/*Recursive call as the function isPalindrome
        is called again within itself*/
    }
    else return 0;
}


int main (void)
{
    int result;
    char str[256];
    printf ("\nPlease type a string: \n");
    gets (str);/*Input a string to check whether it is a palindrome or not*/
int length = strlen (str);
    result = isPalindrome (str, length);/*The function isPalindrome is called.It takes a string
    argument and returns a value 0 or 1 which is stored in
    the integer variable "result"*/
    if (result==1)
        printf ("\n******Input string is a palindrome string.************\n");
    else
        printf ("\n******Not a palindrome******\n");
    return 1;
}

Saturday, September 15, 2012

Recursion find best combination in an array that is equal to a given sum


//Recursion

#include <stdio.h>

int search(int *a, int n, int sum, int sel[], int fillim)
{
    int i;
    if(sum == 0)
    {
        return 1;
    }
    for(i=fillim; i<n; ++i)
    {
        if((sel[i]==0)&&(a[i]<=sum))
        {
            sel[i]=1;
            if(search(a, n, sum-a[i], sel, i+1)==1)
            return 1;
            sel[i]=0;
        }
    }
    return 0;
}
void print(int *a, int n, int sel[])
{
    int i;
    for(i=0;i<n;++i)
    {
        if(sel[i]==1)
        printf("%d \n", a[i]);
    }
    printf("\n");
}

int main()
{
    int a[100], sel[100], n, i , shuma;
    printf("Ju lu tem shkuarni n: ");
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        printf("\n give the element  %d", i);
        scanf("%d", &a[i]);
    }
        printf("pls give the sum");
        scanf("%d", &shuma);

    for(i=0;i<n;i++)
    sel[i]=0;
    if(search(a, n, shuma, sel, 0)==1)
    {
        print(a, n, sel);
    }

}

Thursday, September 13, 2012

Recursion c programm if a number is prime

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

int isprime(int n, int f)
 {
 if(n==f) 
 return 1;
 if(n%f==0 || n==1) 
 return 0; 
 else return isprime(n,f+1);
 }

 int main() 
{
 int x, f=2; 
printf("Enter Number : ");
 scanf("%d", &x);
 printf("%d", isprime(x, f));
 return 0; 
}

Recursion Tower of Hanoi


#include <stdio.h>

void move(int n,int A,int C,int B)
 /* number to move, source pole, destination pole and
spare pole respectively */
{
if (n==1)
{
    printf("Move from %d to %d.\n",A,C);}
else     {

    move(n-1,A,B,C);

    move(1,A,C,B);

    move(n-1,B,C,A);
}
}

main()
{
    int n=3;
move(n,1,3,2);
}

Recursion sorting array using Merge Sort


//Merge Sort...

#include <stdio.h>

void join(int a[], int l, int m, int r)
{
    int i, j, k, v[100];
    i=l, k=0,j=m+1;
    while(i<=m && j<=r)
    {
        if(a[i]>a[j])
        {
            v[k]=a[i];
            i++;
        }
        else
        {
            v[k]=a[j];
            j++;
        }
        k++;
    }
    while(i<=m)
    v[k++]=a[i++];
    while(j<=r)
    v[k++]=a[j++];
    for(i=l;i<=r;i++)
    a[i]=v[i-l];

}


void separate(int a[],int l,int r)
{
    if(l>=r)
    return;

int    m=((l+r)/2);
    separate(a, l, m);
    separate(a, m+1, r);
    join(a, l, m, r);
}


int main()
{
 int a[100]={5, 3, 9, 10, 2};
int i;
    separate(a,0, 4);
for(i=0;i<5;i++)
printf("%d", a[i]);
}

Programm Find second maximum of an array


Find first and second maximum
#include <stdio.h>

void max(int v[], int n, int *max1, int *max2)
{
    int i;
    if(v[1]>v[0])
    {
        *max1=v[1];
        *max2=v[0];
    }
    else
    {
        *max1=v[0];
        *max2=v[1];
    }

    for(i=0;i<n;i++)
    {
        if(v[i]>*max1)
        {

         *max2=*max1;
        *max1=v[i] ;
}
        else if(v[i]>*max2)
        *max2=v[i];
    }
}

int main()
{
    int max1, max2;
    int v[100]={5, 6, 8 , 9, 25};
    max(v,5, &max1, &max2);
    printf("%d  %d", max1, max2);

}

Find all permutations of a given string


# include <stdio.h>

/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

/* Function to print permutations of string
   This function takes three parameters:
   1. String
   2. Starting index of the string
   3. Ending index of the string. */
void permute(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
            printf(" 1%s      ", a);
          permute(a, i+1, n);
          printf(" 2%s   %d   ", a, i);

          swap((a+i), (a+j)); //backtrack
       }
   }
}

/* Driver program to test above functions */
int main()
{
   char a[] = "AAC";
   permute(a, 0, 2);
   getchar();
   return 0;
}

Merging two sorted arrays into a third sorted array


/* Program of merging two sorted arrays into a third sorted array*/
#include<stdio.h>

main()
{
int arr1[20],arr2[20],arr3[40];
int i,j,k;
int max1,max2;

printf("Enter the number of elements in list1 : ");
scanf("%d",&max1);
printf("Take the elements in sorted order :\n");
for(i=0;i<max1;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr1[i]);
}
printf("Enter the number of elements in list2 : ");
scanf("%d",&max2);
printf("Take the elements in sorted order :\n");
for(i=0;i<max2;i++) {
printf("Enter element %d : ",i+1);
scanf("%d",&arr2[i]);
}
/* Merging */
i=0; /*Index for first array*/
j=0; /*Index for second array*/
k=0; /*Index for merged array*/

while( (i < max1) && (j < max2) )
{
if( arr1[i] < arr2[j] )
arr3[k++]=arr1[i++];
else
arr3[k++]=arr2[j++];
}/*End of while*/
/*Put remaining elements of arr1 into arr3*/
while( i < max1 )
arr3[k++]=arr1[i++];
/*Put remaining elements of arr2 into arr3*/
while( j < max2 )
arr3[k++]=arr2[j++];

/*Merging completed*/
printf("List 1 : ");
for(i=0;i<max1;i++)
printf("%d ",arr1[i]);
printf("\nList 2 : ");
for(i=0;i<max2;i++)
printf("%d ",arr2[i]);
printf("\nMerged list : ");
for(i=0;i<max1+max2;i++)
printf("%d ",arr3[i]);
printf("\n");
}