diff --git a/Appendix A.pdf b/Appendix A.pdf new file mode 100644 index 0000000..f3ee638 Binary files /dev/null and b/Appendix A.pdf differ diff --git a/Appendix B.pdf b/Appendix B.pdf new file mode 100644 index 0000000..08a0f78 Binary files /dev/null and b/Appendix B.pdf differ diff --git a/Appendix C.pdf b/Appendix C.pdf new file mode 100644 index 0000000..1a3c64f Binary files /dev/null and b/Appendix C.pdf differ diff --git a/Appendix D.pdf b/Appendix D.pdf new file mode 100644 index 0000000..d07e657 Binary files /dev/null and b/Appendix D.pdf differ diff --git a/Appendix E.pdf b/Appendix E.pdf new file mode 100644 index 0000000..2ee2e09 Binary files /dev/null and b/Appendix E.pdf differ diff --git a/AppendixB/mergesort.c b/AppendixB/mergesort.c new file mode 100644 index 0000000..f5cc86a --- /dev/null +++ b/AppendixB/mergesort.c @@ -0,0 +1,84 @@ +#include +#define max 20 + +void merge(int arr[], int fsi, int fei, int ssi, int sei); +void mergesort(int arr[],int si, int ei); + +int main() +{ + int arr[max]; + int si,i,len; + si=0; + printf("How many numerical to sort? "); + scanf("%d",&len); + printf("Enter %d numericals:\n", len); + for(i=0;i<=len-1;i++) + scanf("%d",&arr[i]); + printf("\nOriginal array is \n"); + for(i=0;isi) + { + mid=(si+ei)/2; + mergesort(arr,si,mid); + mergesort(arr,mid+1,ei); + merge(arr,si,mid,mid+1,ei); + } +} + +void merge(int arr[], int fsi, int fei, int ssi, int sei) +{ + int mgd[max],x,fh,sh,i; + fh=fsi; + sh=ssi; + x=0; + while(fh<=fei && sh <=sei) + { + if(arr[fh] +#include +#define max 20 +int main() +{ + int arr[max],k,e,len,i,j,digit,x,numbdig; + int temp[10][max]; + printf("How many numbers to sort ? "); + scanf("%d",&len); + printf("How many number of digits in each numerical ? "); + scanf("%d",&numbdig); + printf("Enter %d values\n", len); + for(i=0;i +#define max 20 + +int main() +{ + int arr[max],i,j,value,len,index,temp; + printf("How many numbers to sort? "); + scanf("%d",&len); + printf("Enter %d values:\n",len); + for(i=0;iarr[j]) + { + value=arr[j]; + index=j; + } + } + temp=arr[i]; + arr[i]=arr[index]; + arr[index]=temp; + } + printf("\nThe ascending order of the element entered is:\n"); + for(i=0;i + +#define max 20 + +int main() +{ + int len,i,arr[max],incr,j,temp; + + printf("How many values to be sorted ?"); + scanf("%d",&len); + printf("Enter %d values\n", len); + for(i=0;i=1) + { + for(i=0;iarr[j]) + { + temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; + } + } + } + printf("\nThe array when increment is %d\n",incr); + for(i=0;i +#define max 5 +int cque[max]; + +void addq(int *Front,int *r,int h); +int delq(int *Front,int *Rear); +int isQueueEmpty(int *Front); + +int main() +{ + int rear,front,n=0,fromq,val; + rear=front=-1; + while(n!=3) + { + printf("\n1. Adding an element into the circular queue\n"); + printf("2. Deleting an element from the circular queue\n"); + printf("3. Quit\n"); + printf("Enter your choice 1/2/3: "); + scanf("%d",&n); + switch(n) + { + case 1: printf("Enter the value to add to the circular queue: "); + scanf("%d",&val); + addq(&front,&rear,val); + break; + case 2: if(!isQueueEmpty(&front)) + { + fromq=delq(&front,&rear); + printf("The value removed from the circular queue is %d\n",fromq); + } + else + printf("The circular queue is empty\n"); + break; + } + } + return 0; +} + +void addq(int *Front,int *Rear,int Val) +{ + int oldR; + if(*Rear >=max-1) + { + oldR=*Rear; + *Rear=(*Rear+1)%5; + if(*Rear<*Front) + { + cque[*Rear]=Val; + printf("Value %d is added in circular queue\n", Val); + } + else + { + *Rear=oldR; + printf("Sorry the circular queue is full\n "); + } + } + else + { + (*Rear)++; + if(*Rear !=*Front) + { + cque[*Rear]=Val; + printf("Value %d is added in circular queue\n",Val); + } + else + { + (*Rear)--; + printf("Sorry, the circular queue is full\n"); + } + } + if(*Front==-1) + *Front=*Rear; +} + +int isQueueEmpty(int *Front) +{ + if(*Front==-1) + return (1); + else + return (0); +} + +int delq(int *Front,int *Rear) +{ + int val=0; + if(*Front==*Rear) + { + val=cque[*Front]; + *Front=*Rear=-1; + } + else + { + if(*Front >=max) + { + *Front=*Front%5; + if (*Front<*Rear) + { + val=cque[*Front]; + (*Front)++; + } + else + { + if(*Front==*Rear) + { + val=cque[*Front]; + *Front=*Rear=-1; + } + } + } + else + { + val=cque[*Front]; + (*Front)++; + } + } + return(val); +} \ No newline at end of file diff --git a/AppendixC/dequeuearray.c b/AppendixC/dequeuearray.c new file mode 100644 index 0000000..66a5ffb --- /dev/null +++ b/AppendixC/dequeuearray.c @@ -0,0 +1,196 @@ +#include +#define max 5 + +int dq[max]; + + +void addInFront(int *Front,int *r,int h); +void addInRear(int *Front,int *r,int h); +int delFFront(int *Front,int *Rear); +int delFRear(int *Front,int *Rear); +int isDQueueEmpty(int *Front); +int isDQueueFull(int *Front, int *Rear); + + +int main() +{ + int rear,front,n=0,fromdq,val; + rear=-1; + front=-1; + + while(n !=5) + { + printf("\n1. Adding element in front into the dequeue\n"); + printf("2. Adding element at rear into the dequeue\n"); + printf("3. Deleting an element from the front in dequeue\n"); + printf("4. Deleting an element from the rear in dequeue\n"); + printf("5. Quit\n"); + printf("Enter your choice 1/2/3/4/5: "); + scanf("%d",&n); + + + switch(n) + { + case 1: printf("Enter the value to be added to the front in dequeue: "); + scanf("%d",&val); + addInFront(&front,&rear,val); + break; + case 2: printf("Enter the value to be added at the rear in dequeue: "); + scanf("%d",&val); + addInRear(&front,&rear,val); + break; + case 3: if(!isDQueueEmpty(&front)) + { + fromdq=delFFront(&front,&rear); + printf("The value removed from the front in dequeue is %d\n",fromdq); + } + else + printf("The dequeue is empty\n"); + break; + case 4: if(!isDQueueEmpty(&front)) + { + fromdq=delFRear(&front,&rear); + printf("The value removed from the rear in dequeue is %d\n",fromdq); + } + else + printf("The dequeue is empty\n"); + break; + } + } + return 0; +} + + +int isDQueueEmpty(int *Front) +{ + if(*Front==-1) + return (1); + else + return (0); +} + +int isDQueueFull(int *Front, int *Rear) +{ + if((*Front==0) && (*Rear==max-1)) + return (1); + if(*Front==(*Rear+1)) + return (1); + return (0); +} + + +void addInRear(int *Front,int *Rear, int Val) +{ + int oldR; + if(*Rear ==max-1) + { + oldR=*Rear; + *Rear=(*Rear+1)%5; + if(*Rear < *Front) + { + dq[*Rear]=Val; + printf("Value %d is added in dequeue\n", Val); + } + else + { + *Rear=oldR; + printf("Sorry the dequeue is full\n "); + } + } + else + { + (*Rear)++; + if(*Rear !=*Front) + { + dq[*Rear]=Val; + printf("Value %d is added in dequeue\n",Val); + } + else + { + (*Rear)--; + printf("Sorry, the dequeue is full\n"); + } + } + if(*Front==-1) + *Front=*Rear; +} + + +void addInFront(int *Front,int *Rear,int Val) +{ + if (*Front == -1) + { + *Front = 0; + *Rear = 0; + } + else + { + if (*Front ==0) + *Front=max-1; + else + *Front=(*Front) -1; + } + dq[*Front]=Val; +} + +int delFFront(int *Front,int *Rear) +{ + int val=0; + if(*Front==*Rear) + { + val=dq[*Front]; + *Front=*Rear=-1; + } + else + { + if(*Front >=max) + { + *Front=*Front%5; + if (*Front<*Rear) + { + val=dq[*Front]; + (*Front)++; + } + else + { + if(*Front==*Rear) + { + val=dq[*Front]; + *Front=*Rear=-1; + } + } + } + else + { + val=dq[*Front]; + (*Front)++; + } + } + return(val); +} + +int delFRear(int *Front,int *Rear) +{ + int val=0; + if(*Front==*Rear) + { + val=dq[*Front]; + *Front=*Rear=-1; + } + else + { + if (*Rear == 0) + { + val=dq[*Rear]; + *Rear=max-1; + } + else + { + val=dq[*Rear]; + *Rear = *Rear-1; + } + } + return(val); +} + + diff --git a/AppendixC/queuearray.c b/AppendixC/queuearray.c new file mode 100644 index 0000000..f83d814 --- /dev/null +++ b/AppendixC/queuearray.c @@ -0,0 +1,79 @@ +#include +#define max 5 +int front=-1,rear=-1; + +void addq(int Que[], int Val); +int delq(int Que[]); +int IsQueueEmpty(); + +int main() +{ + int que[max]; + int val,n=0; + while(n!=3) + { + printf("\n1. Adding an element into the queue\n"); + printf("2. Deleting an element from the queue\n"); + printf("3. Quit\n"); + printf("Enter your choice 1/2/3: "); + scanf("%d",&n); + switch(n) + { + case 1: if(rear >=max-1) + printf("Sorry the queue is full\n"); + else + { + printf("Enter the value to add to queue: "); + scanf("%d",&val); + addq(que,val); + printf("Value %d is added to queue\n", val); + } + break; + case 2: if(!IsQueueEmpty()) + { + + val=delq(que); + printf("Value removed from queue %d\n",val); + } + else + printf("Sorry, the queue is empty\n"); + break; + } + } + return 0; +} + +void addq(int Que[], int Val) +{ + rear++; + Que[rear]=Val; + if(front==-1) + front=rear; +} + +int IsQueueEmpty() +{ + if(front==-1) + return (1); + else + return (0); + + +} + +int delq(int Que[]) +{ + int val; + if(front==rear) + { + val=Que[front]; + front=-1; + rear=-1; + } + else + { + val=Que[front]; + front++; + } + return(val); +} diff --git a/Chapter1/commoninarray.c b/Chapter01/commoninarray.c similarity index 100% rename from Chapter1/commoninarray.c rename to Chapter01/commoninarray.c diff --git a/Chapter1/deletefromarray.c b/Chapter01/deletefromarray.c similarity index 100% rename from Chapter1/deletefromarray.c rename to Chapter01/deletefromarray.c diff --git a/Chapter1/differencearray.c b/Chapter01/differencearray.c similarity index 100% rename from Chapter1/differencearray.c rename to Chapter01/differencearray.c diff --git a/Chapter1/identitymatrix.c b/Chapter01/identitymatrix.c similarity index 100% rename from Chapter1/identitymatrix.c rename to Chapter01/identitymatrix.c diff --git a/Chapter1/insertintoarray.c b/Chapter01/insertintoarray.c similarity index 100% rename from Chapter1/insertintoarray.c rename to Chapter01/insertintoarray.c diff --git a/Chapter1/matrixmulti.c b/Chapter01/matrixmulti.c similarity index 100% rename from Chapter1/matrixmulti.c rename to Chapter01/matrixmulti.c diff --git a/Chapter1/mergetwosortedarrays.c b/Chapter01/mergetwosortedarrays.c similarity index 100% rename from Chapter1/mergetwosortedarrays.c rename to Chapter01/mergetwosortedarrays.c diff --git a/Chapter1/sparsematrix.c b/Chapter01/sparsematrix.c similarity index 100% rename from Chapter1/sparsematrix.c rename to Chapter01/sparsematrix.c diff --git a/Chapter1/uniqueelements.c b/Chapter01/uniqueelements.c similarity index 100% rename from Chapter1/uniqueelements.c rename to Chapter01/uniqueelements.c diff --git a/Chapter2/convertvowels.c b/Chapter02/convertvowels.c similarity index 100% rename from Chapter2/convertvowels.c rename to Chapter02/convertvowels.c diff --git a/Chapter2/countofeach.c b/Chapter02/countofeach.c similarity index 100% rename from Chapter2/countofeach.c rename to Chapter02/countofeach.c diff --git a/Chapter2/countvowelsandcons.c b/Chapter02/countvowelsandcons.c similarity index 100% rename from Chapter2/countvowelsandcons.c rename to Chapter02/countvowelsandcons.c diff --git a/Chapter2/palendrome.c b/Chapter02/palendrome.c similarity index 100% rename from Chapter2/palendrome.c rename to Chapter02/palendrome.c diff --git a/Chapter2/repetitive.c b/Chapter02/repetitive.c similarity index 100% rename from Chapter2/repetitive.c rename to Chapter02/repetitive.c diff --git a/Chapter3/armstrong.c b/Chapter03/armstrong.c similarity index 100% rename from Chapter3/armstrong.c rename to Chapter03/armstrong.c diff --git a/Chapter3/binaryintohexa.c b/Chapter03/binaryintohexa.c similarity index 100% rename from Chapter3/binaryintohexa.c rename to Chapter03/binaryintohexa.c diff --git a/Chapter3/findpalindrome.c b/Chapter03/findpalindrome.c similarity index 100% rename from Chapter3/findpalindrome.c rename to Chapter03/findpalindrome.c diff --git a/Chapter3/gcd.c b/Chapter03/gcd.c similarity index 100% rename from Chapter3/gcd.c rename to Chapter03/gcd.c diff --git a/Chapter3/returnarray.c b/Chapter03/returnarray.c similarity index 100% rename from Chapter3/returnarray.c rename to Chapter03/returnarray.c diff --git a/Chapter04/assertdemoprog.c b/Chapter04/assertdemoprog.c new file mode 100644 index 0000000..d120c0f --- /dev/null +++ b/Chapter04/assertdemoprog.c @@ -0,0 +1,24 @@ +#include +#include + +int main(void) +{ + int c, noOfPassengers; + char fl_from[30], fl_to[30], dateofJourney[12]; + printf("How many passengers ? "); + scanf("%d",&noOfPassengers); + assert(noOfPassengers > 0 && "Number of passengers should be a positive integer"); + printf("Flight from: "); + while((c= getchar()) != '\n' && c != EOF); + gets(fl_from); + printf("Flight to: "); + gets(fl_to); + printf("Date of journey "); + scanf("%s", dateofJourney); + printf("The information entered is:\n"); + printf("Number of passengers %d\n", noOfPassengers); + printf("Flight from: %s\n", fl_from); + printf("Flight to: %s\n", fl_to); + printf("Date of journey: %s\n", dateofJourney); + return 0; +} \ No newline at end of file diff --git a/Chapter04/assertprog.c b/Chapter04/assertprog.c new file mode 100644 index 0000000..ab7c831 --- /dev/null +++ b/Chapter04/assertprog.c @@ -0,0 +1,31 @@ +#include +#include + +float findaverage(int *Ptr, int Count); +int main() +{ + int arr[]={3,9,1,6,2}; + float average; + int *ptr=NULL,count; + + ptr=arr; + count=5; + average=findaverage(ptr, count); + printf("Average of values is %f\n", average); + return(0); +} + +float findaverage(int *Ptr, int Count) +{ + int sum,i; + float Average; + assert(Ptr != NULL && "Pointer is not pointing to any array"); + sum=0; + for(i=0;i +#include + +struct customers +{ + int orderid; + char customer_name[20]; + float amount; +}; + +static_assert(sizeof(struct customers) == 28, "The structure is consuming unexpected number of bytes"); + +int main(void) +{ + printf("sizeof(int) %d\n",sizeof(int)); + printf("sizeof(float) %d\n",sizeof(float)); + printf("sizeof(char) %d\n",sizeof(char)); + printf("sizeof(struct customers) %d\n",sizeof(struct customers)); + return 0; +} \ No newline at end of file diff --git a/Chapter04/condcompile.c b/Chapter04/condcompile.c new file mode 100644 index 0000000..1b45daf --- /dev/null +++ b/Chapter04/condcompile.c @@ -0,0 +1,43 @@ +#include +#define Qty 10 +#define FestivalOffer 2 +#define DiscountCoupon 5 +#define Kindle 2 + +int main() +{ + int discount; + float price, totalAmount, afterDisc, afterFDisc; + + printf("Enter price of a book "); + scanf("%f", &price); + #if Qty >= 10 + discount=15; + #elif Qty >=5 + discount=10; + #else + discount=5; + #endif + totalAmount=Qty*price; + afterDisc=totalAmount - (totalAmount*discount)/100; + #ifdef FestivalOffer + afterFDisc=afterDisc-(totalAmount*FestivalOffer)/100; + #else + afterFDisc=afterDisc; + #endif + + + printf("Quantity = %d, Price is $ %.2f, total amount for the books is $ %.2f\n", Qty, price, totalAmount); + printf("Discount is %d%% and the total amount after discount is $ %.2f\n", discount, afterDisc); + #ifdef FestivalOffer + printf("Festival discount is %d%%, the total amount after festival discount is $ %.2f\n", FestivalOffer, afterFDisc); + #endif + #if defined (DiscountCoupon) + printf("You are also eligible for a discount coupon of $ %d\n", DiscountCoupon); + #endif + #ifndef Kindle + #define Kindle 1 + #endif + printf("You can use the Kindle version of the book for %d month(s)\n", Kindle); + return 0; +} \ No newline at end of file diff --git a/Chapter04/preconcat.c b/Chapter04/preconcat.c new file mode 100644 index 0000000..421531f --- /dev/null +++ b/Chapter04/preconcat.c @@ -0,0 +1,44 @@ +#include +#include + +#define pizzaprice(a, b) a##b +#define convertIntoStr(str) #str + +int main() +{ + float smallnormal=5; + float mediumnormal=7; + float largenormal=10; + float smallextra=7; + float mediumextra=9; + float largeextra=12; + char pizzasize[30]; + char topping[20]; + + printf("What size pizza you want? small/medium/large: "); + scanf("%s", pizzasize); + printf("Normal or with extra cheese? normal/extra: "); + scanf("%s",topping); + if(strcmp(topping, "normal")==0) + { + if(strcmp(pizzasize, "small")==0) + printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(small, normal)); + else + if(strcmp(pizzasize, "medium")==0) + printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(medium, normal)); + else + printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(large, normal)); + } + if(strcmp(topping, "extra")==0) + { + if(strcmp(pizzasize, "small")==0) + printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(small, extra)); + else + if(strcmp(pizzasize, "medium")==0) + printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(medium, extra)); + else + printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(large, extra)); + } + printf(convertIntoStr(Thanks for visiting us)); + return 0; +} \ No newline at end of file diff --git a/Chapter4/largestinarray.c b/Chapter05/largestinarray.c similarity index 94% rename from Chapter4/largestinarray.c rename to Chapter05/largestinarray.c index bcdfa88..0c95f9d 100644 --- a/Chapter4/largestinarray.c +++ b/Chapter05/largestinarray.c @@ -13,7 +13,7 @@ void main() scanf("%d",&p[i]); mx=p; ptr=p; - for(i=1;i +#define max 20 + +int binary_search(int[], int, int); + +int main() +{ + int len,found,numb,arr[max],i; + printf("Enter the length of an array: "); + scanf("%d",&len); + printf("Enter %d values in sorted order \n", len); + for(i=0;i< len;i++) + scanf("%d",&arr[i]); + printf("Enter the value to search "); + scanf("%d",&numb); + found=binary_search(arr,numb,len); + if(found==numb) + printf("Value %d is found in the list\n",numb); + else + printf("Value %d is not found in the list \n", numb); + return 0; +} + +int binary_search(int arr[], int pnumb,int plen) +{ + int lindex=0,mid,uindex=plen, nfound; + while(uindex >=lindex) + { + mid=(uindex+lindex)/2; + if(pnumb==arr[mid]) + { + nfound=arr[mid]; + break; + } + else + { + if(pnumb>arr[mid]) + lindex=mid+1; + else + uindex=mid-1; + } + } + return(nfound); +} diff --git a/Chapter09/bubblesort.c b/Chapter09/bubblesort.c new file mode 100644 index 0000000..621f671 --- /dev/null +++ b/Chapter09/bubblesort.c @@ -0,0 +1,28 @@ +# include +#define max 20 + +int main() +{ + int arr[max],temp,len,i,j; + printf("How many values are there? "); + scanf("%d",&len); + printf("Enter %d values to sort\n",len); + for(i=0;i=1;i--) + { + for(j=0;j<=i;j++) + { + if(arr[j]>arr[j+1]) + { + temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + printf("The sorted array is:\n"); + for(i=0;i +#define max 20 +int heap[max],len; + +void insheap(int h); +int delsheap(int j); + +int main() +{ + int arr[max],numb,i,j; + printf("How many elements to sort? "); + scanf("%d",&len); + printf("Enter %d values \n", len); + for(i=0;iheap[par]) + { + temp=heap[cur]; + heap[cur]=heap[par]; + heap[par]=temp; + cur=par; + par=(cur-1)/2; + + } + else break; + } while(cur!=0); + x++; + } +} + +int delsheap(int j) +{ + int loc,n=0,pos,lc=0,rc=0,temp=0; + loc=j; + pos=0; + n=heap[pos]; + heap[pos]=heap[loc]; + if(loc==0 || loc==1) return (n); + loc--; + lc=2*pos+1; + rc=2*pos+2; + while (rc <=loc) + { + if((heap[pos]>heap[lc] && heap[pos]>heap[rc])) + return(n); + else + { + if(heap[lc]>heap[rc]) + { + temp=heap[lc]; + heap[lc]=heap[pos]; + heap[pos]=temp; + pos=lc; + } + else + { + temp=heap[rc]; + heap[rc]=heap[pos]; + heap[pos]=temp; + pos=rc; + } + lc=2*pos+1; + rc=2*pos+2; + } + } + if(lc==loc) + { + if(heap[pos] +#define max 20 + +int main() +{ + int arr[max],i,j,temp,len; + + printf("How many numbers are there ? "); + scanf("%d",&len); + printf("Enter %d values to sort\n",len); + for(i=0;i0;j--) + { + if(arr[j] +# define stacksize 10 +#define arrsize 20 +int top1=-1,top2=-1; +int stack1[stacksize]; +int stack2[stacksize]; +int arr[arrsize]; + +int quick(int, int); +void pushstk1(int); +void pushstk2(int); +int popstk1(); +int popstk2(); + +int main() +{ + int sindex,eindex,lindex,uindex,k,pivot,i, len; + printf("How many numerical to sort? "); + scanf("%d",&len); + printf("Enter %d numericals:\n", len); + for(i=0;i<=len-1;i++) + scanf("%d",&arr[i]); + lindex=0; + uindex=len-1; + pushstk1(lindex); + pushstk2(uindex); + while(top1!=-1) + { + sindex=popstk1(); + eindex=popstk2(); + pivot=quick(sindex,eindex); + if(sindex=arr[li] && li !=ei) + ei--; + if(li==ei) return(li); + if(arr[li]>arr[ei]) + { + temp=arr[li]; + arr[li]=arr[ei]; + arr[ei]=temp; + li=ei; + } + while(arr[si]<=arr[li] && li!=si) + si++; + if(li==si) return(li); + if(arr[si]>arr[li]) + { + temp=arr[si]; + arr[si]=arr[li]; + arr[li]=temp; + li=si; + } + } + return 0; +} + +void pushstk1(int s) +{ + top1++; + stack1[top1]=s; +} + +void pushstk2(int e) +{ + top2++; + stack2[top2]=e; +} + +int popstk1() +{ + return(stack1[top1--]); +} + +int popstk2() +{ + return(stack2[top2--]); +} \ No newline at end of file diff --git a/Chapter10/adjlistdirect.c b/Chapter10/adjlistdirect.c new file mode 100644 index 0000000..e1de761 --- /dev/null +++ b/Chapter10/adjlistdirect.c @@ -0,0 +1,81 @@ +#include +#include + +struct node +{ + char nme; + struct node *vrt; + struct node *edg; +}; + +int main() +{ + int numb,i,j,noe; + char v1,v2; + struct node *startList,*newNode,*temp1,*temp2; + printf ("How many vertices are there ? "); + scanf("%d",&numb); + startList=NULL; + printf("Enter all vertices names\n"); + for(i=1;i<=numb;i++) + { + if (startList==NULL) + { + newNode =malloc(sizeof (struct node)); + scanf (" %c",&newNode->nme); + /* There is a space before %c */ + startList=newNode; + temp1=newNode; + newNode->vrt=NULL; + newNode->edg=NULL; + } + else + { + newNode=malloc(sizeof (struct node)); + scanf (" %c",&newNode->nme); + /* There is a space before %c */ + newNode->vrt=NULL; + newNode->edg=NULL; + temp1->vrt=newNode; + temp1=newNode; + } + } + printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n"); + printf("between v1 and v2. Enter 0 0 if over\n"); + noe=numb*(numb -1); + for(j=1;j<=noe;j++) + { + scanf(" %c %c",&v1,&v2); + /* There is a space before %c */ + if(v1=='0' && v2=='0')break; + temp1=startList; + while(temp1!=NULL && temp1->nme!=v1) + temp1=temp1->vrt; + if(temp1==NULL) + { + printf("Sorry no vertex exist by this name\n"); + break; + } + temp2=temp1; + while(temp2->edg!=NULL)temp2=temp2->edg; + newNode=malloc(sizeof (struct node)); + newNode->nme=v2; + temp2->edg=newNode; + newNode->edg=NULL; + newNode->vrt=NULL; + } + printf ("\nAdjacency List representation of Graph is\n"); + temp1=startList; + while (temp1!=NULL) + { + printf ("%c\t",temp1->nme); + temp2=temp1->edg; + while(temp2!=NULL) + { + printf("%c\t",temp2->nme); + temp2=temp2->edg; + } + printf("\n"); + temp1=temp1->vrt; + } +} \ No newline at end of file diff --git a/Chapter10/adjmatdirect.c b/Chapter10/adjmatdirect.c new file mode 100644 index 0000000..7fc1265 --- /dev/null +++ b/Chapter10/adjmatdirect.c @@ -0,0 +1,32 @@ +#include +#define max 10 +int main() +{ + static int edg[max][max],i,j,v1,v2, numb; + printf("How many vertices are there? "); + scanf("%d",&numb); + printf("We assume that the vertices are numbered from : "); + for(i=1;i<=numb;i++) printf("%d ", i); + printf("\nEnter the edges of the graph. Like 1 4 if there is an \n"); + printf("edge between vertex 1 and 4. Enter 0 0 when over\n"); + for(i=1;i<=numb*(numb-1);i++) + { + /* The for loop will run for at most numb*(numb-1) times because, the number of edges are at most numb*(numb-1) where numb is the number of vertices */ + scanf("%d %d",&v1,&v2); + if(v1==0 && v2==0)break; + edg[v1][v2]=1; + } + printf("\nThe adjacency matrix for the graph is \n"); + for(i=1;i<=numb;i++) printf("\t%d", i); + printf("\n-----------------------------------------------------\n"); + for(i=1;i<=numb;i++) + { + printf("%d |\t",i); + for(j=1;j<=numb;j++) + { + printf("%d\t",edg[i][j]); + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/Chapter10/adjmatundirect.c b/Chapter10/adjmatundirect.c new file mode 100644 index 0000000..9628ff7 --- /dev/null +++ b/Chapter10/adjmatundirect.c @@ -0,0 +1,33 @@ +#include +#define max 10 +int main() +{ + static int edg[max][max],i,j,v1,v2, numb; + printf("How many vertices are there? "); + scanf("%d",&numb); + printf("We assume that the vertices are numbered from : "); + for(i=1;i<=numb;i++) printf("%d ", i); + printf("\nEnter the edges of the graph. Like 1 4 if there is an \n"); + printf("edge between vertex 1 and 4. Enter 0 0 when over\n"); + for(i=1;i<=numb*(numb-1);i++) + { + /* The for loop will run for at most numb*(numb-1) times because, the number of edges are at most numb*(numb-1) where numb is the number of vertices */ + scanf("%d %d",&v1,&v2); + if(v1==0 && v2==0)break; + edg[v1][v2]=1; + edg[v2][v1]=1; + } + printf("\nThe adjacency matrix for the graph is \n"); + for(i=1;i<=numb;i++) printf("\t%d", i); + printf("\n----------------------------------------------------------\n"); + for(i=1;i<=numb;i++) + { + printf("%d |\t",i); + for(j=1;j<=numb;j++) + { + printf("%d\t",edg[i][j]); + } + printf("\n"); + } + return 0; +} diff --git a/Chapter10/breadthfirsttrav.c b/Chapter10/breadthfirsttrav.c new file mode 100644 index 0000000..059060b --- /dev/null +++ b/Chapter10/breadthfirsttrav.c @@ -0,0 +1,144 @@ +#include +#include + +#define max 20 + +enum Setmarked{Y,N}; +struct node +{ + char nme; + struct node *vrt; + struct node *edg; + enum Setmarked marked; +}; + +struct node *que[max]; +int rear=-1,front=-1; +void queue(struct node *paramNode); +struct node *dequeue(); + +int main() +{ + int numb,i,j,noe; + char v1,v2; + struct node *startList,*newNode,*temp1,*temp2, *temp3; + printf ("How many vertices are there ?"); + scanf("%d",&numb); + startList=NULL; + printf("Enter all vertices names\n"); + for(i=1;i<=numb;i++) + { + if (startList==NULL) + { + newNode =malloc(sizeof (struct node)); + scanf (" %c",&newNode->nme); + /* There is a space before %c */ + startList=newNode; + temp1=newNode; + newNode->vrt=NULL; + newNode->edg=NULL; + newNode->marked=N; + } + else + { + newNode=malloc(sizeof (struct node)); + scanf (" %c",&newNode->nme); + /* There is a space before %c */ + newNode->vrt=NULL; + newNode->edg=NULL; + newNode->marked=N; + temp1->vrt=newNode; + temp1=newNode; + } + } + printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n"); + printf("between v1 and v2. Enter 0 0 if over\n"); + noe=numb*(numb-1); + for(j=1;j<=noe;j++) + { + scanf(" %c %c",&v1,&v2); + /* There is a space before %c */ + if(v1=='0' && v2=='0')break; + temp1=startList; + while(temp1!=NULL && temp1->nme!=v1) + temp1=temp1->vrt; + if(temp1==NULL) + { + printf("Sorry no vertex exist by this name\n"); + break; + } + temp2=temp1; + while(temp2->edg!=NULL)temp2=temp2->edg; + newNode=malloc(sizeof (struct node)); + newNode->nme=v2; + temp2->edg=newNode; + newNode->edg=NULL; + newNode->vrt=NULL; + } + printf ("\nAdjacency List representation of Graph is\n"); + temp1=startList; + while (temp1!=NULL) + { + printf ("%c\t",temp1->nme); + temp2=temp1->edg; + while(temp2 !=NULL) + { + printf("%c\t",temp2->nme); + temp2=temp2->edg; + } + printf("\n"); + temp1=temp1->vrt; + } + printf("\nBreadth First traversal of the graph is \n"); + temp1=startList; + if(temp1==NULL) + printf("Sorry no vertices in the graph\n"); + else + queue(temp1); + while(rear !=-1) + { + temp3=dequeue(); + temp1=startList; + while(temp1->nme !=temp3->nme)temp1=temp1->vrt; + temp3=temp1; + if(temp3->marked==N) + { + printf("%c\t",temp3->nme); + temp3->marked=Y; + temp2=temp3->edg; + while(temp2!=NULL) + { + queue(temp2); + temp2=temp2->edg; + } + } + } + return 0; +} + +void queue(struct node *paramNode) +{ + rear++; + que[rear]=paramNode; + if(front==-1)front=0; +} + +struct node *dequeue() +{ + struct node *tempNode; + if(front==rear) + { + tempNode=que[front]; + front=-1; + rear=-1; + } + else + { + tempNode=que[front]; + front++; + } +return(tempNode); +} + + + diff --git a/Chapter10/depthfirsttrav.c b/Chapter10/depthfirsttrav.c new file mode 100644 index 0000000..763716b --- /dev/null +++ b/Chapter10/depthfirsttrav.c @@ -0,0 +1,126 @@ +#include +#include +#define max 20 +enum Setmarked{Y,N}; +struct node +{ + char nme; + struct node *vrt; + struct node *edg; + enum Setmarked marked; +}; + +struct node *stack[max]; +int top=-1; +void push(struct node *h); +struct node *pop(); + +int main() +{ + int numb,i,j,noe; + char v1,v2; + struct node *startList,*newNode,*temp1,*temp2, *temp3; + printf ("How many vertices are there ?"); + scanf("%d",&numb); + startList=NULL; + printf("Enter all vertices names\n"); + for(i=1;i<=numb;i++) + { + if (startList==NULL) + { + newNode =malloc(sizeof (struct node)); + scanf (" %c",&newNode->nme); + /* There is a white space before %c */ + startList=newNode; + temp1=newNode; + newNode->vrt=NULL; + newNode->edg=NULL; + newNode->marked=N; + } + else + { + newNode=malloc(sizeof (struct node)); + scanf (" %c",&newNode->nme); + /* There is a white space before %c */ + newNode->vrt=NULL; + newNode->edg=NULL; + newNode->marked=N; + temp1->vrt=newNode; + temp1=newNode; + } + } + printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n"); + printf("between v1 and v2. Enter 0 0 if over\n"); + noe=numb*(numb-1); + for(j=1;j<=noe;j++) + { + scanf(" %c %c",&v1,&v2); + /* There is a white space before %c */ + if(v1=='0' && v2=='0')break; + temp1=startList; + while(temp1!=NULL && temp1->nme!=v1) + temp1=temp1->vrt; + if(temp1==NULL) + { + printf("Sorry no vertex exist by this name\n"); + break; + } + temp2=temp1; + while(temp2->edg!=NULL)temp2=temp2->edg; + newNode=malloc(sizeof (struct node)); + newNode->nme=v2; + temp2->edg=newNode; + newNode->edg=NULL; + newNode->vrt=NULL; + } + printf ("\nAdjacency List representation of Graph is\n"); + temp1=startList; + while (temp1!=NULL) + { + printf ("%c\t",temp1->nme); + temp2=temp1->edg; + while(temp2!=NULL) + { + printf("%c\t",temp2->nme); + temp2=temp2->edg; + } + printf("\n"); + temp1=temp1->vrt; + } + printf("\nDepth First traversal of the graph is \n"); + temp1=startList; + if(temp1==NULL) + printf("Sorry no vertices in the graph\n"); + else + push(temp1); + while(top >=0) + { + temp3=pop(); + temp1=startList; + while(temp1->nme !=temp3->nme)temp1=temp1->vrt; + temp3=temp1; + if(temp3->marked==N) + { + printf("%c\t",temp3->nme); + temp3->marked=Y; + temp2=temp3->edg; + while(temp2!=NULL) + { + push(temp2); + temp2=temp2->edg; + } + } + } + return 0; +} + +void push(struct node *h) +{ + top++; + stack[top]=h; +} + +struct node *pop() +{ + return(stack[top--]); +} \ No newline at end of file diff --git a/Chapter10/kruskal.c b/Chapter10/kruskal.c new file mode 100644 index 0000000..ade43b5 --- /dev/null +++ b/Chapter10/kruskal.c @@ -0,0 +1,199 @@ +#include +#include +#define max 20 + +struct node +{ + int nme; + int wt; + struct node *v; + struct node *e; +}; + +typedef struct lst +{ + int u,v; + int wt; + struct lst *nxt; +}lst; + +lst *pq=NULL; +lst *tr=NULL; +void addpqu(int a, int b, int w); +void maketree(); +void disptree(); +lst *delet(); +int parent[max]; + +int main() +{ + int n,i,j,noe,w; + int a,b; + struct node *adj,*newNode,*p,*q; + printf ("How many vertices are there ? "); + scanf("%d",&n); + for(i=1;i<=n;i++)parent[i]=0; + printf("The vertices are named\n"); + for(i=1;i<=n;i++)printf("%d\t",i); + printf("for convenience \n"); + for(i=1;i<=n;i++) + { + if (i==1) + { + newNode =malloc(sizeof (struct node)); + newNode->nme=i; + adj=newNode; + p=newNode; + newNode->v=NULL; + newNode->e=NULL; + } + else + { + newNode=malloc(sizeof (struct node)); + newNode->nme=i; + newNode->v=NULL; + newNode->e=NULL; + p->v=newNode; + p=newNode; + } + } + printf("Enter the edges between vertices. Enter 1 3, if there is an edge\n"); + printf("between 1 and 3. Enter 0 0 if over\n"); + noe=n*(n-1); + for(j=1;j<=noe;j++) + { + printf("Enter edge: "); + scanf("%d %d",&a,&b); + if(a==0 && b==0)break; + printf("Enter weight: "); + scanf("%d",&w); + p=adj; + while(p!=NULL && p->nme!=a) + p=p->v; + if(p==NULL) + { + printf("Sorry no vertex exist by this name\n"); + break; + } + q=p; + while(q->e!=NULL)q=q->e; + newNode=malloc(sizeof (struct node)); + newNode->nme=b; + newNode->wt=w; + q->e=newNode; + newNode->e=NULL; + newNode->v=NULL; + addpqu(a,b,w); + } + printf ("Adjacency List representation of Graph is\n"); + p=adj; + while (p!=NULL) + { + printf ("%d\t",p->nme); + q=p->e; + while(q!=NULL) + { + printf("%d\t",q->nme); + q=q->e; + } + printf("\n"); + p=p->v; + } + maketree(); + disptree(); + return 0; +} + +void addpqu(int a, int b, int w) +{ + lst *newNode,*k,*h; + newNode=(lst *)malloc(sizeof(lst)); + newNode->u=a; + newNode->v=b; + newNode->wt=w; + newNode->nxt=NULL; + if(pq==NULL) + pq = newNode; + else + { + if(newNode->wt < pq->wt) + { + newNode->nxt=pq; + pq=newNode; + } + else + { + k=pq; + while((k!=NULL) &&(k->wt <= newNode->wt)) + { + h=k; + k=k->nxt; + } + h->nxt=newNode; + newNode->nxt=k; + } + } +} + +lst *delet() +{ +lst *q; +if (pq !=NULL) +{ + q=pq; + pq=pq->nxt; + return q; +} +else + return NULL; +} + +void maketree() +{ + lst *newNode,*p; + int x,y,r1,r2; + newNode=delet(); + while(newNode !=NULL) + { + newNode->nxt=NULL; + x=newNode->u; + y=newNode->v; + while(x>0) + { + r1=x; + x=parent[x]; + } + while(y>0) + { + r2=y; + y=parent[y]; + } + if(r1 !=r2) + { + parent[r2]=r1; + if (tr==NULL) + { + tr=newNode; + p=tr; + } + else + { + p->nxt=newNode; + p=newNode; + } + } + newNode=delet(); + } +} + +void disptree() +{ + lst *t; + t=tr; + printf("Minimal Spanning tree with Kruskal Algorithm is \n"); + while(t!=NULL) + { + printf("%d %d\n",t->u,t->v); + t=t->nxt; + } +} \ No newline at end of file diff --git a/Chapter10/prims.c b/Chapter10/prims.c new file mode 100644 index 0000000..e432c42 --- /dev/null +++ b/Chapter10/prims.c @@ -0,0 +1,239 @@ +#include +#include +#define max 20 + +struct node +{ + int nme; + int wt; + struct node *vrt; + struct node *edg; +}; +struct node *startList; + +struct lst +{ + int u,v; + int wt; + struct lst *next; +}lst; + +struct lst *pq=NULL; +struct lst *tr=NULL; +void addpqu(int a, int b, int w); +void maketree(); +void disptree(); +struct lst *delet(); +int visited[max]; +int n,nov=0; + +int main() +{ + int i,j,noe,w; + int a,b; + struct node *newNode,*temp1,*temp2; + printf ("How many vertices are there ?"); + scanf("%d",&n); + printf("The vertices are named\n"); + for(i=1;i<=n;i++)printf("%d\t",i); + printf("for convenience \n"); + startList=NULL; + for(i=1;i<=n;i++) + { + if (startList==NULL) + { + newNode =malloc(sizeof (struct node)); + newNode->nme=i; + startList=newNode; + temp1=newNode; + newNode->vrt=NULL; + newNode->edg=NULL; + } + else + { + newNode=malloc(sizeof (struct node)); + newNode->nme=i; + newNode->vrt=NULL; + newNode->edg=NULL; + temp1->vrt=newNode; + temp1=newNode; + } + } + printf("Enter the edges between vertices. Enter 1 3, if there is an edge\n"); + printf("between 1 and 3. Enter 0 0 if over\n"); + noe=n*(n-1); + for(j=1;j<=noe;j++) + { + printf("Enter edge "); + scanf("%d %d",&a,&b); + if(a==0 && b==0)break; + printf("Enter weight "); + scanf("%d",&w); + temp1=startList; + while(temp1!=NULL && temp1->nme!=a) + { + temp1=temp1->vrt; + } + if(temp1==NULL) + { + printf("Sorry no vertex exist by this name\n"); + break; + } + temp2=temp1; + while(temp2->edg!=NULL)temp2=temp2->edg; + newNode=malloc(sizeof (struct node)); + newNode->nme=b; + newNode->wt=w; + temp2->edg=newNode; + newNode->edg=NULL; + newNode->vrt=NULL; + temp1=startList; + while(temp1!=NULL && temp1->nme!=b) + temp1=temp1->vrt; + if(temp1==NULL) + { + printf("Sorry no vertex exist by this name\n"); + break; + } + temp2=temp1; + while(temp2->edg!=NULL)temp2=temp2->edg; + newNode=malloc(sizeof (struct node)); + newNode->nme=a; + newNode->wt=w; + temp2->edg=newNode; + newNode->edg=NULL; + newNode->vrt=NULL; + + } + printf ("Adjacency List representation of Graph is\n"); + temp1=startList; + while (temp1!=NULL) + { + printf ("%d\t",temp1->nme); + temp2=temp1->edg; + while(temp2!=NULL) + { + printf("%d\t",temp2->nme); + temp2=temp2->edg; + } + printf("\n"); + temp1=temp1->vrt; + } + temp1=startList; + temp2=temp1->edg; + while(temp2!=NULL) + { + addpqu(temp1->nme,temp2->nme, temp2->wt); + temp2=temp2->edg; + } + maketree(); + disptree(); + return 0; +} + +void addpqu(int a, int b, int w) +{ + struct lst *lstNode,*findloc1,*findloc2; + lstNode=malloc(sizeof(struct lst)); + lstNode->u=a; + lstNode->v=b; + lstNode->wt=w; + lstNode->next=NULL; + if(pq==NULL) + { + pq = lstNode; + } + else + { + if(lstNode->wt < pq->wt) + { + lstNode->next=pq; + pq=lstNode; + } + else + { + findloc1=pq; + while((findloc1!=NULL) && (findloc1->wt <= lstNode->wt)) + { + findloc2=findloc1; + findloc1=findloc1->next; + } + findloc2->next=lstNode; + lstNode->next=findloc1; + } + } +} + +struct lst *delet() +{ + struct lst *tempNode; + if (pq !=NULL) + { + tempNode=pq; + pq=pq->next; + return tempNode; + } + else + return NULL; +} + +void maketree() +{ + struct lst *lstNode,*tempNode1,*tempNode2; + struct node *x,*y; + int i,j; + while(nov u) + { + for(j=1;j<=nov;j++) + if(visited[j]==lstNode->v) goto nxt; + } + } + for(i=1;i<=nov;i++) + if(visited[i]==lstNode->u) goto rpt; + nov++; + visited[nov]=lstNode->u; +rpt: for(i=1;i<=nov;i++) + { + if(visited[i]==lstNode->v) goto rptt; + } + nov++; + visited[nov]=lstNode->v; +rptt: lstNode->next=NULL; + if (tr==NULL) + { + tr=lstNode; + tempNode1=tr; + } + else + { + tempNode1->next=lstNode; + tempNode1=lstNode; + } + x=startList; + while(x->nme!=lstNode->v)x=x->vrt; + y=x->edg; + pq=NULL; + while(y!=NULL) + { + addpqu(x->nme,y->nme, y->wt); + y=y->edg; + } + } +} + +void disptree() +{ + struct lst *t; + t=tr; + printf("Minimal Spanning tree with Prims Algorithm is \n"); + while(t!=NULL) + { + printf("%d %d\n",t->u,t->v); + t=t->next; + } +} \ No newline at end of file diff --git a/Chapter11/binarysearchtree.c b/Chapter11/binarysearchtree.c new file mode 100644 index 0000000..1bc0ca1 --- /dev/null +++ b/Chapter11/binarysearchtree.c @@ -0,0 +1,106 @@ +#include +#include + +#define max 10 + +struct tree +{ + int data; + struct tree *right; + struct tree *left; +}; + +void build(int Arr[], int Len); +struct tree *makeroot(int val); +void rightchild(struct tree * rootNode, int val); +void leftchild(struct tree *rootNode, int val); +void travino(struct tree *node); + +int main() +{ + int arr[max],i,len; + printf("How many elements are there for making the binary search tree? "); + scanf("%d", &len); + printf("Enter %d elements in array \n",len); + for(i=0;idata ) + { + if(temp->left !=NULL) + { + temp=temp->left; + continue; + } + leftchild(temp,Arr[j]); + } + if(Arr[j] >temp->data) + { + if(temp->right !=NULL) + { + temp=temp->right; + continue; + } + rightchild(temp,Arr[j]); + } + break; + } + } + printf ("Binary Search Tree is created\n"); + printf("The inorder traversal of the tree is as follows:\n"); + travino(rootNode); +} + +struct tree *makeroot(int val) +{ + struct tree *rootNode; + rootNode=(struct tree *)malloc(sizeof(struct tree)); + rootNode->data=val; + rootNode->right=NULL; + rootNode->left=NULL; + return rootNode; +} + +void leftchild(struct tree *rootNode, int val) +{ + struct tree *newNode; + newNode=(struct tree *)malloc(sizeof(struct tree)); + newNode->data=val; + newNode->left=NULL; + newNode->right=NULL; + rootNode->left=newNode; +} + +void rightchild(struct tree *rootNode, int val) +{ + struct tree *newNode; + newNode=(struct tree *)malloc(sizeof(struct tree)); + newNode->data=val; + newNode->left=NULL; + newNode->right=NULL; + rootNode->right=newNode; +} + +void travino(struct tree *node) +{ + if (node!=NULL) + { + travino(node->left); + printf ("%d\t",node->data); + travino(node->right); + } +} + diff --git a/Chapter11/circularlinkedlist.c b/Chapter11/circularlinkedlist.c new file mode 100644 index 0000000..7fc7a79 --- /dev/null +++ b/Chapter11/circularlinkedlist.c @@ -0,0 +1,81 @@ +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +struct node *startList=NULL; + +void addlist(struct node **h); +void disp(); + +int main() +{ + struct node *newNode; + int n=0,i,k; + while(n!=3) + { + printf("\n1. Adding elements to the circular linked list\n"); + printf("2. Displaying elements of the circular linked list\n"); + printf("3. Quit\n"); + printf("Enter your choice 1/2/3: "); + scanf("%d",&n); + switch(n) + { + case 1: printf("How many values are there "); + scanf("%d",&k); + printf("Enter %d values\n",k); + for(i=1;i<=k;i++) + { + newNode=(struct node *)malloc(sizeof(struct node)); + scanf("%d",&newNode->data); + addlist(&newNode); + } + printf("Values added in Circular Linked List \n"); + break; + case 2: disp(); + break; + } + } + return 0; +} + +void addlist(struct node **NewNode) +{ + struct node *temp; + if(startList==NULL) + { + startList=*NewNode; + startList->next=startList; + } + else + { + temp=startList; + while(temp->next !=startList) + temp=temp->next; + temp->next=*NewNode; + temp=*NewNode; + temp->next=startList; + } +} + +void disp() +{ + struct node *temp; + if (startList==NULL) + printf("The circular linked list is empty\n"); + else + { + printf("Following are the elements in circular linked list:\n"); + printf("%d\n",startList->data); + temp=startList->next; + while(temp !=startList) + { + printf("%d\n",temp->data); + temp=temp->next; + } + } +} diff --git a/Chapter11/doublylinkedlist.c b/Chapter11/doublylinkedlist.c new file mode 100644 index 0000000..b3f8656 --- /dev/null +++ b/Chapter11/doublylinkedlist.c @@ -0,0 +1,105 @@ +#include +#include +#include + +struct node +{ + int data; + struct node *next, *prev; +}; + +struct node *startList, *endList; +void createdoubly(); +void list_lifo(); +void list_fifo(); + +int main() +{ + + + int n=0; + while(n!=4) + { + printf("\n1. Creating a doubly linked list\n"); + printf("2. Displaying elements in L.I.F.O. order\n"); + printf("3. Displaying elements in F.I.F.O. order\n"); + printf("4. Quit\n"); + printf("Enter your choice 1/2/3/4: "); + scanf("%d",&n); + switch(n) + { + case 1: createdoubly(); + break; + case 2: list_lifo(); + break; + case 3: list_fifo(); + break; + + } + } + return 0; +} + +void createdoubly() +{ + char k[10]; + struct node *newNode; + startList=NULL; + strcpy(k,"yes"); + while(strcmp(k,"yes")==0 || strcmp(k, "Yes")==0) + { + if(startList==NULL) + { + newNode=(struct node *)malloc(sizeof(struct node)); + printf("Enter the value to add: "); + scanf("%d",&newNode->data); + newNode->next=NULL; + newNode->prev=NULL; + startList = newNode; + endList=startList; + } + else + { + newNode=(struct node *)malloc(sizeof(struct node)); + printf("Enter the value to add: "); + scanf("%d",&newNode->data); + newNode->next=NULL; + newNode->prev=endList; + endList->next = newNode; + endList=newNode; + } + printf("Want to add more yes/no? "); + scanf("%s",k); + } + printf("Doubly linked list is created\n"); +} + +void list_lifo() +{ + struct node *temp; + temp=endList; + if(temp !=NULL) + { + printf("The elements of the doubly linked list in L.I.F.O. order :\n"); + while(temp!=NULL) + { + printf("%d\n",temp->data); + temp=temp->prev; + } + } + else + printf("The doubly linked list is empty\n"); +} + +void list_fifo() +{ + struct node *temp; + temp=startList; + printf("The elements of the doubly linked list in F.I.F.O. order: \n"); + while(temp!=NULL) + { + printf("%d\n",temp->data); + temp=temp->next; + } +} + diff --git a/Chapter11/postordernonrec.c b/Chapter11/postordernonrec.c new file mode 100644 index 0000000..d2a5cc9 --- /dev/null +++ b/Chapter11/postordernonrec.c @@ -0,0 +1,146 @@ +#include +#include + +struct tree +{ + int data; + struct tree *right; + struct tree *left; +}; + +struct stackstruc +{ + int valueArray[15]; + struct tree *nodeArray[15]; +}; + +struct stackstruc stack; +int top=-1; + +struct tree *makeroot(int val); +void rightchild(struct tree *rootNode, int val); +void leftchild(struct tree *rootNode, int val); +void nontravpost(struct tree *node); +void pushNode (struct tree *node, int val); +struct tree *popNode(); +int popVal(); + +int main() +{ + struct tree *temp, *rootNode; + int val; + printf ("Enter elements of tree and 0 to quit\n"); + scanf("%d",&val); + rootNode=makeroot(val); + scanf("%d",&val); + while(val !=0) + { + temp=rootNode; + while (1) + { + if(val < temp->data ) + { + if(temp->left !=NULL) + { + temp=temp->left; + continue; + } + leftchild(temp,val); + } + if(val >temp->data) + { + if(temp->right !=NULL) + { + temp=temp->right; + continue; + } + rightchild(temp,val); + } + break; + } + scanf("%d",&val); + } + printf ("\nTraversal of tree in Postorder without using recursion: \n"); + nontravpost(rootNode); +} + +struct tree *makeroot(int val) +{ + struct tree *rootNode; + rootNode=(struct tree *)malloc(sizeof(struct tree)); + rootNode->data=val; + rootNode->right=NULL; + rootNode->left=NULL; + return rootNode; +} + +void leftchild(struct tree *rootNode, int val) +{ + struct tree *newNode; + newNode=(struct tree *)malloc(sizeof(struct tree)); + newNode->data=val; + newNode->left=NULL; + newNode->right=NULL; + rootNode->left=newNode; +} + +void rightchild(struct tree *rootNode, int val) +{ + struct tree *newNode; + newNode=(struct tree *)malloc(sizeof(struct tree)); + newNode->data=val; + newNode->left=NULL; + newNode->right=NULL; + rootNode->right=newNode; +} + +void nontravpost(struct tree *node) +{ + struct tree *temp; + int val; + temp=node; + while (1) + { + while(temp!=NULL) + { + pushNode(temp,0); + temp=temp->left; + } + while(top >=0) + { + temp=popNode(); + val=popVal(); + if (val==0) + { + if (temp->right !=NULL) + { + pushNode(temp,1); + temp=temp->right; + break; + } + } + printf("%d\n",temp->data); + continue; + } + + if((temp==NULL) || (top <0)) break; + else continue; + } +} + +void pushNode (struct tree *node, int val) +{ + top++; + stack.nodeArray[top]=node; + stack.valueArray[top]=val; +} + +struct tree *popNode() +{ + return (stack.nodeArray[top]); +} + +int popVal() +{ + return(stack.valueArray[top--]); + } diff --git a/Chapter11/stacklinkedlist.c b/Chapter11/stacklinkedlist.c new file mode 100644 index 0000000..be19d29 --- /dev/null +++ b/Chapter11/stacklinkedlist.c @@ -0,0 +1,59 @@ +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +void push(struct node *NewNode,struct node **Top); +struct node *pop(struct node **Top); + +int main() +{ + struct node *newNode,*top, *recNode; + int n=0; + top=NULL; + while(n!=3) + { + printf("\n1. Pushing an element into the stack\n"); + printf("2. Popping out an element from the stack\n"); + printf("3. Quit\n"); + printf("Enter your choice 1/2/3:"); + scanf("%d",&n); + switch(n) + { + case 1: newNode=(struct node *)malloc(sizeof(struct node)); + printf("Enter the value to push: "); + scanf("%d",&newNode->data); + push(newNode,&top); + printf("Value %d is pushed to stack\n", newNode->data); + break; + case 2: recNode=pop(&top); + if(recNode==NULL)printf("Stack is empty\n"); + else + printf("The value popped is %d\n",recNode->data); + break; + } + } + return 0; +} + +void push(struct node *NewNode,struct node **Top) +{ + NewNode->next=*Top; + *Top=NewNode; +} + +struct node *pop(struct node **Top) +{ + struct node * temp; + if(*Top==NULL) return(NULL); + else + { + temp=*Top; + (*Top)=(*Top)->next; + return(temp); + } +} diff --git a/Chapter12/ballanim.c b/Chapter12/ballanim.c new file mode 100644 index 0000000..ae4cc25 --- /dev/null +++ b/Chapter12/ballanim.c @@ -0,0 +1,38 @@ +#include +#include +#include +#define pi 3.142857 + +void animball (void) +{ + int x,y; + glClearColor(0.0, 0.0, 0.0, 1.0); + glColor3f(0.0, 1.0, 0.0); + glPointSize(1.0); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D(-350, 350, -350, 350); + for (float j = 0; j < 1000; j += 0.01) + { + glClear(GL_COLOR_BUFFER_BIT); + glBegin(GL_POINTS); + for (int i=0; i <360; i++) + { + x = 100 * cos(i); + y = 100 * sin(i); + glVertex2i(x / 2 - 1 * cos(j), y / 2 - 150* sin(j)); + } + glEnd(); + glFlush(); + } +} + +int main (int argc, char** argv) +{ + glutInit(&argc, argv); + glutCreateWindow("Animating a ball"); + glutInitWindowSize(1000, 1000); + glutInitWindowPosition(0, 0); + glutDisplayFunc(animball); + glutMainLoop(); +} \ No newline at end of file diff --git a/Chapter12/opengldrawbar.c b/Chapter12/opengldrawbar.c new file mode 100644 index 0000000..e96881c --- /dev/null +++ b/Chapter12/opengldrawbar.c @@ -0,0 +1,38 @@ +#include + +void display(){ + float x,y,width, result[] = {10.0, 15.0, 5.0}; + int i, barCount = 3; + x=1.0; + y = 0.0; + width = 2.0; + glColor3f(1.0, 0.0, 0.0); + glClearColor(1.0, 1.0, 1.0, 1.0); + gluOrtho2D(-5, 20, -5, 20); + glBegin(GL_LINES); + glVertex2f(-30, 0.0); + glVertex2f(30, 0.0); + glVertex2f(0.0, -30); + glVertex2f(0.0, 30); + glEnd(); + for(i=0; i + +void drawshapes() { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glBegin(GL_QUADS); + glColor3f(0.0f, 0.0f, 1.0f); + glVertex2f(0.0f, 0.0f); + glVertex2f( 0.0f, .75f); + glVertex2f( -.75f, .75f); + glVertex2f(-.75f, 0.0f); + glEnd(); + glLineWidth(2.0); + glColor3f(1.0, 0.0, 0.0); + glBegin(GL_LINES); + glVertex2f(-0.5, -0.5); + glVertex2f(0.5,-0.5); + glEnd(); + glColor3f(1.0, 0.0, 0.0); + glPointSize(3.0); + glBegin(GL_POINTS); + glVertex2f(-.25f, -0.25f); + glVertex2f(0.25f, -0.25f); + glEnd(); + glBegin(GL_TRIANGLES); + glColor3f( 0, 1, 0 ); + glVertex2f( 0,0 ); + glVertex2f( .5,.5 ); + glVertex2f( 1,0); + glEnd(); + glFlush(); +} + +int main(int argc, char** argv) { + glutInit(&argc, argv); + glutCreateWindow("Drawing some shapes"); + glutInitWindowSize(1500, 1500); + glutInitWindowPosition(0, 0); + glutDisplayFunc(drawshapes); + glutMainLoop(); + return 0; +} \ No newline at end of file diff --git a/Chapter12/opengldrawshapes2.c b/Chapter12/opengldrawshapes2.c new file mode 100644 index 0000000..9adacae --- /dev/null +++ b/Chapter12/opengldrawshapes2.c @@ -0,0 +1,27 @@ +#include +#include +#define pi 3.142857 + +void drawshapes() { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + glColor3f(0.0f, 1.0f, 0.0f); + glBegin(GL_LINE_LOOP); + for (int i=0; i <360; i++) + { + float angle = i*pi/180; + glVertex2f(cos(angle)*0.5,sin(angle)*0.5); + } + glEnd(); + glFlush(); +} + +int main(int argc, char** argv) { + glutInit(&argc, argv); + glutCreateWindow("Drawing some shapes"); + glutInitWindowSize(1500, 1500); + glutInitWindowPosition(0, 0); + glutDisplayFunc(drawshapes); + glutMainLoop(); + return 0; +} \ No newline at end of file diff --git a/Chapter12/openglmouseclick.c b/Chapter12/openglmouseclick.c new file mode 100644 index 0000000..aba0b69 --- /dev/null +++ b/Chapter12/openglmouseclick.c @@ -0,0 +1,61 @@ +#include + +int noOfClicks = 0; +int coord[2][2]; +int leftPressed = 0; + +void drawLine(void) +{ + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + glBegin(GL_LINES); + for(int i=0; i +#include + +char *str; +void freeup() +{ + free(str); + printf( "Allocated memory is freed \n"); +} + +int main() +{ + int retvalue; + retvalue = atexit(freeup); + if (retvalue != 0) { + printf("Registration of function for atexit () function failed\n"); + exit(1); + } + str = malloc( 20 * sizeof(char) ); + if( str== NULL ) + { + printf("Some error occurred in allocating memory\n"); + exit(1); + } + printf("Enter a string "); + scanf("%s", str); + printf("The string entered is %s\n", str); +} \ No newline at end of file diff --git a/Chapter14/atexistprog2.c b/Chapter14/atexistprog2.c new file mode 100644 index 0000000..da10df7 --- /dev/null +++ b/Chapter14/atexistprog2.c @@ -0,0 +1,39 @@ +#include +#include + +char *str; +void freeup1() +{ + free(str); + printf( "Allocated memory is freed \n"); +} + +void freeup2() +{ + printf( "The size of dynamic memory can be increased and decreased \n"); +} + +int main() +{ + int retvalue; + retvalue = atexit(freeup1); + if (retvalue != 0) { + printf("Registration of function freeup1() for atexit () function failed\n"); + exit(1); + } + retvalue = atexit(freeup2); + if (retvalue != 0) { + printf("Registration of function freeup2() for atexit () function failed\n"); + exit(1); + } + str = malloc( 20 * sizeof(char) ); + if( str== NULL ) + { + printf("Some error occurred in allocating memory\n"); + exit(1); + } + printf("Enter a string "); + scanf("%s", str); + printf("The string entered is %s\n", str); + +} \ No newline at end of file diff --git a/Chapter14/dynamicmem.c b/Chapter14/dynamicmem.c new file mode 100644 index 0000000..0b57a37 --- /dev/null +++ b/Chapter14/dynamicmem.c @@ -0,0 +1,39 @@ +#include +#include + +int main() +{ + int* ptr; + int m,n, i; + + printf("How many elements are there? "); + scanf("%d", &n); + ptr = (int*)calloc(n, sizeof(int)); + if (ptr == NULL) { + printf("Memory could not be allocated.\n"); + exit(0); + } + printf("Enter %d elements \n", n); + for (i = 0; i < n; ++i) + scanf("%d",&ptr[i]); + printf("\nThe elements entered are: \n"); + for (i = 0; i < n; ++i) + printf("%d\n", ptr[i]); + printf("\nHow many elements you want to add more? "); + scanf("%d",&m); + ptr = realloc(ptr, (m+n) * sizeof(int)); + printf("Enter values for %d elements\n",m); + for (i = n; i < (m+n); ++i) + scanf("%d",&ptr[i]); + printf("\nThe complete set of elements now are: \n"); + for (i = 0; i < (m+n); ++i) + printf("%d\n", ptr[i]); + printf("\nHow many elements you want to keep ? "); + scanf("%d", &m); + ptr = realloc(ptr, (m) * sizeof(int)); + printf("\nThe new set of elements now are: \n"); + for (i = 0; i < m; ++i) + printf("%d\n", ptr[i]); + free(ptr); + return 0; +} \ No newline at end of file diff --git a/Chapter14/signalhandling.c b/Chapter14/signalhandling.c new file mode 100644 index 0000000..048ba22 --- /dev/null +++ b/Chapter14/signalhandling.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +void sighandler1(int signum) { + printf("Ctrl + C is auto pressed \n"); +} + +void sighandler2(int signum) { + printf("You have pressed Ctrl+c\n"); + printf("Press Ctrl+c again to exit\n"); + (void) signal(SIGINT, SIG_DFL); +} + +int main () { + int x=1; + signal(SIGINT, sighandler1); + while(x<=5) { + printf("Signal will be raised automatically after 5 seconds\n"); + x++; + sleep(1); + } + raise(SIGINT); + signal(SIGINT, sighandler2); + while(1) { + printf("Infinite loop, press Ctrl+C to raise signal\n"); + sleep(1); + } + return(0); +} + diff --git a/Chapter14/timecalc.c b/Chapter14/timecalc.c new file mode 100644 index 0000000..6468548 --- /dev/null +++ b/Chapter14/timecalc.c @@ -0,0 +1,23 @@ +#include +#include + +void somefunction() +{ + for (int i=0; i<32000; i++) + { + for (int j=0; j<32000; j++) ; + } +} + +int main() +{ + clock_t clocktickstart, clocktickend; + double timeconsumed; + clocktickstart = clock(); + somefunction(); + clocktickend = clock(); + timeconsumed = (double)(clocktickend - clocktickstart) / CLOCKS_PER_SEC; + printf("Number of clocks ticks required in running the function is : %.3f\n", (double)(clocktickend - clocktickstart)); + printf("Time taken by program is : %.2f sec\n", timeconsumed); + return 0; +} \ No newline at end of file diff --git a/Chapter15/fastinp.c b/Chapter15/fastinp.c new file mode 100644 index 0000000..4e0b34b --- /dev/null +++ b/Chapter15/fastinp.c @@ -0,0 +1,21 @@ +#include + +int getdata() { + char cdigit = getchar_unlocked(); + int cnumb = 0; + while(cdigit<'0' || cdigit>'9') cdigit = getchar_unlocked(); + while(cdigit >='0' && cdigit <='9') { + cnumb = 10 * cnumb + cdigit - 48; + cdigit = getchar_unlocked(); + } + return cnumb; +} + +int main() +{ + int numb; + printf("Enter a number "); + numb=getdata(); + printf("The number entered is %d\n",numb); + return 0; +} \ No newline at end of file diff --git a/Chapter15/loopunrolling.c b/Chapter15/loopunrolling.c new file mode 100644 index 0000000..6c2bab0 --- /dev/null +++ b/Chapter15/loopunrolling.c @@ -0,0 +1,28 @@ +#include + +int main() { + int sum,i,limit,rem,quot,incr,x, count; + sum = 0; + printf("Enter limit "); + scanf("%d", &limit); + for(i=9;i>=1;i--) + { + rem=limit % i; + if (rem==0) break; + } + incr=i; + count=0; + for(i=1;i<=limit; i+=incr) + { + x=0; + while(x +#include + +int main() { + int distance; + char car_type[20]; + register int Acperkm,Nonacperkm,servicetax; + float carRent, totalrent; + + printf("How many kilometers? "); + scanf("%d", &distance); + printf("AC car or non AC ac/non? "); + scanf("%s", car_type); + Acperkm=3; + Nonacperkm=2; + servicetax=1; + if(strcmp(car_type, "ac")==0) + carRent=distance*Acperkm; + else + carRent=distance*Nonacperkm; + totalrent=carRent + (carRent*servicetax/100); + printf("The total rent for the car will be $ %.2f\n",totalrent); + return 0; +} \ No newline at end of file diff --git a/Chapter16/asmdivide.c b/Chapter16/asmdivide.c new file mode 100644 index 0000000..6d8987a --- /dev/null +++ b/Chapter16/asmdivide.c @@ -0,0 +1,12 @@ +#include + +void main() { + int var1=19,var2=4, var3=0, remainder, quotient; + asm("divl %%ebx;" + "movl %%edx, %0" + : "=b" (remainder) , "=r" (quotient) + : "a" (var1), "b" (var2), "d" (var3) + + ); + printf ("On dividing %d by %d, you get %d quotient and %d remainder\n", var1, var2, quotient, remainder); +} diff --git a/Chapter16/binintodec.c b/Chapter16/binintodec.c new file mode 100644 index 0000000..c7aee0f --- /dev/null +++ b/Chapter16/binintodec.c @@ -0,0 +1,19 @@ +#include + +void main() +{ + int num,bin,temp,dec=0,topower=0; + + printf("Enter the binary number: "); + scanf("%d",&bin); + temp=bin; + while(bin >0) + { + num=bin %10; + num=num< + +void main() +{ + int num,i,x,temp;; + int p[10]; + printf("Enter Decimal Number : "); + scanf("%d",&num); + temp=num; + x=0; + while(num > 0) + { + if((num & 1) == 0 ) + { + p[x]=0; + x++; + } + else + { + p[x]=1; + x++; + } + num = num >> 1; + } + printf("Binary of %d is ",temp); + for(i=x-1;i>=0;i--)printf("%d",p[i]); +} \ No newline at end of file diff --git a/Chapter16/decintobin.c b/Chapter16/decintobin.c new file mode 100644 index 0000000..71d9397 --- /dev/null +++ b/Chapter16/decintobin.c @@ -0,0 +1,21 @@ +/* Converting Decimal to Binary using masking */ + +#include +void main() +{ + int i, totbits; + unsigned mask,num; + + printf("Enter decimal value: "); + scanf("%d", &num); + totbits=32; + mask = 1 << (totbits - 1); + for(i = 0; i < totbits; i++) + { + if((num & mask) == 0 ) + printf("0"); + else + printf("1"); + mask >>= 1; + } +} diff --git a/Chapter16/multiasm.c b/Chapter16/multiasm.c new file mode 100644 index 0000000..5ff849e --- /dev/null +++ b/Chapter16/multiasm.c @@ -0,0 +1,13 @@ +#include +#include + +int main(int argc, char **argv) +{ + int32_t var1=10, var2=20, multi = 0; + asm volatile ("imull %%ebx,%%eax;" + : "=a" (multi) /* output: multi = EAX */ + : "a" (var1), "b" (var2) /* inputs: EAX = var1, EBX = var2 */ + ); + printf("Multiplication = %d\n", multi); + return 0; +} \ No newline at end of file diff --git a/Chapter17/ArduinoLedBlink/ArduinoLedBlink.ino b/Chapter17/ArduinoLedBlink/ArduinoLedBlink.ino new file mode 100644 index 0000000..1e805ed --- /dev/null +++ b/Chapter17/ArduinoLedBlink/ArduinoLedBlink.ino @@ -0,0 +1,14 @@ +int Led = 13; + +void setup() { +pinMode(Led, OUTPUT); + +} + +void loop() { + digitalWrite(Led, HIGH); + delay(1000); + digitalWrite(Led, LOW); + delay(1000); + +} diff --git a/Chapter17/ArduinoTakinginput/ArduinoTakinginput.ino b/Chapter17/ArduinoTakinginput/ArduinoTakinginput.ino new file mode 100644 index 0000000..a4f1a3a --- /dev/null +++ b/Chapter17/ArduinoTakinginput/ArduinoTakinginput.ino @@ -0,0 +1,31 @@ + +int Led = 13; +void setup() { + pinMode(Led,OUTPUT); + Serial.begin(9600); + Serial.println("Enter 0 to switch Off LED and 1 to switch it On"); +} + +void loop() { +if(Serial.available()) + { + int input=Serial.read(); + input=input-48; + if(input==0) + { + Serial.println("LED is OFF"); + digitalWrite(Led,LOW); + } + else if(input==1) + { + Serial.println("LED is ON"); + digitalWrite(Led,HIGH); + } + else + { + Serial.println("Enter 0 to switch Off LED and 1 to switch it On"); + } + } + + +} diff --git a/Chapter17/SensorApp/SensorApp.ino b/Chapter17/SensorApp/SensorApp.ino new file mode 100644 index 0000000..1fed175 --- /dev/null +++ b/Chapter17/SensorApp/SensorApp.ino @@ -0,0 +1,21 @@ +float voltage; +int tempPin = 0; + +void setup() { + Serial.begin(9600); +} + +void loop() { + voltage = analogRead(tempPin); + float tempInCelsius = voltage * 0.48828125; + float tempinFahrenheit = (tempInCelsius*9)/5 + 32; + Serial.print("Temperature in Celsius is: "); + Serial.print(tempInCelsius); + Serial.print("*C"); + Serial.println(); + Serial.print("Temperature in Fahrenheit is: "); + Serial.print(tempinFahrenheit); + Serial.print("*F"); + Serial.println(); + delay(1000); +} diff --git a/Chapter17/blinkingLed.c b/Chapter17/blinkingLed.c new file mode 100644 index 0000000..b351a83 --- /dev/null +++ b/Chapter17/blinkingLed.c @@ -0,0 +1,24 @@ +#include + +sbit LED = P1^0; +void Delay(int); +void main (void) +{ + while(1) + { + LED = 0; + Delay(500); + LED = 1; + Delay(500); + } +} + +void Delay(int n) +{ + int i,j; + for(i=0;i +#include + +void delay(void); +void main() + { +unsigned char i; + i=0x00; + while(++i) + { + P3=i; + delay(); + } + } + +void delay(void) +{ + int j; + int i; + for(i=0;i<1000;i++) + { + for(j=0;j<10000;j++) + { + } + } +} diff --git a/Chapter18/fileproblem.c b/Chapter18/fileproblem.c new file mode 100644 index 0000000..2438115 --- /dev/null +++ b/Chapter18/fileproblem.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#define BUFFSIZE 255 + +void main (int argc, char* argv[]) +{ + FILE *fp; + char str[BUFFSIZE]; + if (symlink("file2.txt","file1.txt") != 0) { + perror("symlink() error"); + unlink("file2.txt"); + exit(1); + } + else + { + fp = fopen ("file1.txt","w"); + if (fp == NULL) { + perror ("An error occurred in creating the file\n"); + exit(1); + } + printf("Enter content for the file\n"); + gets(str); + while(strcmp(str, "stop") !=0){ + fputs(str,fp); + gets(str); + } + } + fclose(fp); +} diff --git a/Chapter18/filesolved.c b/Chapter18/filesolved.c new file mode 100644 index 0000000..62e7a83 --- /dev/null +++ b/Chapter18/filesolved.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +#define BUFFSIZE 255 + +void main (int argc, char* argv[]) +{ + int ifp; + FILE *fp; + char str[BUFFSIZE]; + if (symlink("file2.txt","file1.txt") != 0) { + perror("symlink() error"); + unlink("file2.txt"); + exit(1); + } + else + { + unlink("file1.txt"); + ifp = open("file1.txt", O_WRONLY | O_CREAT | O_EXCL, 0600); + if (ifp == -1) { + perror("An error occurred in creating the file\n"); + exit(1); + } + fp = fdopen(ifp, "w"); + if (fp == NULL) { + perror ("Could not be linked to the stream\n"); + exit(1); + } + printf("Enter content for the file\n"); + gets(str); + while(strcmp(str, "stop") !=0){ + fputs(str,fp); + gets(str); + } + } + fclose(fp); +} diff --git a/Chapter18/getsproblem.c b/Chapter18/getsproblem.c new file mode 100644 index 0000000..4946803 --- /dev/null +++ b/Chapter18/getsproblem.c @@ -0,0 +1,26 @@ +#include + +struct users +{ + char name[10]; + int orderid; +}; + +int main(void) +{ + struct users user1, user2; + printf("Enter order number "); + scanf("%d", &user1.orderid); + fpurge(stdin); + printf ("Enter first user name "); + gets(user1.name); + printf("Enter order number "); + scanf("%d", &user2.orderid); + fpurge(stdin); + printf ("Enter second user name "); + gets(user2.name); + printf("Information of first user - Name %s, Order number %d\n", user1.name, user1.orderid); + printf("Information of second user - Name %s, Order number %d\n", user2.name, user2.orderid); +} + + diff --git a/Chapter18/getssolved.c b/Chapter18/getssolved.c new file mode 100644 index 0000000..9fded7a --- /dev/null +++ b/Chapter18/getssolved.c @@ -0,0 +1,26 @@ +#include +#include +#include + +struct users +{ + char name[10]; + int orderid; +}; + +int main(void) +{ + struct users user1; + int n; + printf("Enter order number "); + scanf("%d", &user1.orderid); + fpurge(stdin); + printf ("Enter user name "); + fgets(user1.name, sizeof(user1.name), stdin); + n = strlen(user1.name)-1; + if(user1.name[n] == '\n') + user1.name[n] = '\0'; + printf("Information of the user is - Name %s, Order number %d\n", user1.name, user1.orderid); +} + + diff --git a/Chapter18/sprintfproblem.c b/Chapter18/sprintfproblem.c new file mode 100644 index 0000000..c22b865 --- /dev/null +++ b/Chapter18/sprintfproblem.c @@ -0,0 +1,17 @@ +#include + +struct users +{ + char name[10]; + int orderid; +}; + +int main(void) +{ + struct users user1; + user1.orderid=101; + sprintf(user1.name, "%s","bintuharwani"); + printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); +} + + diff --git a/Chapter18/sprintfsolved.c b/Chapter18/sprintfsolved.c new file mode 100644 index 0000000..528cf1b --- /dev/null +++ b/Chapter18/sprintfsolved.c @@ -0,0 +1,18 @@ +#include + +struct users +{ + char name[10]; + int orderid; +}; + +int main(void) +{ + struct users user1; + user1.orderid=101; + snprintf(user1.name, sizeof(user1.name), "%s","bintuharwani"); + printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); +} + + + diff --git a/Chapter18/strcpyproblem.c b/Chapter18/strcpyproblem.c new file mode 100644 index 0000000..cfe0b23 --- /dev/null +++ b/Chapter18/strcpyproblem.c @@ -0,0 +1,19 @@ +#include +#include + +struct users +{ + char name[10]; + int orderid; +}; + +int main(void) +{ + struct users user1; + char userid[]="administrator"; + user1.orderid=101; + strcpy(user1.name, userid); + printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); +} + + diff --git a/Chapter18/strcpysolved.c b/Chapter18/strcpysolved.c new file mode 100644 index 0000000..0c8c8ee --- /dev/null +++ b/Chapter18/strcpysolved.c @@ -0,0 +1,23 @@ +#include +#include + +struct users +{ + char name[10]; + int orderid; +}; + +int main(void) +{ + int strsize; + struct users user1; + char userid[]="administrator"; + user1.orderid=101; + strsize=sizeof(user1.name); + strncpy(user1.name, userid,strsize); + if (user1.name[strsize-1] != '\0') + user1.name[strsize-1] = '\0'; + printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); +} + + diff --git a/README.md b/README.md index e79ffb3..6aff7cb 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,27 @@ -# C Programming Cookbook -C Programming Cookbook -This is the code repository for [C Programming Cookbook](https://www.packtpub.com/application-development/c-programming-cookbook-0?utm_source=github&utm_medium=repository&utm_campaign=9781789617450), published by Packt. -**Over 40 recipes exploring data structures, pointers, interprocess communication, and database in C** +# Practical C Programming + +Practical C Programming + +This is the code repository for [Practical C Programming](https://www.packtpub.com/in/programming/c-programming-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781838641108), published by Packt. + +**Solutions for modern C developers to create efficient and well-structured programs** ## What is this book about? -C is a high-level language popular among developers. It enables you to write drivers for different devices, access machine-level hardware, apply dynamic memory allocation, and much more. With self-contained tutorials, known as recipes, this book will guide you in dealing with C and its idiosyncrasies and help you benefit from its latest features. +Practical C Programming will teach you how to deal with C and its idiosyncrasies, and benefit from its new features, through bite-sized recipes. Each recipe in the book addresses a specific problem through a discussion that reveals and explains the solution to the recipe. This book will teach all you need to know to become a better C programmer. This book covers the following exciting features: -* Manipulate single and multi-dimensional arrays -* Perform complex operations on strings -* Understand how to use pointers and memory optimally -* Discover how to use arrays, functions, and strings to make large applications -* Use arrays, functions, and strings for making large applications -Implement multitasking using threads and communicate between processes -Apply networking concepts in communication -Store simple text into files and store data into a database +* Discover how to use arrays, functions, and strings to create large apps +* Perform preprocessing and conditional compilation for efficient programming +* Understand how to use pointers and memory optimally +* Use general-purpose utilities and improve the performance of the code +* Implement multitasking using threads and process synchronization +* Explore low-level programming and inline assembly language +* Understand how to use graphics for animation +* Implement security while developing C programs +* Establish communication between two or more processes using different techniques If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1789617456) today! @@ -45,23 +49,23 @@ for(i=0;i<2;i++) **Following is what you need for this book:** If you’re a programmer with basic experience in C and want to leverage its features through modern programming practices, then this book is for you. -With the following software and hardware list you can run all code files present in the book (Chapter 1-8). +With the following software and hardware list you can run all code files present in the book (Chapter 1-18). ### Software and Hardware List | Chapter | Software required | OS required | | -------- | ------------------------------------ | ----------------------------------- | -| 1-7 | cygwin | Windows 8 or above | -| 8 | MySQL Community Server | Windows 8 or above | +| 1-18 | cygwin | Windows 8 or above | +| 13 | MySQL Community Server | Windows 8 or above | -We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://www.packtpub.com/sites/default/files/downloads/9781789617450_ColorImages.pdf?). +We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://static.packt-cdn.com/downloads/9781838641108_ColorImages.pdf). ### Related products -* Hands-On Design Patterns with C++ [[Packt]](https://www.packtpub.com/application-development/hands-design-patterns-c?utm_source=github&utm_medium=repository&utm_campaign=9781788832564) [[Amazon]](https://www.amazon.com/dp/1788832566) +* Hands-On Network Programming with C [[Packt]](https://www.packtpub.com/networking-and-servers/hands-network-programming-c?utm_source=github&utm_medium=repository&utm_campaign=9781789349863) [[Amazon]](https://www.amazon.com/dp/1789349869) -* Hands-On System Programming with C++ [[Packt]](https://www.packtpub.com/application-development/hands-system-programming-c?utm_source=github&utm_medium=repository&utm_campaign=9781789137880) [[Amazon]](https://www.amazon.com/dp/1789137888) +* Extreme C [[Packt]](https://www.packtpub.com/programming/extreme-c?utm_source=github&utm_medium=repository&utm_campaign=9781789343625) [[Amazon]](https://www.amazon.com/dp/1789343623/) ## Get to Know the Author -**B.M. Harwani** -is the founder of Microchip Computer Education based in Ajmer, India that provides computer literacy in programming and web development to learners of all ages. He further helps the community by sharing his knowledge and expertise of over 20 years of teaching through writing books. His recent publications include jQuery Recipes published by Apress, Introduction to Python Programming and Developing GUI Applications with PyQT published by Cengage Learning, The Android Tablet Developer's Cookbook published by Addison-Wesley Professional, UNIX & Shell Programming published by Oxford University Press, and Qt5 Python GUI Programming Cookbook published by Packt. +**B. M. Harwani** +is the founder of Microchip Computer Education, based in Ajmer, India, which provides computer literacy in programming and web development to learners of all ages. He further helps the community by sharing the knowledge and expertise he's gained over 20 years of teaching by writing books. His recent publications include jQuery Recipes, published by Apress, Introduction to Python Programming and Developing GUI Applications with PyQT, published by Cengage Learning, The Android Tablet Developer's Cookbook, published by Addison-Wesley Professional, UNIX and Shell Programming, published by Oxford University Press, and Qt5 Python GUI Programming Cookbook, published by Packt. ## Other books by the authors [Qt5 Python GUI Programming Cookbook](https://www.packtpub.com/application-development/qt5-python-gui-programming-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781788831000) @@ -70,3 +74,7 @@ is the founder of Microchip Computer Education based in Ajmer, India that provid [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. +### Download a free PDF + + If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
+

https://packt.link/free-ebook/9781786467300

\ No newline at end of file