Wednesday 27 August 2014

Read a text file containing several occurrences of student and replace the word student with graduate.

Read a text file containing several occurrences of student and replace the word student with graduate.

#include<stdio.h>
#include<string.h>
int main(){
    FILE *fp;
    char ch;
    char str[1000];
    int i=0;
    FILE *fp1;
    fp = fopen("test.txt","r");
    fp1= fopen("test1.txt","w+");
    ch=fgetc(fp);
    while(ch!=EOF){   
        if(ch == '\n'){           
            i++;           
            str[i] = '\0';
            i = 0;
            char *a = NULL;
            char b[9]="graduate";
            a = strtok(str," \n");
            while(a!=NULL){
                if(strncmp(a,"student",8) == 0){
                    fprintf(fp1,"%s ",b);       
                }
                else{
                    fprintf(fp1,"%s ",a);   
                }
            a = strtok(NULL," \n");
            }
            fprintf(fp1,"%c",'\n');
        }
        else{
            str[i] = ch;
            i++;
        }           
    ch =fgetc(fp);
    }
    fclose(fp);
    fclose(fp1);
    return 0;
}

Sunday 17 August 2014

Strassen Matrix Multiplication program in c

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define MAX_SIZE 32
void add(int **a, int **b, int size,int **c);
void sub(int **a, int **b, int size,int **c);
void  multiply(int **c,int **d,int size,int size2,int **new){
    if(size == 1){   
        new[0][0] = c[0][0] *d[0][0];   
    }
    else {
        int i,j;
        int nsize =size/2;
        int **c11 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            c11[i]= malloc(nsize*sizeof(int));
        }
        int **c12 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            c12[i]= malloc(nsize * sizeof(int));
        }
        int **c21 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            c21[i]= malloc(nsize * sizeof(int));
        }
        int **c22 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            c22[i]= malloc(nsize*sizeof(int));
        }
        int **d11 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            d11[i]= malloc(nsize*sizeof(int));
        }
        int **d12 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            d12[i]= malloc(nsize*sizeof(int));
        }
        int **d21 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            d21[i]= malloc(nsize*sizeof(int));
        }
        int **d22 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            d22[i]= malloc(nsize*sizeof(int));
        }
        int **m1 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m1[i]= malloc(nsize*sizeof(int));
        }
        int **m2 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m2[i]= malloc(nsize*sizeof(int));
        }
        int **m3 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m3[i]= malloc(nsize*sizeof(int));
        }
        int **m4 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m4[i]= malloc(nsize*sizeof(int));
        }
        int **m5 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m5[i]= malloc(nsize*sizeof(int));
        }
        int **m6 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m6[i]= malloc(nsize*sizeof(int));
        }
        int **m7 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            m7[i]= malloc(nsize * sizeof(int));
        }
        for(i=0;i<nsize;i++){
            for(j=0;j<nsize;j++){
                c11[i][j]=c[i][j];
                c12[i][j]=c[i][j+nsize];
                c21[i][j]=c[i+nsize][j];
                c22[i][j]=c[i+nsize][j+nsize];                   
                d11[i][j]=d[i][j];
                d12[i][j]=d[i][j+nsize];
                d21[i][j]=d[i+nsize][j];
                d22[i][j]=d[i+nsize][j+nsize];
            }
        }
        int **temp1 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp1[i]= malloc(nsize*sizeof(int));
        }
        int **temp2 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp2[i]= malloc(nsize*sizeof(int));
        }
       
        add(c11,c22,nsize,temp1);
        add(d11,d22,nsize,temp2);
        multiply(temp1,temp2,nsize,size,m1);
        free(temp1);
        free(temp2);
       
        int **temp3 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp3[i]= malloc(nsize*sizeof(int));
        }       
        add(c21,c22,nsize,temp3);
        multiply(temp3,d11,nsize,size,m2);
        free(temp3);


        int **temp4 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp4[i]= malloc(nsize*sizeof(int));
        }
        sub(d12,d22,nsize,temp4);
        multiply(c11,temp4,nsize,size,m3);
        free(temp4);


        int **temp5 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp5[i]= malloc(nsize*sizeof(int));
        }
        sub(d21,d11,nsize,temp5);
        multiply(c22,temp5,nsize,size,m4);
        free(temp5);


        int **temp6 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp6[i]= malloc(nsize*sizeof(int));
        }
        add(c11,c12,nsize,temp6);
        multiply(temp6,d22,nsize,size,m5);
        free(temp6);

        int **temp7 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp7[i]= malloc(nsize*sizeof(int));
        }   
        int **temp8 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp8[i]= malloc(nsize*sizeof(int));
        }
        sub(c21,c11,nsize,temp7);
        add(d11,d12,nsize,temp8);
        multiply(temp7,temp8,nsize,size,m6);
        free(temp7);   
        free(temp8);

        int **temp9 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp9[i]= malloc(nsize*sizeof(int));
        }   
        int **temp10 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            temp10[i]= malloc(nsize*sizeof(int));
        }       
        sub(c12,c22,nsize,temp9);
        add(d21,d22,nsize,temp10);
        multiply(temp9,temp10,nsize,size,m7);
        free(temp9);
        free(temp10);   
       

        int **te1 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te1[i]= malloc(nsize*sizeof(int));
        }
        int **te2 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te2[i]= malloc(nsize*sizeof(int));
        }
        int **te3 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te3[i]= malloc(nsize*sizeof(int));
        }
        int **te4 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te4[i]= malloc(nsize*sizeof(int));
        }
        int **te5 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te5[i]= malloc(nsize*sizeof(int));
        }
        int **te6 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te6[i]= malloc(nsize*sizeof(int));
        }
        int **te7 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te7[i]= malloc(nsize*sizeof(int));
        }
        int **te8 = malloc(nsize * sizeof(int *));
        for(i=0;i<nsize;i++){
            te8[i]= malloc(nsize*sizeof(int));
        }

        add(m1,m7,nsize,te1);
        sub(m4,m5,nsize,te2);
        add(te1,te2,nsize,te3);    //c11
           
        add(m3,m5,nsize,te4);//c12   
        add(m2,m4,nsize,te5);//c21
       
        add(m3,m6,nsize,te6);
        sub(m1,m2,nsize,te7);
       
        add(te6,te7,nsize,te8);//c22
       
        int a=0;
        int b=0;
        int c=0;   
        int d=0;
        int e=0;
        int nsize2= 2*nsize;
        for(i=0;i<nsize2;i++){
            for(j=0;j<nsize2;j++){
                if(j>=0 && j<nsize && i>=0 && i<nsize){
                    new[i][j] = te3[i][j];
                }
                if(j>=nsize && j<nsize2 && i>=0 && i<nsize){
                    a=j-nsize;
                    new[i][j] = te4[i][a];
                }
                if(j>=0 && j<nsize && i>= nsize && i < nsize2){
                    c=i-nsize;
                    new[i][j] = te5[c][j];
                }
                if(j>=nsize && j< nsize2 && i>= nsize && i< nsize2 ){
                    d=i-nsize;
                    e=j-nsize;
                    new[i][j] =te8[d][e];
                }
            }   
        }
    free(m1);
    free(m2);   
    free(m3);   
    free(m4);   
    free(m5);   
    free(m6);   
    free(m7);   
    free(te1);   
    free(te2);   
    free(te3);   
    free(te4);   
    free(te5);   
    free(te6);   
    free(te7);   
    free(te8);   
    free(c11);   
    free(c12);   
    free(c21);   
    free(c22);   
    free(d11);   
    free(d12);   
    free(d21);   
    free(d22);   
    }   
}

void main(){
    int size,p,itr,itr1,i,j,nsize;
    printf("Enter Size of matrix\n");   
    scanf("%d",&size);
    int tempS = size;
    if(size & size-1 != 0){
        p = log(size)/log(2);
        size = pow(2,p+1);
    }
    int **a = malloc(size * sizeof(int *));
    for(i=0;i<size;i++){
        a[i] = malloc(size*sizeof(int));
    }   
    int **b = malloc(size * sizeof(int *));
    for(i=0;i<size;i++){
        b[i] = malloc(size*sizeof(int));
    }
    printf("Enter elements of 1st matrix\n");
    for(itr=0;itr<size;itr++){
    for(itr1=0;itr1<size;itr1++){
        if(itr>=tempS || itr1>=tempS )
            a[itr][itr1]=0;
        else
            scanf("%d",&a[itr][itr1]);
        }
    }
    printf("Enter elements of 2nd matrix\n");
    for(itr=0;itr<size;itr++){
    for(itr1=0;itr1<size;itr1++){
        if(itr>=tempS || itr1>=tempS)
            a[itr][itr1]=0;
        else
            scanf("%d",&b[itr][itr1]);
        }
    }
    int **new = malloc(size * sizeof(int *));
    for(i=0;i<size;i++){
        new[i] = malloc(size*sizeof(int));
    }   
    multiply(a,b,size,size,new);
   
    if(tempS<size)
        size =tempS;
    for(i=0;i<size;i++){
        for(j=0;j<size;j++){
            printf("%d   ",new[i][j]);
        }
        printf("\n");
    }
}

void add(int **a, int **b, int size,int **c){
    int i,j;       
    for(i=0;i<size;i++){
        for(j=0;j<size;j++){
            c[i][j] = a[i][j] + b[i][j];   
        }
    }
}

void sub(int **a,int **b,int size,int **c){
    int i,j;
    for(i=0;i<size;i++){
                for(j=0;j<size;j++){
                        c[i][j]= a[i][j] - b[i][j];
                }
        }
}

Friday 15 August 2014

.(Duplicate Elimination) Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read.

#include<stdio.h>
void main(){
        int a[20];
        int num,i,j;
        int f=0;
        int k=0;
        printf("enter 20 numbers\n");
        for(i=0;i<20;i++){
                scanf("%d",&num);
                if(num>=10 && num <=100){
                        if(k==0){
                                a[0]=num;
                                k++;
                        }
                        else{
                                for(j=0;j<k;j++){
                                        if(a[j] == num){
                                                f=1;
                                                break;
                                        }
                                }
                                if(f==0){
                                        a[k]=num;
                                        k++;
                                }
                                f=0;
                        }
                }
        }
        printf("the array is: \n");
        for(i=0;i<k;i++){
                printf("%d\n",a[i]);
        }
}

Sunday 10 August 2014

Claculate Value of Pi up to 2,3,4,5 decimal places using c program

Calculate Value of Pi up to 2,3,4,5 decimal places using c program

#include<stdio.h>
#include<math.h>
void main()
{
    float i= 4.0;
    int itr=1;
    int j=3;
    while(fabs(i-3.14000f)>0.00001f)
    {
        i=i + pow(-1,itr)*(4.0/j);
            //printf("%f\n",i);
        itr++;
        j=j+2;   
    }
    printf("Irt for 3.14 =%d\n",itr);
    itr=1,j=3,i = 4.0;
    while(fabs(i-3.14100f)>0.00001f)
        {
                i=i + pow(-1,itr)*(4.0/j);
                //printf("%f\n",i);
                itr++;
                j=j+2;
        }
    printf("Itr for 3.141=%d\n",itr);
    itr=1,j=3,i=4.0;
    while(fabs(i-3.14150f)>0.00001f)
        {
                i=i + pow(-1,itr)*(4.0/j);
                //printf("%f\n",i);
                itr++;
                j=j+2;
        }
    printf("itr for 3.1415=%d\n",itr);
    itr=1,j=3,i=4.0;
    while(fabs(i-3.141590f)>0.00001f)
        {
                i=i + pow(-1,itr)*(4.0/j);
                //printf("%f\n",i);
                itr++;
                j=j+2;
        }
    printf("itr for 3.14159=%d\n",itr);

}

Palindrome using a stack and a que using linked list in C program.

Palindrome using a stack and a que using linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int val;
    struct node *next;
}*top1,*last,*first;

void push(struct node **top,int val)
{
    struct node *n1 =(struct node *)malloc(sizeof(struct node));   
    n1->val= val;
    n1->next = NULL;   
    if(*top == NULL)
    {
        *top = n1;
       
    }
    else
    {
        n1->next = *top;
        *top = n1;
    }   
}
int pop(struct node **top)
{
    int a;   
    struct node *save = *top;   
    *top = (*top)->next;   
    a = save->val;   
    free(save);
    return a;
       
}

void insert(struct node **last,struct node **first, int val)
{
    struct node *n1 =(struct node*)malloc(sizeof(struct node));
    n1->val = val;
    n1->next = NULL;
    if(*last == NULL)
    {
        *last = n1;
        *first = n1;
    }
    else
    {       
        (*last)->next=n1;
        *last = (*last)->next;
    }   
}
int delete(struct node **first)
{
    int a;   
    struct node *save = *first;   
    *first = (*first)->next;   
    a = save->val;   
    free(save);
    return a;
}

void main()
{
    int num;
    int flag = 0;
    int digit,val,val1,val2;
    printf("Enter number");
    scanf("%d",&num);
    while(num != 0)
    {
        digit = num%10;
        num =num/10;
        push(&top1,digit);
        insert(&last,&first,digit);
    }
    while(top1 != NULL)
    {
        val1= pop(&top1);
        printf("val 1 is %d\n",val1);       
        val2= delete(&first);
        printf("val 2 is %d\n",val2);
             if(val1!=val2)
        flag = 1;               
    }   
    if(flag==1)
    printf("not palidrom\n");   
    else
    printf("palidrom\n");   
       
}

Palindrome Using 3 stacks(Stacks implemented using Linked List) in c program

Palindrome Using 3 stacks(Stacks implemented using Linked List)

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int val;
    struct node *next;
}*top1,*top2,*top3;

void push(struct node **top,int val)
{
    struct node *n1 =(struct node *)malloc(sizeof(struct node));   
    n1->val= val;   
    if(*top == NULL)
    {
        *top = n1;
        n1->next = NULL;
    }
    else
    {
        n1->next = *top;
        *top = n1;
    }   
}
int pop(struct node **top)
{
    int a;   
    struct node *save = *top;   
    *top = (*top)->next;   
    a = save->val;   
    free(save);
    return a;
       
}

void main()
{
    int num;
    int flag = 0;
    int digit,val,val1,val2;
    printf("Enter number");
    scanf("%d",&num);
    while(num != 0)
    {
        digit = num%10;
        num =num/10;
        push(&top1,digit);
        push(&top2,digit);   
    }
    while(top2 != NULL)
    {
         val = pop(&top2);
        push(&top3,val);
    }
    while(top3 != NULL)
    {
        val1= pop(&top3);
        printf("val 1 is %d\n",val1);       
        val2=pop(&top1);
        printf("val 2 is %d\n",val2);
             if(val1!=val2)
        flag = 1;               
    }   
    if(flag==1)
    printf("not palidrom\n");   
    else
    printf("palidrom\n");   
       
}

Towers Of Hanoi Using Link List as stacks(Binary Logic implementation) in c program

Towers Of Hanoi Using Link List as stacks(Binary Logic implementation)

#include<stdio.h>
#include <stdlib.h>

typedef struct node
{
int value1;
struct node *nxt;
} node ;

typedef struct stack
{
node *top;
}str;


void push(str *,int);
int pop(str *);
void display(str *);

str initStack() {
    str st ;
    st.top = NULL ;
    return st ;
}

void main()
{
    int i,j,k,a,b;
    int value=0;
    str top1 = initStack() ;
    str top2 = initStack() ;
    str top3 = initStack() ;
    printf("Enter number of disks:");
    scanf("%d",&i);

    for(j=i;j>=1;j++)
    {
        push(&top1,j);
    }

    display(&top1);
    display(&top2);
    display(&top3);
   
    for(k=1;k < 1<<i ;k++)
    {
    a=(k&k-1)%3;
    b=((k|k-1)+1)%3;
   
    switch(a)
    {
        case 0: value=pop(&top1);
        break;
        case 1: value=pop(&top2);
        break;
        case 2: value=pop(&top3);
        break;
    }

    switch(b)
    {
        case 0: push(&top1,value);
        break;
        case 1: push(&top2,value);
        break;
        case 2: push(&top3,value);
        break;
    }
    display(&top1);
    display(&top2);
    display(&top3);
    printf("\n") ;
    }
}

void push(str *s,int t)
{
    node *n1 = (node*)malloc(sizeof(node)) ;
    n1->value1 = t;
    n1->nxt = s->top ;
    s->top = n1 ;   
}

int pop(str *s)
{
    node *n2 = s->top ;   
    s->top = n2->nxt ;
    char ch = n2->value1 ;
    free(n2) ;
    return ch;
}    

void display(str *st) {
    node *s = st->top ;
    while(s!=NULL)
    {
    printf("%d ",s->value1);
    s = s->nxt ;
    }
    printf("\n") ;
}

Quick sort Simplest Programming Solution in c program

Quick sort Simplest Programming Solution.


#include<stdio.h>
#include<math.h>
void quicksort(int *ar,int start,int end);      
int  divide(int *ar,int start,int end,int pivot);

void main(){
        int size;
        int itr;
        printf("Enter size of array\n");
        scanf("%d",&size);
        int a[size];
        for(itr=0;itr<size;itr++){
        printf("enter %d element\n",itr+1);
    scanf("%d",&a[itr]);
        }
        quicksort(a,0,size-1);
    for(itr=0;itr<size;itr++)
        printf(" element %d \n",a[itr]);

}

void quicksort(int *ar,int start,int end){
        if(start < end) {
        int pivot = start;
            pivot = rand() % (end - start) + start ;
            pivot = divide(ar,start,end,pivot);
            quicksort(ar,start,pivot-1);
            quicksort (ar,pivot+1,end);
    }
}      
int  divide(int *ar,int start,int end,int pivot){
        int itr,temp,next,next1;
        temp =ar[pivot];
        ar[pivot]=ar[start]; 
        ar[start]=temp;
        pivot =start;
        next =start+1;
        while(next<=end){
                if(ar[next]>ar[pivot]){
                        temp = ar[next];
            next1 = next;
                        while(next1!=pivot+1){
                               ar[next1]=ar[next1-1];
                                next1--;
                        }
                        ar[next1]=ar[pivot];
                        ar[pivot]=temp;
                        pivot++;
                }
            next++;
    }
return pivot;   
}