Powered By Blogger

Saturday 30 April 2011

Can u guess the output... U jst need a simple concept of bitwise and logical operators in C

#include<stdio.h>
int main(void)
{
    int a[5],i=0,s=0;
    printf("Enter 5 digits (choose digits between 0-9)::\n");
    for(i=0;i<5;i++)
        scanf("%d",&a[i]);


    printf("\n\n");
    for(i=0;i<4;i++)
        printf("%5d|%d=%d",a[i+1],a[i],a[i+1]|a[i]);



    printf("\n\n");
    for(i=0;i<4;i++)
        printf("%5d||%d=%d",a[i+1],a[i],a[i+1]||a[i]);
  
  

Thursday 28 April 2011

Guess why the output is different from the initialized value. Its due to a simple change done while initialization nd with a definite logic.

#include<stdio.h>

#include<conio.h>
int main(void)
{
  int i=012;
  int j=046;
  int k=056;
  printf("i=%d\n",i);
  printf("j=%d\n",j);
  printf("k=%d\n",k);

  getch();

  return 0;
}


A tricky C program to print the string "Hi i'm Subhabrata Chakraborty" without using any semicolon(;) in the function.

#include<stdio.h>
#include<conio.h>
void main(void)
{
  if(printf("Hi I'm Subhabrata Chakraborty"))
}

A tricky C program where a negetive no is shown as greater than positive one. But its not by mistake, there is a definite computing logic.

#include<stdio.h>
#include<conio.h>
int main(void)
{
  unsigned int y = 12;
  int x = -2;
 
   if(x>y)
     printf   (" x is greater");
  else
     printf (" y is greater");
   getch();
 
return 0;
}

Create a 2D array dynamically in called function then editing its value and then it's passed to main() using double pointer. Finally updated matrix is printed from main().

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int ** passed(int,int);
int main(void)
{
    int **vam,i=0,j=0,order[2];
    printf("\n**************** Addition to matrix by passing pointer ****************\n\n");
   
    printf("Input the order of the matrix (m n) ::\n");
    for(i=0;i<2;i++)
        scanf(" %d",&order[i]);
    //Function called to get the input and to edit
    vam=passed(order[0],order[1]);
    //Matrix edited and is stored into vam
    printf("\n\nMatrix after ::\n\n");

Create a 2D array dynamically then passing it to another function using Call by Reference and then updating the matrix by using pointers and without copying it to any other dummy matrix and without using generel matrix accesing logic. Then its printed from the Main() function.

#include<stdio.h>
#include<conio.h>
int passed(int **,int,int);
int main(void)
{
    int **matrix,order[2],i=0,j=0;
    printf("\n**************** Addition to 2D matrix by pointer arithmetic  ****************\n\n");
    printf("Input the order of the matrix (m n) ::\n");
    for(i=0;i<2;i++)
        scanf(" %d",&order[i]);
    matrix=(int**)malloc((order[0]+2)*sizeof(int*));
    for(i=0;i<order[0];i++)
        matrix[i]=(int*)malloc((order[1]+2)*sizeof(int));
    printf("\n\nEnter the elements of the matrix (Row Wise)::\n");
    for(i=0;i<order[0];i++)
    {
        for(j=0;j<order[1];j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }

Wednesday 27 April 2011

Different operations like Insertion,Deletion,Display,Linear Search,Binary Search,Bubble Sort,Selection Sort, Insertion Sort, Swapping performed on a user defined length restricted linear data structure(ARRAY) in C


#include<stdio.h>
void insert(int *);
void del(int *);
void display(int *);
void lsearch(int *);
void bsearch(int *);
void bsort(int *);
void ssort(int *);
void isort(int *);
void Swap(int *);
void insrt(int *,int,int);
int fmin(int *,int,int);
// GLOBAL VARIABLES USED TO KEEP TRACK OF THE ARRAY
int count=0,length=0,empty=0;

//Start of Main();
int main(void)
{
int *array;
int choice,i=0;
int elem=3;


Saddle point of a matrix in C


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=0,j=0,k=0,r=0,c=0,max=0,min=0,col=0,row=0;
int **a;
printf("\nPlease enter the order of the first matrix::\n");
scanf("%d",&r);
printf("X\n");
scanf("%d",&c);
a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
{
a[i]=(int*)malloc(c*sizeof(int));
}
printf("\nEnter the element of the matrix::\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}

Matrix Minima method in C


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=0,j=0,k=0,r=0,c=0,max=0,min=0,col=0,row=0;
int **a,flag=0,supply=0,demand=0,sum=0,temp=0,count=0,mr=0,mc=0;
printf("\n************* MATRIX MINIMA RULE *************\n");
printf("\nPlease enter the order of the first matrix:\n");
scanf("%d",&mr);
printf("X\n");
scanf("%d",&mc);
r=mr+1;
c=mc+1;
a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
{
a[i]=(int*)malloc(c*sizeof(int));
}
printf("\nEnter the element of the matrix:\n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
scanf("%d",&a[i][j]);
}
}

North West Corner Rule in C


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=0,j=0,k=0,r=0,c=0,max=0,min=0,col=0,row=0;
int **a,flag=0,supply=0,demand=0,sum=0,temp=0,count=0,mr=0,mc=0;
printf("\n************* NORTH WEST CORNER RULE *************\n");
printf("\nPlease enter the order of the first matrix:\n");
scanf("%d",&mr);
printf("X\n");
scanf("%d",&mc);
r=mr+1;
c=mc+1;
a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
{
a[i]=(int*)malloc(c*sizeof(int));
}
printf("\nEnter the element of the matrix:\n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
scanf("%d",&a[i][j]);
}
}

Implementation of Jacobi rule using C


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
int dominat(float**);
float compute(float**,float,float,int);
int main(void)
{
float **arr,*x,*y,*z,e=0.001;
int i=0,j=0,dom,c=0;

//Dynamic allocation
x=(float*)malloc(1000*sizeof(float));
y=(float*)malloc(1000*sizeof(float));
z=(float*)malloc(1000*sizeof(float));
arr=(float**)malloc(3*sizeof(float*));
for(i=0;i<3;i++)
arr[i]=(float*)malloc(4*sizeof(float));
//Allocation complete


Gauss Seidel method using C


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
int dominat(float**);
float compute(float**,float,float,int);
int main(void)
{
float **arr,*x,*y,*z,e=0.001;
int i=0,j=0,dom,c=0;

//Dynamic allocation
x=(float*)malloc(1000*sizeof(float));
y=(float*)malloc(1000*sizeof(float));
z=(float*)malloc(1000*sizeof(float));
arr=(float**)malloc(3*sizeof(float*));
for(i=0;i<3;i++)
arr[i]=(float*)malloc(4*sizeof(float));
//Allocation complete


Addition of a digit to the elements of an array using pointer arithmetic, while the user defined array is passed to the called function using pointer.


#include<stdio.h>
void pass(int *);
int main(void)
{
int *array,i=0,*a;
int b=10;
array=(int*)calloc(sizeof(int)*b);
printf("Enter the %d elements of the array :: ",b);
for(i=0;i<10;i++)
{
scanf(" %d",&array[i]);
}
a=array;
pass(a);
for(i=0;i<10;i++)
{
printf("%d ",array[i]);
}
return 0;
}

A simple program to pass a string to another function using pointer and to print it from called function.


#include<stdio.h>
void pass(char *);

int main(void)
{
char *string="Subhabrata";
pass(string);
return 0;
}

void pass(char *pass)
{
int i=0;
char *t=pass;
t=pass;
for(i=0;i<10;i++)
{
printf("%c",*t++);
}
}

This C programme will split one Source txt file into two equally sized text file

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int enter=0,lines=0,count=0,flag=0;
int i=0,condition=0;
char temp='\n';
FILE *fp1;
FILE *fp2;
FILE *fp3;
fp1=fopen("exsisting.txt","a+");
fp2=fopen("splitted1.txt","w+");
fp3=fopen("splitted2.txt","w+");

i=0;