Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Thursday, January 9, 2014

TO IMPLEMENT BINARY SEARCH ALGORITHM IN C

int BinarySearch(int *array, int number_of_elements, int key)
{
        int low = 0, high = number_of_elements-1, mid;
        while(low <= high)
        {
                mid = (low + high)/2;
                if(array[mid] < key)
{
                       low = mid + 1;
               }
               else if(array[mid] == key)
               {
                       return mid;
                }
                else if(array[mid] > key)
                {
high = mid-1;
               }
       }
       return -1;
}
int main()
{
int number_of_elements;
        scanf("%d",&number_of_elements);
        int array[number_of_elements];
        int iter;
        for(iter = 1;iter < number_of_elements;iter++)
        {
if(array[iter] < array[iter - 1])
                {
                        printf("Given input is
not sorted\n");
return 0;
                }
        }
        int key;
        scanf("%d",&key);
        /* Calling this functions searches for the key and returns its index. It returns -1          if key is not found.*/
        int index;
index = BinarySearch(array,number_of_elements,key);
        if(index==-1)
        {
                printf("Element not found\n");
      }
       else
       {
               printf("Element is at index %d\n",index);
        }
return 0;
}

TO IMPLEMENT PASCALS TRIANGLE USING C

#include<stdio.h>
long fact(int);
int main(){
    int line,i,j;
    printf("Enter the no. of lines: ");
    scanf("%d",&line);
    for(i=0;i<line;i++){
         for(j=0;j<line-i-1;j++)
             printf(" ");
for(j=0;j<=i;j++)
             printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
         printf("\n");
}
    return 0;
}
long fact(int num){
    long f=1;
    int i=1;
    while(i<=num){
f=f*i;
i++;
  }
  return f;
}

Output:
Enter the no. of lines:3
1
1 1
1 2 1

Wednesday, December 4, 2013

TO FIND FACTORIAL OF A NUMBER IN C

//TO FIND FACTORIAL OF A NUMBER IN C
# include<stdio.h>
int main()
{
int fac,n;
int factorial(int);
printf("Enter any number:");
scanf("%d",&n);
fac=factorial(n);
printf("Factorial=%d",fac);
return 0;
}

int factorial(int x)
{
int f;
if(x==1)
return 1;
else
f=x*factorial(x-1);
return f;
}

Wednesday, October 30, 2013

To implement SEMAPHORE in C

 /*
To implement SEMAPHORE in C

*/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>

int main()
{
        int semid,pid,val,i,withdraw;
        struct sembuf sop;
        FILE *fp;
        int balance;
        semid=semget(0x20,1,IPC_CREAT|0666);
        semctl(semid,0,SETVAL,1);
        sop.sem_num=0;
        sop.sem_op=-1;
        sop.sem_flg=SEM_UNDO;
        pid=fork();
        if(pid==0)
        {
                printf("Child Before Sem Operation\n");
                semop(semid,&sop,1);
                printf("Child is in Critical Section\n");
                fp = fopen("data.txt","r+");
                fscanf(fp,"%d",&balance);
                printf("Enter the Amount to be Withdrawn: ");
                scanf(" %d",&withdraw);
                rewind(fp);
                balance = balance - withdraw;
                fprintf(fp,"%d",balance);
                fclose(fp);
                sleep(2);
                printf("\nBalance = %d\n",balance);
                printf("Child Outside Critical Section\n");
        }
        else
        {
                printf("Parent Before Sem Operation\n");
                semop(semid,&sop,1);
                printf("Parent is in Critical Section\n");
                sleep(2);
                fp = fopen("data.txt","r+");
                fscanf(fp,"%d",&balance);
                printf("\nBalance = %d\n",balance);
                printf("Enter the Amount to be Withdrawn: ");
                scanf(" %d",&withdraw);
                rewind(fp);
                balance = balance - withdraw;
                fprintf(fp,"%d",balance);
                fclose(fp);
                sleep(2);
                printf("\nBalance = %d\n",balance);
                printf("Parent Outside Critical Section\n");
        }
        return 0;
}

/* Output:
Child Before Sem Operation
Child is in Critical Section
Parent Before Sem Operation
Enter the Amount to be Withdrawn: 10

Balance = 850
Child Outside Critical Section
Parent is in Critical Section

Balance = 850
Enter the Amount to be Withdrawn: 50

Balance = 800
Parent Outside Critical Section
*/

To Implement Inter Process Communication using Pipes

/*
To Implement Inter Process Communication using Pipes
*/

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

int main()
{
        int fd[2],n,status;
        char buff[100];
        pipe(fd);
        if(pipe (fd)<0)
        {
                printf("Cannot create a pipe.....");
                exit(0);
        }

        else
        {
                switch(fork())
                {
                        case -1:
                                printf("\nFork error...");
                                exit(0);
 
                        case 0:
                                //child process read data from buffer

                                printf("\nRunning child process...");
                                printf("\nBefore read is performed...");
                                read(fd[0],buff,100);
                                printf("\nThe data read from the buffer is:\t %s",buff);
                                break;

                        default:
                                //parent process writes to buffer

                                printf("\nRunning parent process...\n");
                                printf("\nWriting...\n");
                                write(fd[1],"Welcome to HOME....",60);
                                wait(&status);
                                printf("\nWriting is over...\n");
                                break;
                }
        }
    return(0);
}



/*
Output:
Running child process...

Running parent process...

Writing...
Before read is performed...
The data read from the buffer is:        Welcome to HOME....
Writing is over...
*/

IMPLEMENTATATION OF INODE USING C.

/*
TITLE:IMPLEMENTATATION OF INODE USING C.

*/

#include<sys/types.h>
#include<ustat.h>
#include<stdio.h>
#include<sys/dir.h>

main(argc,argv)
int argc;
char *argv[];
{
  FILE *fp;
  struct direct dir;
  fp=fopen(argv[1],"r");
  printf("\n Directory listing of %s\n",argv[1]);
  while(fread(&dir,sizeof(dir),1,fp)!=EOF)
  {
     if(dir.d_ino==0)
         continue;
          printf("inode number:%d of file:%s\n",dir.d_ino,dir.d_name);
  }
   fclose(fp);
   exit(0);
}

To Study the File-Handling

/*
To Study the File-Handling
*/


#include<stdio.h>
#include<fcntl.h>
#include<error.h>
#include<string.h>
#include<sys/wait.h>

int fd;
char name1[100];
void createfile(char *name)
{
printf("\n Enter name of file \n");
scanf("%s",name);
fd=open(name,O_CREAT|O_EXCL,0760);
if(fd==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}

else
{
printf("\nfile created");
close(fd);
}
}


void copy()
{
char ch;
int n;
char name1[100], name2[100];
int fd1, fd2;
printf("\n Enter name of file to copy");
scanf("%s",name1);
fd1=open(name1,O_RDWR|O_APPEND,0760);
printf("\nEnter name of file to be copied into");
scanf("%s",name2);
fd2=open(name2,O_CREAT|O_EXCL|O_RDWR|O_APPEND,0761);
if(fd1==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}
if(fd2==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}


while(read(fd1,&ch,1)>0);
{
write(fd2,&ch,1);
}


printf("\n copied");
close(fd1);
close(fd2);
}
void writeandread()
{
char ch;
int n;
char name2[100], buff[100], buff1[100];
int fd1;
printf("\n Enter name of file to write");
scanf("%s",name1);
fd1=open(name1,O_RDWR|O_APPEND,0760);
/*printf("\nEnter name of file");
scanf("%s",name2);
fd2=open(name2,O_CREAT|O_EXCL|O_RDWR|O_APPEND,0761);
*/
if(fd1==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}

else
{
printf("\nEnter to write");
scanf("%s",buff);
write(fd1,buff,strlen(buff));
printf("\nwrite succesfull");
lseek(fd1,0,SEEK_SET);
read(fd1,buff1,strlen(buff));
printf("\nfile contains: %s\n",buff1);
}
printf("\nexiting");
close(fd1);

}

int main()

{
char name[100];
createfile(name);
        writeandread();
copy();
return 0;
}

/*
OUTPUT:
bash-3.00$ cc file.c
bash-3.00$ ./a.out

Enter name of file a1

file created
 Enter name of file to write a1

Enter to write hi

write succesfull
file contains: hij

exiting
 Enter name of file to copy a1

Enter name of file to be copied into a2

 copiedbash-3.00$ ./a.out
*/

IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.

/*

TITLE: IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.
*/




#include<stdio.h>
#include<sys/wait.h>

int main()
{
      int pid,status,n1,n2,sum;
      printf("\n parent(user) program is in execution");
      pid=fork();
      printf("\n spanning the child process");
      if(pid==0)
      {
           printf("\n Child is executing :\n");
           printf("\n Enter two no.:\n");
           scanf("%d  %d",&n1,&n2);
           sum=n1+n2;
           printf("the sum is: %d\n",sum);
           printf(" pid of child is: %d\n",getpid());
           printf("pid of parent is: %d\n",getppid());
           printf("\n exiting child \n");

      }
    else
     {
       wait(&status);
              printf("\n parent is executing: \n");
           printf("\n pid of parent is :%d\n",getpid());
           printf("\n pid of parent's parent is: %d\n ",getppid());

         
           printf("\n exiting parent \n");
     }
   return 0;
}
/*
 parent(user) program is in execution
 spanning the child process
 Child is executing :

 Enter two no.:
 parent(user) program is in execution
20
14
the sum is: 34
 pid of child is: 4874
pid of parent is: 4873

 exiting child
 spanning the child process
 parent is executing:

 pid of parent is :4873

 pid of parent's parent is: 4857

 exiting parent
*/