Loading

Archive for 30 Aug 2012

C Program to Delete a File

C - Program to Delete a File


#include<stdio.h>
 
main()
{
   int status;
   char file_name[25];
 
   printf("Enter the name of file you wish to Delete :: ");
   gets(file_name);
 
   status = remove(file_name);
 
   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }
 
   return 0;
}

Posted in | 1 Comment

C - Program to Find Substring

C - Program to Find Substring


#include <stdio.h>
#include <malloc.h>
 
char* substring(char*, int, int);
 
main() 
{
   char string[100], *pointer;
   int position, length;
 
   printf("Enter a string\n");
   gets(string);
 
   printf("Enter the position and length of substring\n");
   scanf("%d%d",&position, &length);
 
   pointer = substring( string, position, length);
 
   printf("Required substring is \"%s\"\n", pointer);
 
   free(pointer);
 
   return 0;
}
 
/*C substring function: It returns a pointer to the substring */
 
char *substring(char *string, int position, int length) 
{
   char *pointer;
   int c;
 
   pointer = malloc(length+1);
 
   if (pointer == NULL)
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
 
   for (c = 0 ; c < position -1 ; c++) 
      string++; 
 
   for (c = 0 ; c < length ; c++)
   {
      *(pointer+c) = *string;      
      string++;   
   }
 
   *(pointer+c) = '\0';
 
   return pointer;
}

Posted in | 1 Comment

C Program for Palindrome

C - Program for Palindrome


#include <stdio.h>
#include <string.h>
 
main()
{
   char text[100];
   int begin, middle, end, length = 0;
 
   gets(text);
 
   while ( text[length] != '\0' )
      length++;
 
   end = length - 1;
   middle = length/2;
 
   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("Not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
      printf("Palindrome.\n");
 
   return 0;
}

Posted in | 1 Comment

C Program to Add two Complex Numbers

C - Program to Add two Complex Numbers


#include <stdio.h>
 
struct complex
{
   int real, img;
};
 
main()
{
   struct complex a, b, c;
 
   printf("Enter a and b where a + ib is the first complex number.\n");
   printf("a = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.\n");
   printf("c = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di\n",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di\n",c.real,c.img);
 
   return 0;
}

Posted in | Leave a comment

C Program to Concatenate Strings

C - Program to Concatenate Strings


#include<stdio.h>
 
void concatenate_string(char*, char*);
 
main()
{
    char original[100], add[100];
 
    printf("Enter source string\n");
    gets(original);
 
    printf("Enter string to concatenate\n");
    gets(add);
 
    concatenate_string(original, add);
 
    printf("String after concatenation is \"%s\"\n", original);
 
    return 0;
}
 
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
 
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}

Posted in | 1 Comment

C Program to Compare Two Strings

C - Program to Compare Two Strings


#include<stdio.h>
 
int compare_string(char[], char[]);
 
main()
{
    char first[100], second[100], result;
 
    printf("Enter first string\n");
    gets(first);
 
    printf("Enter second string\n");
    gets(second);
 
    result = compare_string(first, second);
 
    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");
 
    return 0;
}
 
int compare(char a[], char b[])
{
   int c = 0;
 
   while( a[c] == b[c] )
   {
      if( a[c] == '\0' || b[c] == '\0' )
         break;
      c++;
   }
   if( a[c] == '\0' && b[c] == '\0' )
      return 0;
   else
      return -1;
} 

Posted in | 1 Comment

C Program to Find String Length

C - Program to Find String Length


#include<stdio.h>
 
main()
{
   char array[100], *pointer;
   int length = 0;
 
   printf("Enter a string\n");
   gets(array);
 
   pointer = array;
 
   while(*(pointer+length))
      length++;
 
   printf("Length of entered string = %d\n",length);
 
   return 0;
}

Posted in | 1 Comment

C Program for Matrix multiplication

C - Program for Matrix multiplication


#include <stdio.h>
 
int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
 
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
 
  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);
 
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);
 
  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
 
    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
 
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }
 
        multiply[c][d] = sum;
        sum = 0;
      }
    }
 
    printf("Product of entered matrices:-\n");
 
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);
 
      printf("\n");
    }
  }
 
  return 0;
}

Posted in | 1 Comment

C Program to Transpose a Matrix

C - Program to Transpose a Matrix


#include<stdio.h>
 
main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
 
   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
 
   printf("Transpose of entered matrix :-\n");
 
   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }  
      printf("\n");
   }
 
   return 0;
}

Posted in | 1 Comment

C program to perform addition, subtraction, multiplication and division

C Program to Perform Addition, Subtraction, Multiplication and Division 



#include <stdio.h>
 
int main()
{
   int first, second, add, subtract, multiply;
   float divide;
 
   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);
 
   add      = first + second;
   subtract = first - second;
   multiply = first * second;
   divide   = first / (float)second;   //typecasting
 
   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);
 
   return 0;
}

Posted in , , , | 1 Comment

C/C++ Program for Water Jug Problem in Artificial Intelligence

C/C++ Program for Water Jug Problem in Artificial Intelligence



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

class Queue
{
            public:
                        Queue()
                        : head(NULL), tail(NULL)
                        {
                        }
                        void enqueue(int i)
                        {
                            if (head == NULL)
                                head = tail = new Node(i, NULL);
                            else
                                tail = tail->next = new Node(i, NULL);
                        }
                        int dequeue()
                        {
                                    Node* old = head;
                                    head = head->next;
                                    int i = old->i;
                                    delete old;
                                    return i;
                        }
                        int isEmpty()
                        {
                                    return (head == NULL);
                        }
                        ~Queue()
                        {
                                    while (!isEmpty())
                                    dequeue();
                        }
            private:
                        struct Node
                        {
                                    int i;
                                    Node* next;

                                    Node(int iP, Node* nextP)
                                    : i(iP), next(nextP)
                                    {
                                    }
                        } *head, *tail;
} iQueue;

const int MAX = 100;
const int MAX_I = (MAX + 1) * (MAX + 1);

int N, M, k, n, m;

int distance[MAX_I];
int prev[MAX_I];

int nmToI(int n, int m)
{
            return n * (M + 1) + m;
}

int iToN(int i)
{
            return i / (M + 1);
}

int iToM(int i)
{
            return i % (M + 1);
}

void trace(int i)
{
            if (i > 0)
                        trace(prev[i]);
            cout <<"    "<<iToN(i) << "   |   " <<iToM(i) << "\n";
}

void test(int n, int m, int n1, int m1)
{
            if (n1 < 0 || n1 > N || m1 < 0 || m1 > M)
                        return;

            int i1 = nmToI(n1, m1);

            if (distance[i1] != 0)
                        return;

            int i = nmToI(n, m);
            distance[i1] = distance[i] + 1;
            prev[i1] = i;
            iQueue.enqueue(i1);
}

int solve()
{
            n = m = 0;
            distance[0] = 1;
            iQueue.enqueue(0);

            while (!iQueue.isEmpty())
            {
                        int i = iQueue.dequeue();
                        int n = iToN(i);
                        int m = iToM(i);

                        if (n == k || m == k || n + m == k)
                        return i;

            // empty out a jug
                        test(n, m, 0, m);
                        test(n, m, n, 0);

            // fill a jug
                        test(n, m, N, m);
                        test(n, m, n, M);

            // pour one to another until source is empty
                        test(n, m, 0, n + m);
                        test(n, m, n + m, 0);

            // pour one to another until destination is full
                        test(n, m, n - M + m, M);
                        test(n, m, N, m - N + n);
            }
            return -1;
}

void main()
{
            clrscr();
            cout<<"Please enter the number of gallons in first jug:  ";
            cin>>N;
            cout<<"Please enter the number of gallons in second jug:  ";
            cin>>M;
            cout<<"Please enter the vol. of water to be left finally: ";
            cin>>k;

            int i = solve();

            cout<<"  JUG 1  "<<"  JUG 2 \n";
            cout<<"----------------\n";
            if (i == -1)
                        cout << 0 << "\n";
            else
            {
                        cout << distance[i] << "\n";
                        trace(i);
            }
            cout << -1 << "\n";
            getch();
}

Posted in | 5 Comments