├── Appendix A.pdf ├── Appendix B.pdf ├── Appendix C.pdf ├── Appendix D.pdf ├── Appendix E.pdf ├── AppendixA ├── createrandomfile.c ├── createtextfile.c ├── decryptfile.c ├── readrandomfile.c └── readtextfile.c ├── AppendixB ├── mergesort.c ├── radixsort.c ├── selectionsort.c └── shellsort.c ├── AppendixC ├── circularquearray.c ├── dequeuearray.c └── queuearray.c ├── Chapter01 ├── commoninarray.c ├── deletefromarray.c ├── differencearray.c ├── identitymatrix.c ├── insertintoarray.c ├── matrixmulti.c ├── mergetwosortedarrays.c ├── sparsematrix.c └── uniqueelements.c ├── Chapter02 ├── convertvowels.c ├── countofeach.c ├── countvowelsandcons.c ├── palendrome.c └── repetitive.c ├── Chapter03 ├── armstrong.c ├── binaryintohexa.c ├── findpalindrome.c ├── gcd.c └── returnarray.c ├── Chapter04 ├── assertdemoprog.c ├── assertprog.c ├── compileassert.c ├── condcompile.c └── preconcat.c ├── Chapter05 ├── largestinarray.c ├── pointertostruct.c ├── reversestring.c ├── sortlinkedlist.c └── transposemat.c ├── Chapter06 ├── convertcase.c ├── countvowels.c ├── encryptfile.c ├── readrandominreverse.c └── replaceword.c ├── Chapter07 ├── avoiddeadlockst.c ├── createthread.c ├── deadlockstate.c ├── twothreads.c └── twothreadsmutex.c ├── Chapter08 ├── clientprog.c ├── messageqrecv.c ├── messageqsend.c ├── pipedemo.c ├── readfifo.c ├── readmemory.c ├── readwritepipe.c ├── serverprog.c ├── udpc.c ├── udps.c ├── writefifo.c └── writememory.c ├── Chapter09 ├── binarysearch.c ├── bubblesort.c ├── heapsort.c ├── insertionsort.c └── quicksort.c ├── Chapter10 ├── adjlistdirect.c ├── adjmatdirect.c ├── adjmatundirect.c ├── breadthfirsttrav.c ├── depthfirsttrav.c ├── kruskal.c └── prims.c ├── Chapter11 ├── binarysearchtree.c ├── circularlinkedlist.c ├── doublylinkedlist.c ├── postordernonrec.c └── stacklinkedlist.c ├── Chapter12 ├── ballanim.c ├── opengldrawbar.c ├── opengldrawshapes.c ├── opengldrawshapes2.c └── openglmouseclick.c ├── Chapter13 ├── adduser.c ├── deleteuser.c ├── mysql1.c ├── searchuser.c └── updateuser.c ├── Chapter14 ├── atexistprog1.c ├── atexistprog2.c ├── dynamicmem.c ├── signalhandling.c └── timecalc.c ├── Chapter15 ├── fastinp.c ├── loopunrolling.c └── tourvehicle.c ├── Chapter16 ├── asmdivide.c ├── binintodec.c ├── convertintobin.c ├── decintobin.c └── multiasm.c ├── Chapter17 ├── ArduinoLedBlink │ └── ArduinoLedBlink.ino ├── ArduinoTakinginput │ └── ArduinoTakinginput.ino ├── SensorApp │ └── SensorApp.ino ├── blinkingLed.c └── showcounter.c ├── Chapter18 ├── fileproblem.c ├── filesolved.c ├── getsproblem.c ├── getssolved.c ├── sprintfproblem.c ├── sprintfsolved.c ├── strcpyproblem.c └── strcpysolved.c ├── LICENSE └── README.md /Appendix A.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Practical-C-Programming/e08b04132fdf60a8ba5aab9207be4361d3d61b31/Appendix A.pdf -------------------------------------------------------------------------------- /Appendix B.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Practical-C-Programming/e08b04132fdf60a8ba5aab9207be4361d3d61b31/Appendix B.pdf -------------------------------------------------------------------------------- /Appendix C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Practical-C-Programming/e08b04132fdf60a8ba5aab9207be4361d3d61b31/Appendix C.pdf -------------------------------------------------------------------------------- /Appendix D.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Practical-C-Programming/e08b04132fdf60a8ba5aab9207be4361d3d61b31/Appendix D.pdf -------------------------------------------------------------------------------- /Appendix E.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Practical-C-Programming/e08b04132fdf60a8ba5aab9207be4361d3d61b31/Appendix E.pdf -------------------------------------------------------------------------------- /AppendixA/createrandomfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct data{ 6 | char str[ 255 ]; 7 | }; 8 | 9 | void main (int argc, char* argv[]) 10 | { 11 | FILE *fp; 12 | struct data line; 13 | fp = fopen (argv[1], "wb"); 14 | if (fp == NULL) { 15 | perror ("An error occurred in creating the file\n"); 16 | exit(1); 17 | } 18 | printf("Enter file content:\n"); 19 | gets(line.str); 20 | while(strcmp(line.str, "stop") !=0){ 21 | fwrite( &line, sizeof(struct data), 1, fp ); 22 | gets(line.str); 23 | } 24 | fclose(fp); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /AppendixA/createtextfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main (int argc, char* argv[]) 6 | { 7 | char str[255]; 8 | FILE *fp; 9 | 10 | fp = fopen (argv[1], "w"); 11 | if (fp == NULL) { 12 | perror ("An error occurred in creating the file\n"); 13 | exit(1); 14 | } 15 | printf("Enter content for the file\n"); 16 | gets(str); 17 | while(strcmp(str, "stop") !=0){ 18 | fputs(str,fp); 19 | gets(str); 20 | } 21 | fclose(fp); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /AppendixA/decryptfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define BUFFSIZE 255 6 | 7 | void main (int argc, char* argv[]) 8 | { 9 | FILE *fp,*fq; 10 | int i,n; 11 | char buffer[BUFFSIZE]; 12 | 13 | fp = fopen (argv [1],"r"); 14 | if (fp == NULL) { 15 | printf("%s file does not exist\n", argv[1]); 16 | exit(1); 17 | } 18 | 19 | fq = fopen (argv[2], "w"); 20 | if (fq == NULL) { 21 | perror ("An error occurred in creating the file\n"); 22 | exit(1); 23 | } 24 | while (!feof(fp)) 25 | { 26 | fgets(buffer, BUFFSIZE, fp); 27 | n=strlen(buffer); 28 | for(i=0;i 2 | #include 3 | #include 4 | 5 | struct data{ 6 | char str[ 255 ]; 7 | }; 8 | 9 | void main (int argc, char* argv[]) 10 | { 11 | 12 | FILE *fp; 13 | struct data line; 14 | int n,nol,i; 15 | fp = fopen (argv[1], "rb"); 16 | if (fp == NULL) { 17 | perror ("An error occurred in opening the file\n"); 18 | exit(1); 19 | } 20 | fseek(fp, 0L, SEEK_END); /* go to end of file */ 21 | n = ftell(fp); 22 | nol=n/sizeof(struct data); 23 | rewind(fp); 24 | printf("The content in file is :\n"); 25 | for (i=1;i<=nol;i++) 26 | { 27 | fread(&line,sizeof(struct data),1,fp); 28 | puts(line.str); 29 | } 30 | fclose(fp); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /AppendixA/readtextfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define BUFFSIZE 255 5 | 6 | void main (int argc, char* argv[]) 7 | { 8 | FILE *fp; 9 | char buffer[BUFFSIZE]; 10 | 11 | fp = fopen (argv [1],"r"); 12 | if (fp == NULL) { 13 | printf("%s file does not exist\n", argv[1]); 14 | exit(1); 15 | } 16 | while (!feof(fp)) 17 | { 18 | fgets(buffer, BUFFSIZE, fp); 19 | puts(buffer); 20 | } 21 | fclose(fp); 22 | } 23 | -------------------------------------------------------------------------------- /AppendixB/mergesort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 20 3 | 4 | void merge(int arr[], int fsi, int fei, int ssi, int sei); 5 | void mergesort(int arr[],int si, int ei); 6 | 7 | int main() 8 | { 9 | int arr[max]; 10 | int si,i,len; 11 | si=0; 12 | printf("How many numerical to sort? "); 13 | scanf("%d",&len); 14 | printf("Enter %d numericals:\n", len); 15 | for(i=0;i<=len-1;i++) 16 | scanf("%d",&arr[i]); 17 | printf("\nOriginal array is \n"); 18 | for(i=0;isi) 32 | { 33 | mid=(si+ei)/2; 34 | mergesort(arr,si,mid); 35 | mergesort(arr,mid+1,ei); 36 | merge(arr,si,mid,mid+1,ei); 37 | } 38 | } 39 | 40 | void merge(int arr[], int fsi, int fei, int ssi, int sei) 41 | { 42 | int mgd[max],x,fh,sh,i; 43 | fh=fsi; 44 | sh=ssi; 45 | x=0; 46 | while(fh<=fei && sh <=sei) 47 | { 48 | if(arr[fh] 2 | #include 3 | #define max 20 4 | int main() 5 | { 6 | int arr[max],k,e,len,i,j,digit,x,numbdig; 7 | int temp[10][max]; 8 | printf("How many numbers to sort ? "); 9 | scanf("%d",&len); 10 | printf("How many number of digits in each numerical ? "); 11 | scanf("%d",&numbdig); 12 | printf("Enter %d values\n", len); 13 | for(i=0;i 2 | #define max 20 3 | 4 | int main() 5 | { 6 | int arr[max],i,j,value,len,index,temp; 7 | printf("How many numbers to sort? "); 8 | scanf("%d",&len); 9 | printf("Enter %d values:\n",len); 10 | for(i=0;iarr[j]) 19 | { 20 | value=arr[j]; 21 | index=j; 22 | } 23 | } 24 | temp=arr[i]; 25 | arr[i]=arr[index]; 26 | arr[index]=temp; 27 | } 28 | printf("\nThe ascending order of the element entered is:\n"); 29 | for(i=0;i 2 | 3 | #define max 20 4 | 5 | int main() 6 | { 7 | int len,i,arr[max],incr,j,temp; 8 | 9 | printf("How many values to be sorted ?"); 10 | scanf("%d",&len); 11 | printf("Enter %d values\n", len); 12 | for(i=0;i=1) 20 | { 21 | for(i=0;iarr[j]) 26 | { 27 | temp=arr[i]; 28 | arr[i]=arr[j]; 29 | arr[j]=temp; 30 | } 31 | } 32 | } 33 | printf("\nThe array when increment is %d\n",incr); 34 | for(i=0;i 2 | #define max 5 3 | int cque[max]; 4 | 5 | void addq(int *Front,int *r,int h); 6 | int delq(int *Front,int *Rear); 7 | int isQueueEmpty(int *Front); 8 | 9 | int main() 10 | { 11 | int rear,front,n=0,fromq,val; 12 | rear=front=-1; 13 | while(n!=3) 14 | { 15 | printf("\n1. Adding an element into the circular queue\n"); 16 | printf("2. Deleting an element from the circular queue\n"); 17 | printf("3. Quit\n"); 18 | printf("Enter your choice 1/2/3: "); 19 | scanf("%d",&n); 20 | switch(n) 21 | { 22 | case 1: printf("Enter the value to add to the circular queue: "); 23 | scanf("%d",&val); 24 | addq(&front,&rear,val); 25 | break; 26 | case 2: if(!isQueueEmpty(&front)) 27 | { 28 | fromq=delq(&front,&rear); 29 | printf("The value removed from the circular queue is %d\n",fromq); 30 | } 31 | else 32 | printf("The circular queue is empty\n"); 33 | break; 34 | } 35 | } 36 | return 0; 37 | } 38 | 39 | void addq(int *Front,int *Rear,int Val) 40 | { 41 | int oldR; 42 | if(*Rear >=max-1) 43 | { 44 | oldR=*Rear; 45 | *Rear=(*Rear+1)%5; 46 | if(*Rear<*Front) 47 | { 48 | cque[*Rear]=Val; 49 | printf("Value %d is added in circular queue\n", Val); 50 | } 51 | else 52 | { 53 | *Rear=oldR; 54 | printf("Sorry the circular queue is full\n "); 55 | } 56 | } 57 | else 58 | { 59 | (*Rear)++; 60 | if(*Rear !=*Front) 61 | { 62 | cque[*Rear]=Val; 63 | printf("Value %d is added in circular queue\n",Val); 64 | } 65 | else 66 | { 67 | (*Rear)--; 68 | printf("Sorry, the circular queue is full\n"); 69 | } 70 | } 71 | if(*Front==-1) 72 | *Front=*Rear; 73 | } 74 | 75 | int isQueueEmpty(int *Front) 76 | { 77 | if(*Front==-1) 78 | return (1); 79 | else 80 | return (0); 81 | } 82 | 83 | int delq(int *Front,int *Rear) 84 | { 85 | int val=0; 86 | if(*Front==*Rear) 87 | { 88 | val=cque[*Front]; 89 | *Front=*Rear=-1; 90 | } 91 | else 92 | { 93 | if(*Front >=max) 94 | { 95 | *Front=*Front%5; 96 | if (*Front<*Rear) 97 | { 98 | val=cque[*Front]; 99 | (*Front)++; 100 | } 101 | else 102 | { 103 | if(*Front==*Rear) 104 | { 105 | val=cque[*Front]; 106 | *Front=*Rear=-1; 107 | } 108 | } 109 | } 110 | else 111 | { 112 | val=cque[*Front]; 113 | (*Front)++; 114 | } 115 | } 116 | return(val); 117 | } -------------------------------------------------------------------------------- /AppendixC/dequeuearray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 5 3 | 4 | int dq[max]; 5 | 6 | 7 | void addInFront(int *Front,int *r,int h); 8 | void addInRear(int *Front,int *r,int h); 9 | int delFFront(int *Front,int *Rear); 10 | int delFRear(int *Front,int *Rear); 11 | int isDQueueEmpty(int *Front); 12 | int isDQueueFull(int *Front, int *Rear); 13 | 14 | 15 | int main() 16 | { 17 | int rear,front,n=0,fromdq,val; 18 | rear=-1; 19 | front=-1; 20 | 21 | while(n !=5) 22 | { 23 | printf("\n1. Adding element in front into the dequeue\n"); 24 | printf("2. Adding element at rear into the dequeue\n"); 25 | printf("3. Deleting an element from the front in dequeue\n"); 26 | printf("4. Deleting an element from the rear in dequeue\n"); 27 | printf("5. Quit\n"); 28 | printf("Enter your choice 1/2/3/4/5: "); 29 | scanf("%d",&n); 30 | 31 | 32 | switch(n) 33 | { 34 | case 1: printf("Enter the value to be added to the front in dequeue: "); 35 | scanf("%d",&val); 36 | addInFront(&front,&rear,val); 37 | break; 38 | case 2: printf("Enter the value to be added at the rear in dequeue: "); 39 | scanf("%d",&val); 40 | addInRear(&front,&rear,val); 41 | break; 42 | case 3: if(!isDQueueEmpty(&front)) 43 | { 44 | fromdq=delFFront(&front,&rear); 45 | printf("The value removed from the front in dequeue is %d\n",fromdq); 46 | } 47 | else 48 | printf("The dequeue is empty\n"); 49 | break; 50 | case 4: if(!isDQueueEmpty(&front)) 51 | { 52 | fromdq=delFRear(&front,&rear); 53 | printf("The value removed from the rear in dequeue is %d\n",fromdq); 54 | } 55 | else 56 | printf("The dequeue is empty\n"); 57 | break; 58 | } 59 | } 60 | return 0; 61 | } 62 | 63 | 64 | int isDQueueEmpty(int *Front) 65 | { 66 | if(*Front==-1) 67 | return (1); 68 | else 69 | return (0); 70 | } 71 | 72 | int isDQueueFull(int *Front, int *Rear) 73 | { 74 | if((*Front==0) && (*Rear==max-1)) 75 | return (1); 76 | if(*Front==(*Rear+1)) 77 | return (1); 78 | return (0); 79 | } 80 | 81 | 82 | void addInRear(int *Front,int *Rear, int Val) 83 | { 84 | int oldR; 85 | if(*Rear ==max-1) 86 | { 87 | oldR=*Rear; 88 | *Rear=(*Rear+1)%5; 89 | if(*Rear < *Front) 90 | { 91 | dq[*Rear]=Val; 92 | printf("Value %d is added in dequeue\n", Val); 93 | } 94 | else 95 | { 96 | *Rear=oldR; 97 | printf("Sorry the dequeue is full\n "); 98 | } 99 | } 100 | else 101 | { 102 | (*Rear)++; 103 | if(*Rear !=*Front) 104 | { 105 | dq[*Rear]=Val; 106 | printf("Value %d is added in dequeue\n",Val); 107 | } 108 | else 109 | { 110 | (*Rear)--; 111 | printf("Sorry, the dequeue is full\n"); 112 | } 113 | } 114 | if(*Front==-1) 115 | *Front=*Rear; 116 | } 117 | 118 | 119 | void addInFront(int *Front,int *Rear,int Val) 120 | { 121 | if (*Front == -1) 122 | { 123 | *Front = 0; 124 | *Rear = 0; 125 | } 126 | else 127 | { 128 | if (*Front ==0) 129 | *Front=max-1; 130 | else 131 | *Front=(*Front) -1; 132 | } 133 | dq[*Front]=Val; 134 | } 135 | 136 | int delFFront(int *Front,int *Rear) 137 | { 138 | int val=0; 139 | if(*Front==*Rear) 140 | { 141 | val=dq[*Front]; 142 | *Front=*Rear=-1; 143 | } 144 | else 145 | { 146 | if(*Front >=max) 147 | { 148 | *Front=*Front%5; 149 | if (*Front<*Rear) 150 | { 151 | val=dq[*Front]; 152 | (*Front)++; 153 | } 154 | else 155 | { 156 | if(*Front==*Rear) 157 | { 158 | val=dq[*Front]; 159 | *Front=*Rear=-1; 160 | } 161 | } 162 | } 163 | else 164 | { 165 | val=dq[*Front]; 166 | (*Front)++; 167 | } 168 | } 169 | return(val); 170 | } 171 | 172 | int delFRear(int *Front,int *Rear) 173 | { 174 | int val=0; 175 | if(*Front==*Rear) 176 | { 177 | val=dq[*Front]; 178 | *Front=*Rear=-1; 179 | } 180 | else 181 | { 182 | if (*Rear == 0) 183 | { 184 | val=dq[*Rear]; 185 | *Rear=max-1; 186 | } 187 | else 188 | { 189 | val=dq[*Rear]; 190 | *Rear = *Rear-1; 191 | } 192 | } 193 | return(val); 194 | } 195 | 196 | 197 | -------------------------------------------------------------------------------- /AppendixC/queuearray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 5 3 | int front=-1,rear=-1; 4 | 5 | void addq(int Que[], int Val); 6 | int delq(int Que[]); 7 | int IsQueueEmpty(); 8 | 9 | int main() 10 | { 11 | int que[max]; 12 | int val,n=0; 13 | while(n!=3) 14 | { 15 | printf("\n1. Adding an element into the queue\n"); 16 | printf("2. Deleting an element from the queue\n"); 17 | printf("3. Quit\n"); 18 | printf("Enter your choice 1/2/3: "); 19 | scanf("%d",&n); 20 | switch(n) 21 | { 22 | case 1: if(rear >=max-1) 23 | printf("Sorry the queue is full\n"); 24 | else 25 | { 26 | printf("Enter the value to add to queue: "); 27 | scanf("%d",&val); 28 | addq(que,val); 29 | printf("Value %d is added to queue\n", val); 30 | } 31 | break; 32 | case 2: if(!IsQueueEmpty()) 33 | { 34 | 35 | val=delq(que); 36 | printf("Value removed from queue %d\n",val); 37 | } 38 | else 39 | printf("Sorry, the queue is empty\n"); 40 | break; 41 | } 42 | } 43 | return 0; 44 | } 45 | 46 | void addq(int Que[], int Val) 47 | { 48 | rear++; 49 | Que[rear]=Val; 50 | if(front==-1) 51 | front=rear; 52 | } 53 | 54 | int IsQueueEmpty() 55 | { 56 | if(front==-1) 57 | return (1); 58 | else 59 | return (0); 60 | 61 | 62 | } 63 | 64 | int delq(int Que[]) 65 | { 66 | int val; 67 | if(front==rear) 68 | { 69 | val=Que[front]; 70 | front=-1; 71 | rear=-1; 72 | } 73 | else 74 | { 75 | val=Que[front]; 76 | front++; 77 | } 78 | return(val); 79 | } 80 | -------------------------------------------------------------------------------- /Chapter01/commoninarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 100 3 | 4 | int ifexists(int z[], int u, int v) 5 | { 6 | int i; 7 | if (u==0) return 0; 8 | for (i=0; i<=u;i++) 9 | if (z[i]==v) return (1); 10 | return (0); 11 | } 12 | 13 | 14 | void main() 15 | { 16 | int p[max], q[max], r[max]; 17 | int m,n; 18 | int i,j,k; 19 | k=0; 20 | printf("Enter length of first array:"); 21 | scanf("%d",&m); 22 | printf("Enter %d elements of first array\n",m); 23 | for(i=0;i0) 46 | { 47 | printf("\nThe common elements in the two array are:\n"); 48 | 49 | for(i = 0;i 4 | void main() 5 | { 6 | int p[100],i,n,a; 7 | printf("Enter the length of array: "); 8 | scanf("%d",&n); 9 | printf("Enter %d elements of array \n",n); 10 | for(i=0;i<=n-1;i++) 11 | scanf("%d",&p[i]); 12 | printf("\nThe array is:\n"); 13 | for(i=0;i<=n-1;i++) 14 | printf("%d\n",p[i]); 15 | printf("Enter the position/ location to delete: "); 16 | scanf("%d",&a); 17 | a--; 18 | for(i=a;i<=n-2;i++) 19 | { 20 | p[i]=p[i+1]; 21 | /* All values from bottom of the array are shifted up till the location of the element to be deleted */ 22 | } 23 | p[n-1]=0; 24 | /* The vacant position created at the bottom of the array is set to 0 */ 25 | printf("Array after deleting the element is\n"); 26 | for(i=0;i<= n-2;i++) 27 | printf("%d\n",p[i]); 28 | } -------------------------------------------------------------------------------- /Chapter01/differencearray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 100 3 | 4 | int ifexists(int z[], int u, int v) 5 | { 6 | int i; 7 | if (u==0) return 0; 8 | for (i=0; i<=u;i++) 9 | if (z[i]==v) return (1); 10 | return (0); 11 | } 12 | 13 | void main() 14 | { 15 | int p[max], q[max], r[max]; 16 | int m,n; 17 | int i,j,k; 18 | k=0; 19 | printf("Enter length of first array:"); 20 | scanf("%d",&m); 21 | printf("Enter %d elements of first array\n",m); 22 | for(i=0;i 2 | #define max 100 3 | /* All the elements of the principal diagonal of the Identity matrix are ones and rest all are zero elements */ 4 | void main () 5 | { 6 | static int arr[max][max]; 7 | int i,j,r,c, bool; 8 | printf("How many rows and columns are in this matrix ? "); 9 | scanf("%d %d", &r, &c); 10 | if (r !=c) 11 | { 12 | printf("An identity matrix is a square matrix\n"); 13 | printf("Because this matrix is not a square matrix, so it is not an identity matrix\n"); 14 | } 15 | else 16 | { 17 | printf("Enter elements in the matrix :\n"); 18 | for(i=0;i 2 | #define max 100 3 | void main() 4 | { 5 | int p[max], n,i,k,j; 6 | 7 | printf("Enter length of array:"); 8 | scanf("%d",&n); 9 | printf("Enter %d elements of array\n",n); 10 | for(i=0;i<=n-1;i++ ) 11 | scanf("%d",&p[i]); 12 | printf("\nThe array is:\n"); 13 | for(i = 0;i<=n-1;i++) 14 | printf("%d\n",p[i]); 15 | printf("\nEnter position where to insert:"); 16 | scanf("%d",&k); 17 | k--; 18 | 19 | /*The position is always one value higher than subscript so it is decremented by one*/ 20 | for(j=n-1;j>=k;j--) 21 | p[j+1]=p[j]; 22 | 23 | /* Shifting all the elements of the array one position down from the location where to insert */ 24 | printf("\nEnter the value to insert:"); 25 | scanf("%d",&p[k]); 26 | printf("\nArray after insertion of element: \n"); 27 | for(i=0;i<=n;i++) 28 | printf("%d\n",p[i]); 29 | } 30 | -------------------------------------------------------------------------------- /Chapter01/matrixmulti.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int matA[2][3], matB[3][4], matR[2][4]; 6 | int i,j,k; 7 | 8 | printf("Enter elements of first matrix of order 2 x 3 \n"); 9 | for(i=0;i<2;i++) 10 | { 11 | for(j=0;j<3;j++) 12 | { 13 | scanf("%d",&matA[i][j]); 14 | } 15 | } 16 | printf("Enter elements of second matrix of order 3 x 4 \n"); 17 | for(i=0;i<3;i++) 18 | { 19 | for(j=0;j<4;j++) 20 | { 21 | scanf("%d",&matB[i][j]); 22 | } 23 | } 24 | for(i=0;i<2;i++) 25 | { 26 | for(j=0;j<4;j++) 27 | { 28 | matR[i][j]=0; 29 | for(k=0;k<3;k++) 30 | { 31 | matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j]; 32 | } 33 | } 34 | } 35 | printf("\nFirst Matrix is \n"); 36 | for(i=0;i<2;i++) 37 | { 38 | for(j=0;j<3;j++) 39 | { 40 | printf("%d\t",matA[i][j]); 41 | } 42 | printf("\n"); 43 | } 44 | printf("\nSecond Matrix is \n"); 45 | for(i=0;i<3;i++) 46 | { 47 | for(j=0;j<4;j++) 48 | { 49 | printf("%d\t",matB[i][j]); 50 | } 51 | printf("\n"); 52 | } 53 | printf("\nMatrix multiplication is \n"); 54 | for(i=0;i<2;i++) 55 | { 56 | for(j=0;j<4;j++) 57 | { 58 | printf("%d\t",matR[i][j]); 59 | } 60 | printf("\n"); 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Chapter01/mergetwosortedarrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 100 3 | 4 | void main() 5 | { 6 | int p[max], q[max], r[max]; 7 | int m,n; 8 | int i,j,k; 9 | printf("Enter length of first array:"); 10 | scanf("%d",&m); 11 | printf("Enter %d elements of first array in sorted order \n",m); 12 | for(i=0;i 2 | #define max 100 3 | /*A sparse martix is matrix which has more zero elements than nonzero elements */ 4 | void main () 5 | { 6 | static int arr[max][max]; 7 | int i,j,r,c; 8 | int ctr=0; 9 | printf("How many rows and columns are in this matrix ? "); 10 | scanf("%d %d", &r, &c); 11 | printf("Enter elements in the matrix :\n"); 12 | for(i=0;i((r*c)/2)) 22 | printf ("The given matrix is a sparse matrix. \n"); 23 | else 24 | printf ("The given matrix is not a sparse matrix.\n"); 25 | printf ("There are %d number of zeros in the matrix.\n\n",ctr); 26 | } -------------------------------------------------------------------------------- /Chapter01/uniqueelements.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 100 3 | 4 | int ifexists(int z[], int u, int v) 5 | { 6 | int i; 7 | for (i=0; i 2 | void main() 3 | { 4 | char str[255]; 5 | int i; 6 | printf("Enter a sentence: "); 7 | gets(str); 8 | i=0; 9 | while(str[i]!='\0') 10 | { 11 | if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u') 12 | str[i]=str[i]-32; 13 | i++; 14 | } 15 | printf("The sentence after converting vowels into upper case is:\n"); 16 | puts(str); 17 | } -------------------------------------------------------------------------------- /Chapter02/countofeach.c: -------------------------------------------------------------------------------- 1 | #include 2 | # include 3 | 4 | int ifexists(char u, char p[], int v, int q[]) 5 | { 6 | int i; 7 | for (i=0; i<=v;i++) 8 | { 9 | if (p[i]==u) 10 | { 11 | q[i]++; 12 | return (1); 13 | } 14 | } 15 | if(i>v) return (0); 16 | } 17 | 18 | void main() 19 | { 20 | char str[80],chr[80]; 21 | int n,i,x,count[80]; 22 | printf("Enter a string: "); 23 | scanf("%s",str); 24 | n=strlen(str); 25 | chr[0]=str[0]; 26 | count[0]=1; 27 | x=0; 28 | for(i=1;i < n; i++) 29 | { 30 | if(!ifexists(str[i], chr, x, count)) 31 | { 32 | x++; 33 | chr[x]=str[i]; 34 | count[x]=1; 35 | } 36 | } 37 | printf("The count of each character in the string %s is \n", str); 38 | for (i=0;i<=x;i++) 39 | printf("%c\t%d\n",chr[i],count[i]); 40 | } 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Chapter02/countvowelsandcons.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | char str[255]; 5 | int ctrV,ctrC,i; 6 | printf("Enter a sentence: "); 7 | gets(str); 8 | ctrV=ctrC=i=0; 9 | while(str[i]!='\0') 10 | { 11 | if((str[i] >=65 && str[i]<=90) || (str[i] >=97 && str[i]<=122)) 12 | { 13 | if(str[i]=='A' ||str[i]=='E' ||str[i]=='I' ||str[i]=='O' ||str[i]=='U' ||str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u') 14 | ctrV++; 15 | else 16 | ctrC++; 17 | } 18 | i++; 19 | } 20 | printf("Number of vowels are : %d\nNumber of consonants are : %d\n",ctrV,ctrC); 21 | } -------------------------------------------------------------------------------- /Chapter02/palendrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | # include 3 | 4 | void main() 5 | { 6 | char str[80],rev[80]; 7 | int n,i,x; 8 | printf("Enter a string: "); 9 | scanf("%s",str); 10 | n=strlen(str); 11 | x=0; 12 | for(i=n-1;i >=0; i--) 13 | { 14 | rev[x]=str[i]; 15 | x++; 16 | } 17 | rev[x]='\0'; 18 | if(strcmp(str,rev)==0) 19 | printf("The %s is palendrome",str); 20 | else 21 | printf("The %s is not palendrome",str); 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter02/repetitive.c: -------------------------------------------------------------------------------- 1 | #include 2 | # include 3 | 4 | int ifexists(char u, char z[], int v) 5 | { 6 | int i; 7 | for (i=0; i 4 | # include 5 | 6 | #define max 10 7 | 8 | int top=-1; 9 | int stack[max]; 10 | 11 | void push(int); 12 | int pop(); 13 | int findarmstrong(int ); 14 | 15 | void main() 16 | { 17 | int n; 18 | printf("Enter a number "); 19 | scanf("%d",&n); 20 | if (findarmstrong(n)) 21 | printf("%d is an armstrong number",n); 22 | else printf("%d is not an armstrong number", n); 23 | } 24 | 25 | int findarmstrong(int numb) 26 | { 27 | int j, remainder, temp,count,value; 28 | temp=numb; 29 | count=0; 30 | while(numb >0) 31 | { 32 | remainder=numb%10; 33 | push(remainder); 34 | count++; 35 | numb=numb/10; 36 | } 37 | numb=temp; 38 | value=0; 39 | while(top >=0) 40 | { 41 | j=pop(); 42 | value=value+pow(j,count); 43 | } 44 | if(value==numb)return 1; 45 | else return 0; 46 | } 47 | 48 | void push(int m) 49 | { 50 | top++; 51 | stack[top]=m; 52 | } 53 | 54 | int pop() 55 | { 56 | int j; 57 | if(top==-1)return(top); 58 | else 59 | { 60 | j=stack[top]; 61 | top--; 62 | return(j); 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Chapter03/binaryintohexa.c: -------------------------------------------------------------------------------- 1 | /* Converting binary to hexa */ 2 | 3 | # include 4 | #include 5 | 6 | #define max 10 7 | 8 | int top=-1; 9 | int stack[max]; 10 | 11 | void push(); 12 | int pop(); 13 | char prnhexa(int); 14 | int intodecimal(int); 15 | void intohexa(int, int); 16 | 17 | void main() 18 | { 19 | int b,d; 20 | printf("Enter a number in binary number "); 21 | scanf("%d",&b); 22 | d=intodecimal(b); 23 | printf("The decimal of binary number %d is %d\n", b, d); 24 | intohexa(b,d); 25 | } 26 | 27 | int intodecimal(int bin) 28 | { 29 | int deci, remainder,exp,j; 30 | while(bin >0) 31 | { 32 | remainder=bin%10; 33 | push(remainder); 34 | bin=bin/10; 35 | } 36 | deci=0; 37 | exp=top; 38 | while(top >=0) 39 | { 40 | j=pop(); 41 | deci=deci+j*pow(2,exp); 42 | exp--; 43 | } 44 | return (deci); 45 | } 46 | 47 | void intohexa(int bin, int deci) 48 | { 49 | int remainder,j; 50 | while(deci >0) 51 | { 52 | remainder=deci%16; 53 | push(remainder); 54 | deci=deci/16; 55 | } 56 | printf("The hexa decimal format of binary number %d is ",bin); 57 | while(top >=0) 58 | { 59 | j=pop(); 60 | if(j<10)printf("%d",j); 61 | else printf("%c",prnhexa(j)); 62 | } 63 | } 64 | 65 | void push(int m) 66 | { 67 | top++; 68 | stack[top]=m; 69 | } 70 | 71 | int pop() 72 | { 73 | int j; 74 | if(top==-1)return(top); 75 | j=stack[top]; 76 | top--; 77 | return(j); 78 | } 79 | 80 | char prnhexa(int v) 81 | { 82 | switch(v) 83 | { 84 | case 10: return ('A'); 85 | break; 86 | case 11: return ('B'); 87 | break; 88 | case 12: return ('C'); 89 | break; 90 | case 13: return ('D'); 91 | break; 92 | case 14: return ('E'); 93 | break; 94 | case 15: return ('F'); 95 | break; 96 | } 97 | } 98 | 99 | 100 | -------------------------------------------------------------------------------- /Chapter03/findpalindrome.c: -------------------------------------------------------------------------------- 1 | /* Find out whether the entered number is palindrome or not */ 2 | 3 | # include 4 | #include 5 | 6 | #define max 10 7 | 8 | int top=-1; 9 | int stack[max]; 10 | 11 | void push(); 12 | int pop(); 13 | int findpalindrome(int); 14 | 15 | void main() 16 | { 17 | int n; 18 | printf("Enter a number "); 19 | scanf("%d",&n); 20 | if(findpalindrome(n)) 21 | printf("%d is a palindrome number",n); 22 | else 23 | printf("%d is not a palindrome number", n); 24 | } 25 | 26 | int findpalindrome(int numb) 27 | { 28 | int j, value, remainder, temp,count; 29 | temp=numb; 30 | while(numb >0) 31 | { 32 | remainder=numb%10; 33 | push(remainder); 34 | numb=numb/10; 35 | } 36 | numb=temp; 37 | count=0; 38 | value=0; 39 | while(top >=0) 40 | { 41 | j=pop(); 42 | value=value+j*pow(10,count); 43 | count++; 44 | } 45 | if(numb==value) return (1); 46 | else return (0); 47 | } 48 | 49 | void push(int m) 50 | { 51 | top++; 52 | stack[top]=m; 53 | } 54 | 55 | int pop() 56 | { 57 | int j; 58 | if(top==-1)return(top); 59 | else 60 | { 61 | j=stack[top]; 62 | top--; 63 | return(j); 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /Chapter03/gcd.c: -------------------------------------------------------------------------------- 1 | #include 2 | int gcd(int p, int q); 3 | 4 | void main() 5 | { 6 | int u,v,g; 7 | printf("Enter two numbers: "); 8 | scanf("%d %d",&u,&v); 9 | g=gcd(u,v); 10 | printf("Greatest Common Divisor of %d and %d is %d",u,v,g); 11 | } 12 | 13 | int gcd(int a, int b) 14 | { 15 | int m; 16 | m=a%b; 17 | if(m==0) 18 | return(b); 19 | else 20 | gcd(b,m); 21 | } 22 | -------------------------------------------------------------------------------- /Chapter03/returnarray.c: -------------------------------------------------------------------------------- 1 | /* Find out maximum and minimum of some values using function returning an array */ 2 | 3 | # include 4 | 5 | #define max 10 6 | 7 | int *maxmin(int ar[], int v); 8 | 9 | void main() 10 | { 11 | int arr[max]; 12 | int n,i, *p; 13 | printf("How many values? "); 14 | scanf("%d",&n); 15 | printf("Enter %d values\n", n); 16 | for(i=0;i ar[i]) 32 | mm[0]=ar[i]; 33 | if(mm[1]< ar[i]) 34 | mm[1]= ar[i]; 35 | } 36 | return mm; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter04/assertdemoprog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int c, noOfPassengers; 7 | char fl_from[30], fl_to[30], dateofJourney[12]; 8 | printf("How many passengers ? "); 9 | scanf("%d",&noOfPassengers); 10 | assert(noOfPassengers > 0 && "Number of passengers should be a positive integer"); 11 | printf("Flight from: "); 12 | while((c= getchar()) != '\n' && c != EOF); 13 | gets(fl_from); 14 | printf("Flight to: "); 15 | gets(fl_to); 16 | printf("Date of journey "); 17 | scanf("%s", dateofJourney); 18 | printf("The information entered is:\n"); 19 | printf("Number of passengers %d\n", noOfPassengers); 20 | printf("Flight from: %s\n", fl_from); 21 | printf("Flight to: %s\n", fl_to); 22 | printf("Date of journey: %s\n", dateofJourney); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter04/assertprog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float findaverage(int *Ptr, int Count); 5 | int main() 6 | { 7 | int arr[]={3,9,1,6,2}; 8 | float average; 9 | int *ptr=NULL,count; 10 | 11 | ptr=arr; 12 | count=5; 13 | average=findaverage(ptr, count); 14 | printf("Average of values is %f\n", average); 15 | return(0); 16 | } 17 | 18 | float findaverage(int *Ptr, int Count) 19 | { 20 | int sum,i; 21 | float Average; 22 | assert(Ptr != NULL && "Pointer is not pointing to any array"); 23 | sum=0; 24 | for(i=0;i 2 | #include 3 | 4 | struct customers 5 | { 6 | int orderid; 7 | char customer_name[20]; 8 | float amount; 9 | }; 10 | 11 | static_assert(sizeof(struct customers) == 28, "The structure is consuming unexpected number of bytes"); 12 | 13 | int main(void) 14 | { 15 | printf("sizeof(int) %d\n",sizeof(int)); 16 | printf("sizeof(float) %d\n",sizeof(float)); 17 | printf("sizeof(char) %d\n",sizeof(char)); 18 | printf("sizeof(struct customers) %d\n",sizeof(struct customers)); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter04/condcompile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define Qty 10 3 | #define FestivalOffer 2 4 | #define DiscountCoupon 5 5 | #define Kindle 2 6 | 7 | int main() 8 | { 9 | int discount; 10 | float price, totalAmount, afterDisc, afterFDisc; 11 | 12 | printf("Enter price of a book "); 13 | scanf("%f", &price); 14 | #if Qty >= 10 15 | discount=15; 16 | #elif Qty >=5 17 | discount=10; 18 | #else 19 | discount=5; 20 | #endif 21 | totalAmount=Qty*price; 22 | afterDisc=totalAmount - (totalAmount*discount)/100; 23 | #ifdef FestivalOffer 24 | afterFDisc=afterDisc-(totalAmount*FestivalOffer)/100; 25 | #else 26 | afterFDisc=afterDisc; 27 | #endif 28 | 29 | 30 | printf("Quantity = %d, Price is $ %.2f, total amount for the books is $ %.2f\n", Qty, price, totalAmount); 31 | printf("Discount is %d%% and the total amount after discount is $ %.2f\n", discount, afterDisc); 32 | #ifdef FestivalOffer 33 | printf("Festival discount is %d%%, the total amount after festival discount is $ %.2f\n", FestivalOffer, afterFDisc); 34 | #endif 35 | #if defined (DiscountCoupon) 36 | printf("You are also eligible for a discount coupon of $ %d\n", DiscountCoupon); 37 | #endif 38 | #ifndef Kindle 39 | #define Kindle 1 40 | #endif 41 | printf("You can use the Kindle version of the book for %d month(s)\n", Kindle); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Chapter04/preconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define pizzaprice(a, b) a##b 5 | #define convertIntoStr(str) #str 6 | 7 | int main() 8 | { 9 | float smallnormal=5; 10 | float mediumnormal=7; 11 | float largenormal=10; 12 | float smallextra=7; 13 | float mediumextra=9; 14 | float largeextra=12; 15 | char pizzasize[30]; 16 | char topping[20]; 17 | 18 | printf("What size pizza you want? small/medium/large: "); 19 | scanf("%s", pizzasize); 20 | printf("Normal or with extra cheese? normal/extra: "); 21 | scanf("%s",topping); 22 | if(strcmp(topping, "normal")==0) 23 | { 24 | if(strcmp(pizzasize, "small")==0) 25 | printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(small, normal)); 26 | else 27 | if(strcmp(pizzasize, "medium")==0) 28 | printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(medium, normal)); 29 | else 30 | printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(large, normal)); 31 | } 32 | if(strcmp(topping, "extra")==0) 33 | { 34 | if(strcmp(pizzasize, "small")==0) 35 | printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(small, extra)); 36 | else 37 | if(strcmp(pizzasize, "medium")==0) 38 | printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(medium, extra)); 39 | else 40 | printf("The prize for %s size pizza with %s toppings is $%.2f \n", pizzasize, topping, pizzaprice(large, extra)); 41 | } 42 | printf(convertIntoStr(Thanks for visiting us)); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Chapter05/largestinarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define max 100 4 | 5 | void main() 6 | { 7 | int p[max], i,n, *ptr, *mx; 8 | 9 | printf("How many elements are there? "); 10 | scanf("%d", &n); 11 | printf("Enter %d elements \n", n); 12 | for(i=0;i 2 | #include 3 | 4 | struct cart 5 | { 6 | int orderno; 7 | char emailaddress[30]; 8 | char password[30]; 9 | }; 10 | 11 | void main() 12 | { 13 | struct cart mycart; 14 | struct cart *ptrcart, *ptrcust; 15 | ptrcart = &mycart; 16 | printf("Enter order number: "); 17 | scanf("%d",&mycart.orderno); 18 | printf("Enter email address: "); 19 | scanf("%s",mycart.emailaddress); 20 | printf("Enter password: "); 21 | scanf("%s",mycart.password); 22 | printf("\nDetails of the customer are as follows:\n"); 23 | printf("Order number : %d\n", ptrcart->orderno); 24 | printf("Email address : %s\n", ptrcart->emailaddress); 25 | printf("Password : %s\n", ptrcart->password); 26 | printf("\nEnter new email address: "); 27 | scanf("%s",ptrcart->emailaddress); 28 | printf("Enter new password: "); 29 | scanf("%s",ptrcart->password); 30 | printf("\nModified customer's information is:\n"); 31 | printf("Order number: %d\n", mycart.orderno); 32 | printf("Email address: %s\n", mycart.emailaddress); 33 | printf("Password: %s\n", mycart.password); 34 | ptrcust=(struct cart *)malloc(sizeof(struct cart)); 35 | printf("\nEnter information of another customer:\n"); 36 | printf("Enter order number: "); 37 | scanf("%d",&ptrcust->orderno); 38 | printf("Enter email address: "); 39 | scanf("%s",ptrcust->emailaddress); 40 | printf("Enter password: "); 41 | scanf("%s",ptrcust->password); 42 | printf("\nDetails of the second customer are as follows:\n"); 43 | printf("Order number : %d\n", ptrcust->orderno); 44 | printf("Email address : %s\n", ptrcust->emailaddress); 45 | printf("Password : %s\n", ptrcust->password); 46 | } -------------------------------------------------------------------------------- /Chapter05/reversestring.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | char str[255], *ptr1, *ptr2, temp ; 6 | int n,m; 7 | printf("Enter a string: "); 8 | scanf("%s", str); 9 | ptr1=str; 10 | n=1; 11 | while(*ptr1 !='\0') 12 | { 13 | ptr1++; 14 | n++; 15 | } 16 | ptr1--; 17 | ptr2=str; 18 | m=1; 19 | while(m<=n/2) 20 | { 21 | temp=*ptr1; 22 | *ptr1=*ptr2; 23 | *ptr2=temp; 24 | ptr1--; 25 | ptr2++;; 26 | m++; 27 | } 28 | printf("Reverse string is %s", str); 29 | } -------------------------------------------------------------------------------- /Chapter05/sortlinkedlist.c: -------------------------------------------------------------------------------- 1 | /* Sort the linked list by bubble sort */ 2 | 3 | #include 4 | #include 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }; 10 | 11 | 12 | void main() 13 | { 14 | struct node *temp1,*temp2, *t,*newNode, *startList; 15 | int n,k,i,j; 16 | startList=NULL; 17 | printf("How many elements are there in the linked list ?"); 18 | scanf("%d",&n); 19 | 20 | printf("Enter elements in the linked list\n"); 21 | for(i=1;i<=n;i++) 22 | { 23 | if(startList==NULL) 24 | { 25 | newNode=(struct node *)malloc(sizeof(struct node)); 26 | scanf("%d",&newNode->data); 27 | newNode->next=NULL; 28 | startList = newNode; 29 | temp1=startList; 30 | } 31 | else 32 | { 33 | newNode=(struct node *)malloc(sizeof(struct node)); 34 | scanf("%d",&newNode->data); 35 | newNode->next=NULL; 36 | temp1->next = newNode; 37 | temp1=newNode; 38 | } 39 | } 40 | for(i=n-2;i>=0;i--) 41 | { 42 | temp1=startList; 43 | temp2=temp1->next; 44 | for(j=0;j<=i;j++) 45 | { 46 | if(temp1->data > temp2->data) 47 | { 48 | k=temp1->data; 49 | temp1->data=temp2->data; 50 | temp2->data=k; 51 | } 52 | temp1=temp2; 53 | temp2=temp2->next; 54 | } 55 | } 56 | printf("Sorted order is: \n"); 57 | t=startList; 58 | while(t!=NULL) 59 | { 60 | printf("%d\t",t->data); 61 | t=t->next; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter05/transposemat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | int a[10][10], r, c, i, j, *ptr,m; 7 | printf("Enter rows and columns of matrix: "); 8 | scanf("%d %d", &r, &c); 9 | ptr = (int *)malloc(r * c * sizeof(int)); 10 | printf("\nEnter elements of matrix:\n"); 11 | for(i=0; i 2 | #include 3 | #include 4 | 5 | 6 | #define BUFFSIZE 255 7 | 8 | void main (int argc, char* argv[]) 9 | { 10 | FILE *fp; 11 | char buffer[BUFFSIZE]; 12 | int i,n; 13 | 14 | fp = fopen (argv [1],"r"); 15 | if (fp == NULL) { 16 | printf("%s file does not exist\n", argv[1]); 17 | exit(1); 18 | } 19 | while (!feof(fp)) 20 | { 21 | fgets(buffer, BUFFSIZE, fp); 22 | n=strlen(buffer); 23 | for(i=0;i=97 && buffer[i] <=122) 33 | { 34 | buffer[i]=buffer[i]-32; 35 | } 36 | } 37 | } 38 | puts(buffer); 39 | } 40 | fclose(fp); 41 | } 42 | -------------------------------------------------------------------------------- /Chapter06/countvowels.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define BUFFSIZE 255 6 | 7 | void main (int argc, char* argv[]) 8 | { 9 | FILE *fp; 10 | char buffer[BUFFSIZE]; 11 | int n, i, count=0; 12 | 13 | fp = fopen (argv [1],"r"); 14 | if (fp == NULL) { 15 | printf("%s file does not exist\n", argv[1]); 16 | exit(1); 17 | } 18 | printf("The file content is :\n"); 19 | while (!feof(fp)) 20 | { 21 | fgets(buffer, BUFFSIZE, fp); 22 | puts(buffer); 23 | n=strlen(buffer); 24 | for(i=0;i 2 | #include 3 | #include 4 | 5 | #define BUFFSIZE 255 6 | 7 | void main (int argc, char* argv[]) 8 | { 9 | FILE *fp,*fq; 10 | int i,n; 11 | char buffer[BUFFSIZE]; 12 | 13 | /* Open the source file in read mode */ 14 | fp = fopen (argv [1],"r"); 15 | if (fp == NULL) { 16 | printf("%s file does not exist\n", argv[1]); 17 | exit(1); 18 | } 19 | 20 | /* Create the destination file. */ 21 | fq = fopen (argv[2], "w"); 22 | if (fq == NULL) { 23 | perror ("An error occurred in creating the file\n"); 24 | exit(1); 25 | } 26 | while (!feof(fp)) 27 | { 28 | fgets(buffer, BUFFSIZE, fp); 29 | n=strlen(buffer); 30 | for(i=0;i 2 | #include 3 | #include 4 | 5 | struct data{ 6 | char str[ 255 ]; 7 | }; 8 | 9 | void main (int argc, char* argv[]) 10 | { 11 | 12 | FILE *fp; 13 | struct data line; 14 | int n,nol,i; 15 | fp = fopen (argv[1], "rb"); 16 | if (fp == NULL) { 17 | perror ("An error occurred in opening the file\n"); 18 | exit(1); 19 | } 20 | fseek(fp, 0L, SEEK_END); 21 | n = ftell(fp); 22 | nol=n/sizeof(struct data); 23 | printf("The content of random file in reverse order is :\n"); 24 | for (i=1;i<=nol;i++) 25 | { 26 | fseek(fp, -sizeof(struct data)*i, SEEK_END); 27 | fread(&line,sizeof(struct data),1,fp); 28 | puts(line.str); 29 | } 30 | fclose(fp); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter06/replaceword.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main (int argc, char* argv[]) 6 | { 7 | FILE *fp; 8 | char line[255], nline[300], str1[80], str2[80]; 9 | int i,ll, ls1,ls2, x,k, w, oldi; 10 | 11 | fp = fopen (argv [1],"r"); 12 | if (fp == NULL) { 13 | printf("%s file does not exist\n", argv[1]); 14 | exit(1); 15 | } 16 | printf("Enter a string to be replaced: "); 17 | scanf("%s", str1); 18 | printf("Enter the new string "); 19 | scanf("%s", str2); 20 | ls1=strlen(str1); 21 | ls2=strlen(str2); 22 | x=0; 23 | while (!feof(fp)) 24 | { 25 | fgets(line, 255, fp); 26 | ll=strlen(line); 27 | 28 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | #define max 10 7 | pthread_mutex_t pop_mutex; 8 | pthread_mutex_t push_mutex; 9 | int stack[max]; 10 | int top=-1; 11 | 12 | void * push(void *arg) { 13 | int n; 14 | pthread_mutex_lock(&push_mutex); 15 | sleep(2); 16 | pthread_mutex_lock(&pop_mutex); 17 | printf("Enter the value to push: "); 18 | scanf("%d",&n); 19 | top++; 20 | stack[top]=n; 21 | pthread_mutex_unlock(&pop_mutex); 22 | pthread_mutex_unlock(&push_mutex); 23 | printf("Value is pushed to stack \n"); 24 | } 25 | 26 | void * pop(void *arg) { 27 | int k; 28 | pthread_mutex_lock(&push_mutex); 29 | sleep(5); 30 | pthread_mutex_lock(&pop_mutex); 31 | k=stack[top]; 32 | top--; 33 | printf("Value popped from stack is %d \n",k); 34 | pthread_mutex_unlock(&pop_mutex); 35 | pthread_mutex_unlock(&push_mutex); 36 | } 37 | 38 | 39 | int main() { 40 | int result; 41 | pthread_t tid1,tid2; 42 | pthread_create(&tid1,NULL,&push,NULL); 43 | pthread_create(&tid2,NULL,&pop,NULL); 44 | printf("Both threads are created\n"); 45 | pthread_join(tid1,NULL); 46 | pthread_join(tid2,NULL); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Chapter07/createthread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | void *runThread(void *arg) 7 | { 8 | int i; 9 | printf("Running Thread \n"); 10 | for(i=1;i<=5;i++) printf("%d\n",i); 11 | return NULL; 12 | } 13 | 14 | int main() 15 | { 16 | pthread_t tid; 17 | printf("In main function\n"); 18 | pthread_create(&tid, NULL, runThread, NULL); 19 | pthread_join(tid, NULL); 20 | printf("Thread over\n"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter07/deadlockstate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define max 10 7 | pthread_mutex_t pop_mutex; 8 | pthread_mutex_t push_mutex; 9 | int stack[max]; 10 | int top=-1; 11 | 12 | void * push(void *arg) { 13 | int n; 14 | pthread_mutex_lock(&push_mutex); 15 | pthread_mutex_lock(&pop_mutex); 16 | printf("Enter the value to push: "); 17 | scanf("%d",&n); 18 | top++; 19 | stack[top]=n; 20 | pthread_mutex_unlock(&pop_mutex); 21 | pthread_mutex_unlock(&push_mutex); 22 | printf("Value is pushed to stack \n"); 23 | } 24 | 25 | 26 | void * pop(void *arg) { 27 | int k; 28 | pthread_mutex_lock(&pop_mutex); 29 | pthread_mutex_lock(&push_mutex); 30 | k=stack[top]; 31 | top--; 32 | printf("Value popped is %d \n",k); 33 | pthread_mutex_unlock(&push_mutex); 34 | pthread_mutex_unlock(&pop_mutex); 35 | } 36 | 37 | 38 | int main() { 39 | pthread_t tid1,tid2; 40 | pthread_create(&tid1,NULL,&push,NULL); 41 | pthread_create(&tid2,NULL,&pop,NULL); 42 | printf("Both threads are created\n"); 43 | pthread_join(tid1,NULL); 44 | pthread_join(tid2,NULL); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Chapter07/twothreads.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void *runThread1(void *arg){ 5 | int i; 6 | printf("Running Thread 1\n"); 7 | for(i=1;i<=5;i++) 8 | printf("Thread 1 - %d\n",i); 9 | } 10 | 11 | void *runThread2(void *arg){ 12 | int i; 13 | printf("Running Thread 2\n"); 14 | for(i=1;i<=5;i++) 15 | printf("Thread 2 - %d\n",i); 16 | } 17 | 18 | int main(){ 19 | pthread_t tid1, tid2; 20 | pthread_create(&tid1,NULL,runThread1,NULL); 21 | pthread_create(&tid2,NULL,runThread2,NULL); 22 | pthread_join(tid1,NULL); 23 | pthread_join(tid2,NULL); 24 | printf("Both threads are over\n"); 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Chapter07/twothreadsmutex.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | pthread_t tid1,tid2; 6 | pthread_mutex_t lock; 7 | 8 | void* runThread(void *arg) 9 | { 10 | pthread_mutex_lock(&lock); 11 | pthread_t id = pthread_self(); 12 | if(pthread_equal(id,tid1)) 13 | printf("First thread is running\n"); 14 | else 15 | printf("Second thread is running\n"); 16 | printf("Processing the common resource\n"); 17 | sleep(5); 18 | if(pthread_equal(id,tid1)) 19 | printf("First thread is over\n\n"); 20 | else 21 | printf("Second thread is over\n\n"); 22 | pthread_mutex_unlock(&lock); 23 | return NULL; 24 | } 25 | 26 | int main(void) 27 | { 28 | if (pthread_mutex_init(&lock, NULL) != 0) 29 | printf("\n mutex init has failed\n"); 30 | pthread_create(&tid1, NULL, &runThread, NULL); 31 | pthread_create(&tid2, NULL, &runThread, NULL); 32 | pthread_join(tid1, NULL); 33 | pthread_join(tid2, NULL); 34 | pthread_mutex_destroy(&lock); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Chapter08/clientprog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(){ 8 | int clientSocket; 9 | char str[255]; 10 | struct sockaddr_in client_Address; 11 | socklen_t address_size; 12 | 13 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 14 | client_Address.sin_family = AF_INET; 15 | client_Address.sin_port = htons(2000); 16 | client_Address.sin_addr.s_addr = inet_addr("127.0.0.1"); 17 | memset(client_Address.sin_zero, '\0', sizeof client_Address.sin_zero); 18 | address_size = sizeof client_Address; 19 | connect(clientSocket, (struct sockaddr *) &client_Address, address_size); 20 | recv(clientSocket, str, 255, 0); 21 | printf("Data received froms server: %s",str); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter08/messageqrecv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define MSGSIZE 255 7 | 8 | struct msgstruc { 9 | long mtype; 10 | char mesg[MSGSIZE]; 11 | }; 12 | 13 | 14 | int main() 15 | { 16 | int msqid; 17 | key_t key; 18 | struct msgstruc rcvbuffer; 19 | 20 | if ((key = ftok("messagefile", 'a')) == -1) { 21 | perror("ftok"); 22 | exit(1); 23 | } 24 | if ((msqid = msgget(key, 0666)) < 0) 25 | { 26 | perror("msgget"); 27 | exit(1); 28 | } 29 | if (msgrcv(msqid, &rcvbuffer, MSGSIZE, 1, 0) < 0) 30 | { 31 | perror("msgrcv"); 32 | exit(1); 33 | } 34 | printf("The message received is %s\n", rcvbuffer.mesg); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Chapter08/messageqsend.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define MSGSIZE 255 9 | 10 | struct msgstruc { 11 | long mtype; 12 | char mesg[MSGSIZE]; 13 | }; 14 | 15 | int main() 16 | { 17 | int msqid, msglen; 18 | key_t key; 19 | struct msgstruc msgbuf; 20 | system("touch messagefile"); 21 | if ((key = ftok("messagefile", 'a')) == -1) { 22 | perror("ftok"); 23 | exit(1); 24 | } 25 | if ((msqid = msgget(key, 0666 | IPC_CREAT)) == -1) { 26 | perror("msgget"); 27 | exit(1); 28 | } 29 | msgbuf.mtype = 1; 30 | printf("Enter a message to add to message queue : "); 31 | scanf("%s",msgbuf.mesg); 32 | msglen = strlen(msgbuf.mesg); 33 | if (msgsnd(msqid, &msgbuf, msglen, IPC_NOWAIT) < 0) 34 | perror("msgsnd"); 35 | printf("The message sent is %s\n", msgbuf.mesg); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Chapter08/pipedemo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define max 50 7 | 8 | int main() 9 | { 10 | char wstr[max]; 11 | char rstr[max]; 12 | int pp[2]; 13 | pid_t p; 14 | if(pipe(pp) < 0) 15 | { 16 | perror("pipe"); 17 | } 18 | p = fork(); 19 | if(p >= 0) 20 | { 21 | if(p == 0) 22 | { 23 | printf ("Enter the string : "); 24 | gets(wstr); 25 | write (pp[1] , wstr , strlen(wstr)); 26 | exit(0); 27 | } 28 | else 29 | { 30 | read (pp[0] , rstr , sizeof(rstr)); 31 | printf("Entered message : %s\n " , rstr); 32 | exit(0); 33 | } 34 | } 35 | else 36 | { 37 | perror("fork"); 38 | exit(2); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter08/readfifo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define BUFFSIZE 255 7 | 8 | int main() 9 | { 10 | int fr; 11 | char str[BUFFSIZE]; 12 | 13 | fr = open("FIFOPipe", O_RDONLY); 14 | read(fr, str, BUFFSIZE); 15 | printf("Read from the FIFO Pipe: %s\n", str); 16 | close(fr); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter08/readmemory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int shmid; 10 | char * str; 11 | 12 | key_t key = ftok("sharedmem",'a'); 13 | if ((shmid = shmget(key, 1024,0666|IPC_CREAT)) < 0) { 14 | perror("shmget"); 15 | exit(1); 16 | } 17 | if ((str = shmat(shmid, NULL, 0)) == (char *) -1) { 18 | perror("shmat"); 19 | exit(1); 20 | } 21 | printf("Data read from memory: %s\n",str); 22 | shmdt(str); 23 | shmctl(shmid,IPC_RMID,NULL); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter08/readwritepipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define max 50 6 | 7 | int main() 8 | { 9 | char str[max]; 10 | int pp[2]; 11 | 12 | if (pipe(pp) < 0) 13 | exit(1); 14 | printf("Enter first message to write into pipe: "); 15 | gets(str); 16 | write(pp[1], str, max); 17 | printf("Enter second message to write into pipe: "); 18 | gets(str); 19 | write(pp[1], str, max); 20 | printf("Messages read from the pipe are as follows:\n"); 21 | read(pp[0], str, max); 22 | printf("%s\n", str); 23 | read(pp[0], str, max); 24 | printf("%s\n", str); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Chapter08/serverprog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(){ 8 | int serverSocket, toSend; 9 | char str[255]; 10 | struct sockaddr_in server_Address; 11 | 12 | serverSocket = socket(AF_INET, SOCK_STREAM, 0); 13 | server_Address.sin_family = AF_INET; 14 | server_Address.sin_port = htons(2000); 15 | server_Address.sin_addr.s_addr = inet_addr("127.0.0.1"); 16 | memset(server_Address.sin_zero, '\0', sizeof server_Address.sin_zero); 17 | bind(serverSocket, (struct sockaddr *) &server_Address, sizeof(server_Address)); 18 | if(listen(serverSocket,5)==-1) 19 | { 20 | printf("Not able to listen\n"); 21 | return -1; 22 | } 23 | printf("Enter text to send to the client: "); 24 | gets(str); 25 | toSend = accept(serverSocket, (struct sockaddr *) NULL, NULL); 26 | send(toSend,str, strlen(str),0); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter08/udpc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | char msgReceived[255]; 13 | char msgforserver[255]; 14 | int UDPSocket, n; 15 | struct sockaddr_in client_Address; 16 | printf("Enter the message to send to the server: "); 17 | gets(msgforserver); 18 | bzero(&client_Address, sizeof(client_Address)); 19 | client_Address.sin_addr.s_addr = INADDR_ANY; 20 | client_Address.sin_port = htons(2000); 21 | client_Address.sin_family = AF_INET; 22 | if ( (UDPSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 23 | perror("Socket could not be created"); 24 | exit(1); 25 | } 26 | 27 | if(connect(UDPSocket, (struct sockaddr *)&client_Address, sizeof(client_Address)) < 0) 28 | { 29 | printf("\n Error : Connect Failed \n"); 30 | exit(0); 31 | } 32 | sendto(UDPSocket, msgforserver, 255, 0, (struct sockaddr*)NULL, sizeof(client_Address)); 33 | printf("Message to the server sent. \n"); 34 | recvfrom(UDPSocket, msgReceived, sizeof(msgReceived), 0, (struct sockaddr*)NULL, NULL); 35 | printf("Received from the server: "); 36 | puts(msgReceived); 37 | close(UDPSocket); 38 | } -------------------------------------------------------------------------------- /Chapter08/udps.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | char msgReceived[255]; 12 | char msgforclient[255]; 13 | int UDPSocket, len; 14 | struct sockaddr_in server_Address, client_Address; 15 | bzero(&server_Address, sizeof(server_Address)); 16 | printf("Waiting for the message from the client\n"); 17 | if ( (UDPSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 18 | perror("Socket could not be created"); 19 | exit(1); 20 | } 21 | server_Address.sin_addr.s_addr = htonl(INADDR_ANY); 22 | server_Address.sin_port = htons(2000); 23 | server_Address.sin_family = AF_INET; 24 | if ( bind(UDPSocket, (const struct sockaddr *)&server_Address, sizeof(server_Address)) < 0 ) 25 | { 26 | perror("Binding could not be done"); 27 | exit(1); 28 | } 29 | len = sizeof(client_Address); 30 | int n = recvfrom(UDPSocket, msgReceived, sizeof(msgReceived), 0, (struct sockaddr*)&client_Address,&len); 31 | msgReceived[n] = '\0'; 32 | printf("Message received from the client: "); 33 | puts(msgReceived); 34 | printf("Enter the reply to be sent to the client: "); 35 | gets(msgforclient); 36 | sendto(UDPSocket, msgforclient, 255, 0, (struct sockaddr*)&client_Address, sizeof(client_Address)); 37 | printf("Reply to the client sent \n"); 38 | } -------------------------------------------------------------------------------- /Chapter08/writefifo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int fw; 9 | char str[255]; 10 | mkfifo("FIFOPipe", 0666); 11 | fw = open("FIFOPipe", O_WRONLY); 12 | printf("Enter text: "); 13 | gets(str); 14 | write(fw,str, sizeof(str)); 15 | close(fw); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter08/writememory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | char *str; 10 | int shmid; 11 | 12 | key_t key = ftok("sharedmem",'a'); 13 | if ((shmid = shmget(key, 1024,0666|IPC_CREAT)) < 0) { 14 | perror("shmget"); 15 | exit(1); 16 | } 17 | if ((str = shmat(shmid, NULL, 0)) == (char *) -1) { 18 | perror("shmat"); 19 | exit(1); 20 | } 21 | printf("Enter the string to be written in memory : "); 22 | gets(str); 23 | printf("String written in memory: %s\n",str); 24 | shmdt(str); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Chapter09/binarysearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 20 3 | 4 | int binary_search(int[], int, int); 5 | 6 | int main() 7 | { 8 | int len,found,numb,arr[max],i; 9 | printf("Enter the length of an array: "); 10 | scanf("%d",&len); 11 | printf("Enter %d values in sorted order \n", len); 12 | for(i=0;i< len;i++) 13 | scanf("%d",&arr[i]); 14 | printf("Enter the value to search "); 15 | scanf("%d",&numb); 16 | found=binary_search(arr,numb,len); 17 | if(found==numb) 18 | printf("Value %d is found in the list\n",numb); 19 | else 20 | printf("Value %d is not found in the list \n", numb); 21 | return 0; 22 | } 23 | 24 | int binary_search(int arr[], int pnumb,int plen) 25 | { 26 | int lindex=0,mid,uindex=plen, nfound; 27 | while(uindex >=lindex) 28 | { 29 | mid=(uindex+lindex)/2; 30 | if(pnumb==arr[mid]) 31 | { 32 | nfound=arr[mid]; 33 | break; 34 | } 35 | else 36 | { 37 | if(pnumb>arr[mid]) 38 | lindex=mid+1; 39 | else 40 | uindex=mid-1; 41 | } 42 | } 43 | return(nfound); 44 | } 45 | -------------------------------------------------------------------------------- /Chapter09/bubblesort.c: -------------------------------------------------------------------------------- 1 | # include 2 | #define max 20 3 | 4 | int main() 5 | { 6 | int arr[max],temp,len,i,j; 7 | printf("How many values are there? "); 8 | scanf("%d",&len); 9 | printf("Enter %d values to sort\n",len); 10 | for(i=0;i=1;i--) 13 | { 14 | for(j=0;j<=i;j++) 15 | { 16 | if(arr[j]>arr[j+1]) 17 | { 18 | temp=arr[j]; 19 | arr[j]=arr[j+1]; 20 | arr[j+1]=temp; 21 | } 22 | } 23 | } 24 | printf("The sorted array is:\n"); 25 | for(i=0;i 2 | #define max 20 3 | int heap[max],len; 4 | 5 | void insheap(int h); 6 | int delsheap(int j); 7 | 8 | int main() 9 | { 10 | int arr[max],numb,i,j; 11 | printf("How many elements to sort? "); 12 | scanf("%d",&len); 13 | printf("Enter %d values \n", len); 14 | for(i=0;iheap[par]) 47 | { 48 | temp=heap[cur]; 49 | heap[cur]=heap[par]; 50 | heap[par]=temp; 51 | cur=par; 52 | par=(cur-1)/2; 53 | 54 | } 55 | else break; 56 | } while(cur!=0); 57 | x++; 58 | } 59 | } 60 | 61 | int delsheap(int j) 62 | { 63 | int loc,n=0,pos,lc=0,rc=0,temp=0; 64 | loc=j; 65 | pos=0; 66 | n=heap[pos]; 67 | heap[pos]=heap[loc]; 68 | if(loc==0 || loc==1) return (n); 69 | loc--; 70 | lc=2*pos+1; 71 | rc=2*pos+2; 72 | while (rc <=loc) 73 | { 74 | if((heap[pos]>heap[lc] && heap[pos]>heap[rc])) 75 | return(n); 76 | else 77 | { 78 | if(heap[lc]>heap[rc]) 79 | { 80 | temp=heap[lc]; 81 | heap[lc]=heap[pos]; 82 | heap[pos]=temp; 83 | pos=lc; 84 | } 85 | else 86 | { 87 | temp=heap[rc]; 88 | heap[rc]=heap[pos]; 89 | heap[pos]=temp; 90 | pos=rc; 91 | } 92 | lc=2*pos+1; 93 | rc=2*pos+2; 94 | } 95 | } 96 | if(lc==loc) 97 | { 98 | if(heap[pos] 2 | #define max 20 3 | 4 | int main() 5 | { 6 | int arr[max],i,j,temp,len; 7 | 8 | printf("How many numbers are there ? "); 9 | scanf("%d",&len); 10 | printf("Enter %d values to sort\n",len); 11 | for(i=0;i0;j--) 16 | { 17 | if(arr[j] 2 | # define stacksize 10 3 | #define arrsize 20 4 | int top1=-1,top2=-1; 5 | int stack1[stacksize]; 6 | int stack2[stacksize]; 7 | int arr[arrsize]; 8 | 9 | int quick(int, int); 10 | void pushstk1(int); 11 | void pushstk2(int); 12 | int popstk1(); 13 | int popstk2(); 14 | 15 | int main() 16 | { 17 | int sindex,eindex,lindex,uindex,k,pivot,i, len; 18 | printf("How many numerical to sort? "); 19 | scanf("%d",&len); 20 | printf("Enter %d numericals:\n", len); 21 | for(i=0;i<=len-1;i++) 22 | scanf("%d",&arr[i]); 23 | lindex=0; 24 | uindex=len-1; 25 | pushstk1(lindex); 26 | pushstk2(uindex); 27 | while(top1!=-1) 28 | { 29 | sindex=popstk1(); 30 | eindex=popstk2(); 31 | pivot=quick(sindex,eindex); 32 | if(sindex=arr[li] && li !=ei) 56 | ei--; 57 | if(li==ei) return(li); 58 | if(arr[li]>arr[ei]) 59 | { 60 | temp=arr[li]; 61 | arr[li]=arr[ei]; 62 | arr[ei]=temp; 63 | li=ei; 64 | } 65 | while(arr[si]<=arr[li] && li!=si) 66 | si++; 67 | if(li==si) return(li); 68 | if(arr[si]>arr[li]) 69 | { 70 | temp=arr[si]; 71 | arr[si]=arr[li]; 72 | arr[li]=temp; 73 | li=si; 74 | } 75 | } 76 | return 0; 77 | } 78 | 79 | void pushstk1(int s) 80 | { 81 | top1++; 82 | stack1[top1]=s; 83 | } 84 | 85 | void pushstk2(int e) 86 | { 87 | top2++; 88 | stack2[top2]=e; 89 | } 90 | 91 | int popstk1() 92 | { 93 | return(stack1[top1--]); 94 | } 95 | 96 | int popstk2() 97 | { 98 | return(stack2[top2--]); 99 | } -------------------------------------------------------------------------------- /Chapter10/adjlistdirect.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | char nme; 7 | struct node *vrt; 8 | struct node *edg; 9 | }; 10 | 11 | int main() 12 | { 13 | int numb,i,j,noe; 14 | char v1,v2; 15 | struct node *startList,*newNode,*temp1,*temp2; 16 | printf ("How many vertices are there ? "); 17 | scanf("%d",&numb); 18 | startList=NULL; 19 | printf("Enter all vertices names\n"); 20 | for(i=1;i<=numb;i++) 21 | { 22 | if (startList==NULL) 23 | { 24 | newNode =malloc(sizeof (struct node)); 25 | scanf (" %c",&newNode->nme); 26 | /* There is a space before %c */ 27 | startList=newNode; 28 | temp1=newNode; 29 | newNode->vrt=NULL; 30 | newNode->edg=NULL; 31 | } 32 | else 33 | { 34 | newNode=malloc(sizeof (struct node)); 35 | scanf (" %c",&newNode->nme); 36 | /* There is a space before %c */ 37 | newNode->vrt=NULL; 38 | newNode->edg=NULL; 39 | temp1->vrt=newNode; 40 | temp1=newNode; 41 | } 42 | } 43 | printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n"); 44 | printf("between v1 and v2. Enter 0 0 if over\n"); 45 | noe=numb*(numb -1); 46 | for(j=1;j<=noe;j++) 47 | { 48 | scanf(" %c %c",&v1,&v2); 49 | /* There is a space before %c */ 50 | if(v1=='0' && v2=='0')break; 51 | temp1=startList; 52 | while(temp1!=NULL && temp1->nme!=v1) 53 | temp1=temp1->vrt; 54 | if(temp1==NULL) 55 | { 56 | printf("Sorry no vertex exist by this name\n"); 57 | break; 58 | } 59 | temp2=temp1; 60 | while(temp2->edg!=NULL)temp2=temp2->edg; 61 | newNode=malloc(sizeof (struct node)); 62 | newNode->nme=v2; 63 | temp2->edg=newNode; 64 | newNode->edg=NULL; 65 | newNode->vrt=NULL; 66 | } 67 | printf ("\nAdjacency List representation of Graph is\n"); 68 | temp1=startList; 69 | while (temp1!=NULL) 70 | { 71 | printf ("%c\t",temp1->nme); 72 | temp2=temp1->edg; 73 | while(temp2!=NULL) 74 | { 75 | printf("%c\t",temp2->nme); 76 | temp2=temp2->edg; 77 | } 78 | printf("\n"); 79 | temp1=temp1->vrt; 80 | } 81 | } -------------------------------------------------------------------------------- /Chapter10/adjmatdirect.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 10 3 | int main() 4 | { 5 | static int edg[max][max],i,j,v1,v2, numb; 6 | printf("How many vertices are there? "); 7 | scanf("%d",&numb); 8 | printf("We assume that the vertices are numbered from : "); 9 | for(i=1;i<=numb;i++) printf("%d ", i); 10 | printf("\nEnter the edges of the graph. Like 1 4 if there is an \n"); 11 | printf("edge between vertex 1 and 4. Enter 0 0 when over\n"); 12 | for(i=1;i<=numb*(numb-1);i++) 13 | { 14 | /* 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 */ 15 | scanf("%d %d",&v1,&v2); 16 | if(v1==0 && v2==0)break; 17 | edg[v1][v2]=1; 18 | } 19 | printf("\nThe adjacency matrix for the graph is \n"); 20 | for(i=1;i<=numb;i++) printf("\t%d", i); 21 | printf("\n-----------------------------------------------------\n"); 22 | for(i=1;i<=numb;i++) 23 | { 24 | printf("%d |\t",i); 25 | for(j=1;j<=numb;j++) 26 | { 27 | printf("%d\t",edg[i][j]); 28 | } 29 | printf("\n"); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Chapter10/adjmatundirect.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 10 3 | int main() 4 | { 5 | static int edg[max][max],i,j,v1,v2, numb; 6 | printf("How many vertices are there? "); 7 | scanf("%d",&numb); 8 | printf("We assume that the vertices are numbered from : "); 9 | for(i=1;i<=numb;i++) printf("%d ", i); 10 | printf("\nEnter the edges of the graph. Like 1 4 if there is an \n"); 11 | printf("edge between vertex 1 and 4. Enter 0 0 when over\n"); 12 | for(i=1;i<=numb*(numb-1);i++) 13 | { 14 | /* 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 */ 15 | scanf("%d %d",&v1,&v2); 16 | if(v1==0 && v2==0)break; 17 | edg[v1][v2]=1; 18 | edg[v2][v1]=1; 19 | } 20 | printf("\nThe adjacency matrix for the graph is \n"); 21 | for(i=1;i<=numb;i++) printf("\t%d", i); 22 | printf("\n----------------------------------------------------------\n"); 23 | for(i=1;i<=numb;i++) 24 | { 25 | printf("%d |\t",i); 26 | for(j=1;j<=numb;j++) 27 | { 28 | printf("%d\t",edg[i][j]); 29 | } 30 | printf("\n"); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter10/breadthfirsttrav.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define max 20 5 | 6 | enum Setmarked{Y,N}; 7 | struct node 8 | { 9 | char nme; 10 | struct node *vrt; 11 | struct node *edg; 12 | enum Setmarked marked; 13 | }; 14 | 15 | struct node *que[max]; 16 | int rear=-1,front=-1; 17 | void queue(struct node *paramNode); 18 | struct node *dequeue(); 19 | 20 | int main() 21 | { 22 | int numb,i,j,noe; 23 | char v1,v2; 24 | struct node *startList,*newNode,*temp1,*temp2, *temp3; 25 | printf ("How many vertices are there ?"); 26 | scanf("%d",&numb); 27 | startList=NULL; 28 | printf("Enter all vertices names\n"); 29 | for(i=1;i<=numb;i++) 30 | { 31 | if (startList==NULL) 32 | { 33 | newNode =malloc(sizeof (struct node)); 34 | scanf (" %c",&newNode->nme); 35 | /* There is a space before %c */ 36 | startList=newNode; 37 | temp1=newNode; 38 | newNode->vrt=NULL; 39 | newNode->edg=NULL; 40 | newNode->marked=N; 41 | } 42 | else 43 | { 44 | newNode=malloc(sizeof (struct node)); 45 | scanf (" %c",&newNode->nme); 46 | /* There is a space before %c */ 47 | newNode->vrt=NULL; 48 | newNode->edg=NULL; 49 | newNode->marked=N; 50 | temp1->vrt=newNode; 51 | temp1=newNode; 52 | } 53 | } 54 | printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n"); 55 | printf("between v1 and v2. Enter 0 0 if over\n"); 56 | noe=numb*(numb-1); 57 | for(j=1;j<=noe;j++) 58 | { 59 | scanf(" %c %c",&v1,&v2); 60 | /* There is a space before %c */ 61 | if(v1=='0' && v2=='0')break; 62 | temp1=startList; 63 | while(temp1!=NULL && temp1->nme!=v1) 64 | temp1=temp1->vrt; 65 | if(temp1==NULL) 66 | { 67 | printf("Sorry no vertex exist by this name\n"); 68 | break; 69 | } 70 | temp2=temp1; 71 | while(temp2->edg!=NULL)temp2=temp2->edg; 72 | newNode=malloc(sizeof (struct node)); 73 | newNode->nme=v2; 74 | temp2->edg=newNode; 75 | newNode->edg=NULL; 76 | newNode->vrt=NULL; 77 | } 78 | printf ("\nAdjacency List representation of Graph is\n"); 79 | temp1=startList; 80 | while (temp1!=NULL) 81 | { 82 | printf ("%c\t",temp1->nme); 83 | temp2=temp1->edg; 84 | while(temp2 !=NULL) 85 | { 86 | printf("%c\t",temp2->nme); 87 | temp2=temp2->edg; 88 | } 89 | printf("\n"); 90 | temp1=temp1->vrt; 91 | } 92 | printf("\nBreadth First traversal of the graph is \n"); 93 | temp1=startList; 94 | if(temp1==NULL) 95 | printf("Sorry no vertices in the graph\n"); 96 | else 97 | queue(temp1); 98 | while(rear !=-1) 99 | { 100 | temp3=dequeue(); 101 | temp1=startList; 102 | while(temp1->nme !=temp3->nme)temp1=temp1->vrt; 103 | temp3=temp1; 104 | if(temp3->marked==N) 105 | { 106 | printf("%c\t",temp3->nme); 107 | temp3->marked=Y; 108 | temp2=temp3->edg; 109 | while(temp2!=NULL) 110 | { 111 | queue(temp2); 112 | temp2=temp2->edg; 113 | } 114 | } 115 | } 116 | return 0; 117 | } 118 | 119 | void queue(struct node *paramNode) 120 | { 121 | rear++; 122 | que[rear]=paramNode; 123 | if(front==-1)front=0; 124 | } 125 | 126 | struct node *dequeue() 127 | { 128 | struct node *tempNode; 129 | if(front==rear) 130 | { 131 | tempNode=que[front]; 132 | front=-1; 133 | rear=-1; 134 | } 135 | else 136 | { 137 | tempNode=que[front]; 138 | front++; 139 | } 140 | return(tempNode); 141 | } 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /Chapter10/depthfirsttrav.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define max 20 4 | enum Setmarked{Y,N}; 5 | struct node 6 | { 7 | char nme; 8 | struct node *vrt; 9 | struct node *edg; 10 | enum Setmarked marked; 11 | }; 12 | 13 | struct node *stack[max]; 14 | int top=-1; 15 | void push(struct node *h); 16 | struct node *pop(); 17 | 18 | int main() 19 | { 20 | int numb,i,j,noe; 21 | char v1,v2; 22 | struct node *startList,*newNode,*temp1,*temp2, *temp3; 23 | printf ("How many vertices are there ?"); 24 | scanf("%d",&numb); 25 | startList=NULL; 26 | printf("Enter all vertices names\n"); 27 | for(i=1;i<=numb;i++) 28 | { 29 | if (startList==NULL) 30 | { 31 | newNode =malloc(sizeof (struct node)); 32 | scanf (" %c",&newNode->nme); 33 | /* There is a white space before %c */ 34 | startList=newNode; 35 | temp1=newNode; 36 | newNode->vrt=NULL; 37 | newNode->edg=NULL; 38 | newNode->marked=N; 39 | } 40 | else 41 | { 42 | newNode=malloc(sizeof (struct node)); 43 | scanf (" %c",&newNode->nme); 44 | /* There is a white space before %c */ 45 | newNode->vrt=NULL; 46 | newNode->edg=NULL; 47 | newNode->marked=N; 48 | temp1->vrt=newNode; 49 | temp1=newNode; 50 | } 51 | } 52 | printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n"); 53 | printf("between v1 and v2. Enter 0 0 if over\n"); 54 | noe=numb*(numb-1); 55 | for(j=1;j<=noe;j++) 56 | { 57 | scanf(" %c %c",&v1,&v2); 58 | /* There is a white space before %c */ 59 | if(v1=='0' && v2=='0')break; 60 | temp1=startList; 61 | while(temp1!=NULL && temp1->nme!=v1) 62 | temp1=temp1->vrt; 63 | if(temp1==NULL) 64 | { 65 | printf("Sorry no vertex exist by this name\n"); 66 | break; 67 | } 68 | temp2=temp1; 69 | while(temp2->edg!=NULL)temp2=temp2->edg; 70 | newNode=malloc(sizeof (struct node)); 71 | newNode->nme=v2; 72 | temp2->edg=newNode; 73 | newNode->edg=NULL; 74 | newNode->vrt=NULL; 75 | } 76 | printf ("\nAdjacency List representation of Graph is\n"); 77 | temp1=startList; 78 | while (temp1!=NULL) 79 | { 80 | printf ("%c\t",temp1->nme); 81 | temp2=temp1->edg; 82 | while(temp2!=NULL) 83 | { 84 | printf("%c\t",temp2->nme); 85 | temp2=temp2->edg; 86 | } 87 | printf("\n"); 88 | temp1=temp1->vrt; 89 | } 90 | printf("\nDepth First traversal of the graph is \n"); 91 | temp1=startList; 92 | if(temp1==NULL) 93 | printf("Sorry no vertices in the graph\n"); 94 | else 95 | push(temp1); 96 | while(top >=0) 97 | { 98 | temp3=pop(); 99 | temp1=startList; 100 | while(temp1->nme !=temp3->nme)temp1=temp1->vrt; 101 | temp3=temp1; 102 | if(temp3->marked==N) 103 | { 104 | printf("%c\t",temp3->nme); 105 | temp3->marked=Y; 106 | temp2=temp3->edg; 107 | while(temp2!=NULL) 108 | { 109 | push(temp2); 110 | temp2=temp2->edg; 111 | } 112 | } 113 | } 114 | return 0; 115 | } 116 | 117 | void push(struct node *h) 118 | { 119 | top++; 120 | stack[top]=h; 121 | } 122 | 123 | struct node *pop() 124 | { 125 | return(stack[top--]); 126 | } -------------------------------------------------------------------------------- /Chapter10/kruskal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define max 20 4 | 5 | struct node 6 | { 7 | int nme; 8 | int wt; 9 | struct node *v; 10 | struct node *e; 11 | }; 12 | 13 | typedef struct lst 14 | { 15 | int u,v; 16 | int wt; 17 | struct lst *nxt; 18 | }lst; 19 | 20 | lst *pq=NULL; 21 | lst *tr=NULL; 22 | void addpqu(int a, int b, int w); 23 | void maketree(); 24 | void disptree(); 25 | lst *delet(); 26 | int parent[max]; 27 | 28 | int main() 29 | { 30 | int n,i,j,noe,w; 31 | int a,b; 32 | struct node *adj,*newNode,*p,*q; 33 | printf ("How many vertices are there ? "); 34 | scanf("%d",&n); 35 | for(i=1;i<=n;i++)parent[i]=0; 36 | printf("The vertices are named\n"); 37 | for(i=1;i<=n;i++)printf("%d\t",i); 38 | printf("for convenience \n"); 39 | for(i=1;i<=n;i++) 40 | { 41 | if (i==1) 42 | { 43 | newNode =malloc(sizeof (struct node)); 44 | newNode->nme=i; 45 | adj=newNode; 46 | p=newNode; 47 | newNode->v=NULL; 48 | newNode->e=NULL; 49 | } 50 | else 51 | { 52 | newNode=malloc(sizeof (struct node)); 53 | newNode->nme=i; 54 | newNode->v=NULL; 55 | newNode->e=NULL; 56 | p->v=newNode; 57 | p=newNode; 58 | } 59 | } 60 | printf("Enter the edges between vertices. Enter 1 3, if there is an edge\n"); 61 | printf("between 1 and 3. Enter 0 0 if over\n"); 62 | noe=n*(n-1); 63 | for(j=1;j<=noe;j++) 64 | { 65 | printf("Enter edge: "); 66 | scanf("%d %d",&a,&b); 67 | if(a==0 && b==0)break; 68 | printf("Enter weight: "); 69 | scanf("%d",&w); 70 | p=adj; 71 | while(p!=NULL && p->nme!=a) 72 | p=p->v; 73 | if(p==NULL) 74 | { 75 | printf("Sorry no vertex exist by this name\n"); 76 | break; 77 | } 78 | q=p; 79 | while(q->e!=NULL)q=q->e; 80 | newNode=malloc(sizeof (struct node)); 81 | newNode->nme=b; 82 | newNode->wt=w; 83 | q->e=newNode; 84 | newNode->e=NULL; 85 | newNode->v=NULL; 86 | addpqu(a,b,w); 87 | } 88 | printf ("Adjacency List representation of Graph is\n"); 89 | p=adj; 90 | while (p!=NULL) 91 | { 92 | printf ("%d\t",p->nme); 93 | q=p->e; 94 | while(q!=NULL) 95 | { 96 | printf("%d\t",q->nme); 97 | q=q->e; 98 | } 99 | printf("\n"); 100 | p=p->v; 101 | } 102 | maketree(); 103 | disptree(); 104 | return 0; 105 | } 106 | 107 | void addpqu(int a, int b, int w) 108 | { 109 | lst *newNode,*k,*h; 110 | newNode=(lst *)malloc(sizeof(lst)); 111 | newNode->u=a; 112 | newNode->v=b; 113 | newNode->wt=w; 114 | newNode->nxt=NULL; 115 | if(pq==NULL) 116 | pq = newNode; 117 | else 118 | { 119 | if(newNode->wt < pq->wt) 120 | { 121 | newNode->nxt=pq; 122 | pq=newNode; 123 | } 124 | else 125 | { 126 | k=pq; 127 | while((k!=NULL) &&(k->wt <= newNode->wt)) 128 | { 129 | h=k; 130 | k=k->nxt; 131 | } 132 | h->nxt=newNode; 133 | newNode->nxt=k; 134 | } 135 | } 136 | } 137 | 138 | lst *delet() 139 | { 140 | lst *q; 141 | if (pq !=NULL) 142 | { 143 | q=pq; 144 | pq=pq->nxt; 145 | return q; 146 | } 147 | else 148 | return NULL; 149 | } 150 | 151 | void maketree() 152 | { 153 | lst *newNode,*p; 154 | int x,y,r1,r2; 155 | newNode=delet(); 156 | while(newNode !=NULL) 157 | { 158 | newNode->nxt=NULL; 159 | x=newNode->u; 160 | y=newNode->v; 161 | while(x>0) 162 | { 163 | r1=x; 164 | x=parent[x]; 165 | } 166 | while(y>0) 167 | { 168 | r2=y; 169 | y=parent[y]; 170 | } 171 | if(r1 !=r2) 172 | { 173 | parent[r2]=r1; 174 | if (tr==NULL) 175 | { 176 | tr=newNode; 177 | p=tr; 178 | } 179 | else 180 | { 181 | p->nxt=newNode; 182 | p=newNode; 183 | } 184 | } 185 | newNode=delet(); 186 | } 187 | } 188 | 189 | void disptree() 190 | { 191 | lst *t; 192 | t=tr; 193 | printf("Minimal Spanning tree with Kruskal Algorithm is \n"); 194 | while(t!=NULL) 195 | { 196 | printf("%d %d\n",t->u,t->v); 197 | t=t->nxt; 198 | } 199 | } -------------------------------------------------------------------------------- /Chapter10/prims.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define max 20 4 | 5 | struct node 6 | { 7 | int nme; 8 | int wt; 9 | struct node *vrt; 10 | struct node *edg; 11 | }; 12 | struct node *startList; 13 | 14 | struct lst 15 | { 16 | int u,v; 17 | int wt; 18 | struct lst *next; 19 | }lst; 20 | 21 | struct lst *pq=NULL; 22 | struct lst *tr=NULL; 23 | void addpqu(int a, int b, int w); 24 | void maketree(); 25 | void disptree(); 26 | struct lst *delet(); 27 | int visited[max]; 28 | int n,nov=0; 29 | 30 | int main() 31 | { 32 | int i,j,noe,w; 33 | int a,b; 34 | struct node *newNode,*temp1,*temp2; 35 | printf ("How many vertices are there ?"); 36 | scanf("%d",&n); 37 | printf("The vertices are named\n"); 38 | for(i=1;i<=n;i++)printf("%d\t",i); 39 | printf("for convenience \n"); 40 | startList=NULL; 41 | for(i=1;i<=n;i++) 42 | { 43 | if (startList==NULL) 44 | { 45 | newNode =malloc(sizeof (struct node)); 46 | newNode->nme=i; 47 | startList=newNode; 48 | temp1=newNode; 49 | newNode->vrt=NULL; 50 | newNode->edg=NULL; 51 | } 52 | else 53 | { 54 | newNode=malloc(sizeof (struct node)); 55 | newNode->nme=i; 56 | newNode->vrt=NULL; 57 | newNode->edg=NULL; 58 | temp1->vrt=newNode; 59 | temp1=newNode; 60 | } 61 | } 62 | printf("Enter the edges between vertices. Enter 1 3, if there is an edge\n"); 63 | printf("between 1 and 3. Enter 0 0 if over\n"); 64 | noe=n*(n-1); 65 | for(j=1;j<=noe;j++) 66 | { 67 | printf("Enter edge "); 68 | scanf("%d %d",&a,&b); 69 | if(a==0 && b==0)break; 70 | printf("Enter weight "); 71 | scanf("%d",&w); 72 | temp1=startList; 73 | while(temp1!=NULL && temp1->nme!=a) 74 | { 75 | temp1=temp1->vrt; 76 | } 77 | if(temp1==NULL) 78 | { 79 | printf("Sorry no vertex exist by this name\n"); 80 | break; 81 | } 82 | temp2=temp1; 83 | while(temp2->edg!=NULL)temp2=temp2->edg; 84 | newNode=malloc(sizeof (struct node)); 85 | newNode->nme=b; 86 | newNode->wt=w; 87 | temp2->edg=newNode; 88 | newNode->edg=NULL; 89 | newNode->vrt=NULL; 90 | temp1=startList; 91 | while(temp1!=NULL && temp1->nme!=b) 92 | temp1=temp1->vrt; 93 | if(temp1==NULL) 94 | { 95 | printf("Sorry no vertex exist by this name\n"); 96 | break; 97 | } 98 | temp2=temp1; 99 | while(temp2->edg!=NULL)temp2=temp2->edg; 100 | newNode=malloc(sizeof (struct node)); 101 | newNode->nme=a; 102 | newNode->wt=w; 103 | temp2->edg=newNode; 104 | newNode->edg=NULL; 105 | newNode->vrt=NULL; 106 | 107 | } 108 | printf ("Adjacency List representation of Graph is\n"); 109 | temp1=startList; 110 | while (temp1!=NULL) 111 | { 112 | printf ("%d\t",temp1->nme); 113 | temp2=temp1->edg; 114 | while(temp2!=NULL) 115 | { 116 | printf("%d\t",temp2->nme); 117 | temp2=temp2->edg; 118 | } 119 | printf("\n"); 120 | temp1=temp1->vrt; 121 | } 122 | temp1=startList; 123 | temp2=temp1->edg; 124 | while(temp2!=NULL) 125 | { 126 | addpqu(temp1->nme,temp2->nme, temp2->wt); 127 | temp2=temp2->edg; 128 | } 129 | maketree(); 130 | disptree(); 131 | return 0; 132 | } 133 | 134 | void addpqu(int a, int b, int w) 135 | { 136 | struct lst *lstNode,*findloc1,*findloc2; 137 | lstNode=malloc(sizeof(struct lst)); 138 | lstNode->u=a; 139 | lstNode->v=b; 140 | lstNode->wt=w; 141 | lstNode->next=NULL; 142 | if(pq==NULL) 143 | { 144 | pq = lstNode; 145 | } 146 | else 147 | { 148 | if(lstNode->wt < pq->wt) 149 | { 150 | lstNode->next=pq; 151 | pq=lstNode; 152 | } 153 | else 154 | { 155 | findloc1=pq; 156 | while((findloc1!=NULL) && (findloc1->wt <= lstNode->wt)) 157 | { 158 | findloc2=findloc1; 159 | findloc1=findloc1->next; 160 | } 161 | findloc2->next=lstNode; 162 | lstNode->next=findloc1; 163 | } 164 | } 165 | } 166 | 167 | struct lst *delet() 168 | { 169 | struct lst *tempNode; 170 | if (pq !=NULL) 171 | { 172 | tempNode=pq; 173 | pq=pq->next; 174 | return tempNode; 175 | } 176 | else 177 | return NULL; 178 | } 179 | 180 | void maketree() 181 | { 182 | struct lst *lstNode,*tempNode1,*tempNode2; 183 | struct node *x,*y; 184 | int i,j; 185 | while(nov u) 191 | { 192 | for(j=1;j<=nov;j++) 193 | if(visited[j]==lstNode->v) goto nxt; 194 | } 195 | } 196 | for(i=1;i<=nov;i++) 197 | if(visited[i]==lstNode->u) goto rpt; 198 | nov++; 199 | visited[nov]=lstNode->u; 200 | rpt: for(i=1;i<=nov;i++) 201 | { 202 | if(visited[i]==lstNode->v) goto rptt; 203 | } 204 | nov++; 205 | visited[nov]=lstNode->v; 206 | rptt: lstNode->next=NULL; 207 | if (tr==NULL) 208 | { 209 | tr=lstNode; 210 | tempNode1=tr; 211 | } 212 | else 213 | { 214 | tempNode1->next=lstNode; 215 | tempNode1=lstNode; 216 | } 217 | x=startList; 218 | while(x->nme!=lstNode->v)x=x->vrt; 219 | y=x->edg; 220 | pq=NULL; 221 | while(y!=NULL) 222 | { 223 | addpqu(x->nme,y->nme, y->wt); 224 | y=y->edg; 225 | } 226 | } 227 | } 228 | 229 | void disptree() 230 | { 231 | struct lst *t; 232 | t=tr; 233 | printf("Minimal Spanning tree with Prims Algorithm is \n"); 234 | while(t!=NULL) 235 | { 236 | printf("%d %d\n",t->u,t->v); 237 | t=t->next; 238 | } 239 | } -------------------------------------------------------------------------------- /Chapter11/binarysearchtree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define max 10 5 | 6 | struct tree 7 | { 8 | int data; 9 | struct tree *right; 10 | struct tree *left; 11 | }; 12 | 13 | void build(int Arr[], int Len); 14 | struct tree *makeroot(int val); 15 | void rightchild(struct tree * rootNode, int val); 16 | void leftchild(struct tree *rootNode, int val); 17 | void travino(struct tree *node); 18 | 19 | int main() 20 | { 21 | int arr[max],i,len; 22 | printf("How many elements are there for making the binary search tree? "); 23 | scanf("%d", &len); 24 | printf("Enter %d elements in array \n",len); 25 | for(i=0;idata ) 42 | { 43 | if(temp->left !=NULL) 44 | { 45 | temp=temp->left; 46 | continue; 47 | } 48 | leftchild(temp,Arr[j]); 49 | } 50 | if(Arr[j] >temp->data) 51 | { 52 | if(temp->right !=NULL) 53 | { 54 | temp=temp->right; 55 | continue; 56 | } 57 | rightchild(temp,Arr[j]); 58 | } 59 | break; 60 | } 61 | } 62 | printf ("Binary Search Tree is created\n"); 63 | printf("The inorder traversal of the tree is as follows:\n"); 64 | travino(rootNode); 65 | } 66 | 67 | struct tree *makeroot(int val) 68 | { 69 | struct tree *rootNode; 70 | rootNode=(struct tree *)malloc(sizeof(struct tree)); 71 | rootNode->data=val; 72 | rootNode->right=NULL; 73 | rootNode->left=NULL; 74 | return rootNode; 75 | } 76 | 77 | void leftchild(struct tree *rootNode, int val) 78 | { 79 | struct tree *newNode; 80 | newNode=(struct tree *)malloc(sizeof(struct tree)); 81 | newNode->data=val; 82 | newNode->left=NULL; 83 | newNode->right=NULL; 84 | rootNode->left=newNode; 85 | } 86 | 87 | void rightchild(struct tree *rootNode, int val) 88 | { 89 | struct tree *newNode; 90 | newNode=(struct tree *)malloc(sizeof(struct tree)); 91 | newNode->data=val; 92 | newNode->left=NULL; 93 | newNode->right=NULL; 94 | rootNode->right=newNode; 95 | } 96 | 97 | void travino(struct tree *node) 98 | { 99 | if (node!=NULL) 100 | { 101 | travino(node->left); 102 | printf ("%d\t",node->data); 103 | travino(node->right); 104 | } 105 | } 106 | 107 | -------------------------------------------------------------------------------- /Chapter11/circularlinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *next; 8 | }; 9 | 10 | struct node *startList=NULL; 11 | 12 | void addlist(struct node **h); 13 | void disp(); 14 | 15 | int main() 16 | { 17 | struct node *newNode; 18 | int n=0,i,k; 19 | while(n!=3) 20 | { 21 | printf("\n1. Adding elements to the circular linked list\n"); 22 | printf("2. Displaying elements of the circular linked list\n"); 23 | printf("3. Quit\n"); 24 | printf("Enter your choice 1/2/3: "); 25 | scanf("%d",&n); 26 | switch(n) 27 | { 28 | case 1: printf("How many values are there "); 29 | scanf("%d",&k); 30 | printf("Enter %d values\n",k); 31 | for(i=1;i<=k;i++) 32 | { 33 | newNode=(struct node *)malloc(sizeof(struct node)); 34 | scanf("%d",&newNode->data); 35 | addlist(&newNode); 36 | } 37 | printf("Values added in Circular Linked List \n"); 38 | break; 39 | case 2: disp(); 40 | break; 41 | } 42 | } 43 | return 0; 44 | } 45 | 46 | void addlist(struct node **NewNode) 47 | { 48 | struct node *temp; 49 | if(startList==NULL) 50 | { 51 | startList=*NewNode; 52 | startList->next=startList; 53 | } 54 | else 55 | { 56 | temp=startList; 57 | while(temp->next !=startList) 58 | temp=temp->next; 59 | temp->next=*NewNode; 60 | temp=*NewNode; 61 | temp->next=startList; 62 | } 63 | } 64 | 65 | void disp() 66 | { 67 | struct node *temp; 68 | if (startList==NULL) 69 | printf("The circular linked list is empty\n"); 70 | else 71 | { 72 | printf("Following are the elements in circular linked list:\n"); 73 | printf("%d\n",startList->data); 74 | temp=startList->next; 75 | while(temp !=startList) 76 | { 77 | printf("%d\n",temp->data); 78 | temp=temp->next; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Chapter11/doublylinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | int data; 8 | struct node *next, *prev; 9 | }; 10 | 11 | struct node *startList, *endList; 12 | void createdoubly(); 13 | void list_lifo(); 14 | void list_fifo(); 15 | 16 | int main() 17 | { 18 | 19 | 20 | int n=0; 21 | while(n!=4) 22 | { 23 | printf("\n1. Creating a doubly linked list\n"); 24 | printf("2. Displaying elements in L.I.F.O. order\n"); 25 | printf("3. Displaying elements in F.I.F.O. order\n"); 26 | printf("4. Quit\n"); 27 | printf("Enter your choice 1/2/3/4: "); 28 | scanf("%d",&n); 29 | switch(n) 30 | { 31 | case 1: createdoubly(); 32 | break; 33 | case 2: list_lifo(); 34 | break; 35 | case 3: list_fifo(); 36 | break; 37 | 38 | } 39 | } 40 | return 0; 41 | } 42 | 43 | void createdoubly() 44 | { 45 | char k[10]; 46 | struct node *newNode; 47 | startList=NULL; 48 | strcpy(k,"yes"); 49 | while(strcmp(k,"yes")==0 || strcmp(k, "Yes")==0) 50 | { 51 | if(startList==NULL) 52 | { 53 | newNode=(struct node *)malloc(sizeof(struct node)); 54 | printf("Enter the value to add: "); 55 | scanf("%d",&newNode->data); 56 | newNode->next=NULL; 57 | newNode->prev=NULL; 58 | startList = newNode; 59 | endList=startList; 60 | } 61 | else 62 | { 63 | newNode=(struct node *)malloc(sizeof(struct node)); 64 | printf("Enter the value to add: "); 65 | scanf("%d",&newNode->data); 66 | newNode->next=NULL; 67 | newNode->prev=endList; 68 | endList->next = newNode; 69 | endList=newNode; 70 | } 71 | printf("Want to add more yes/no? "); 72 | scanf("%s",k); 73 | } 74 | printf("Doubly linked list is created\n"); 75 | } 76 | 77 | void list_lifo() 78 | { 79 | struct node *temp; 80 | temp=endList; 81 | if(temp !=NULL) 82 | { 83 | printf("The elements of the doubly linked list in L.I.F.O. order :\n"); 84 | while(temp!=NULL) 85 | { 86 | printf("%d\n",temp->data); 87 | temp=temp->prev; 88 | } 89 | } 90 | else 91 | printf("The doubly linked list is empty\n"); 92 | } 93 | 94 | void list_fifo() 95 | { 96 | struct node *temp; 97 | temp=startList; 98 | printf("The elements of the doubly linked list in F.I.F.O. order: \n"); 99 | while(temp!=NULL) 100 | { 101 | printf("%d\n",temp->data); 102 | temp=temp->next; 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /Chapter11/postordernonrec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct tree 5 | { 6 | int data; 7 | struct tree *right; 8 | struct tree *left; 9 | }; 10 | 11 | struct stackstruc 12 | { 13 | int valueArray[15]; 14 | struct tree *nodeArray[15]; 15 | }; 16 | 17 | struct stackstruc stack; 18 | int top=-1; 19 | 20 | struct tree *makeroot(int val); 21 | void rightchild(struct tree *rootNode, int val); 22 | void leftchild(struct tree *rootNode, int val); 23 | void nontravpost(struct tree *node); 24 | void pushNode (struct tree *node, int val); 25 | struct tree *popNode(); 26 | int popVal(); 27 | 28 | int main() 29 | { 30 | struct tree *temp, *rootNode; 31 | int val; 32 | printf ("Enter elements of tree and 0 to quit\n"); 33 | scanf("%d",&val); 34 | rootNode=makeroot(val); 35 | scanf("%d",&val); 36 | while(val !=0) 37 | { 38 | temp=rootNode; 39 | while (1) 40 | { 41 | if(val < temp->data ) 42 | { 43 | if(temp->left !=NULL) 44 | { 45 | temp=temp->left; 46 | continue; 47 | } 48 | leftchild(temp,val); 49 | } 50 | if(val >temp->data) 51 | { 52 | if(temp->right !=NULL) 53 | { 54 | temp=temp->right; 55 | continue; 56 | } 57 | rightchild(temp,val); 58 | } 59 | break; 60 | } 61 | scanf("%d",&val); 62 | } 63 | printf ("\nTraversal of tree in Postorder without using recursion: \n"); 64 | nontravpost(rootNode); 65 | } 66 | 67 | struct tree *makeroot(int val) 68 | { 69 | struct tree *rootNode; 70 | rootNode=(struct tree *)malloc(sizeof(struct tree)); 71 | rootNode->data=val; 72 | rootNode->right=NULL; 73 | rootNode->left=NULL; 74 | return rootNode; 75 | } 76 | 77 | void leftchild(struct tree *rootNode, int val) 78 | { 79 | struct tree *newNode; 80 | newNode=(struct tree *)malloc(sizeof(struct tree)); 81 | newNode->data=val; 82 | newNode->left=NULL; 83 | newNode->right=NULL; 84 | rootNode->left=newNode; 85 | } 86 | 87 | void rightchild(struct tree *rootNode, int val) 88 | { 89 | struct tree *newNode; 90 | newNode=(struct tree *)malloc(sizeof(struct tree)); 91 | newNode->data=val; 92 | newNode->left=NULL; 93 | newNode->right=NULL; 94 | rootNode->right=newNode; 95 | } 96 | 97 | void nontravpost(struct tree *node) 98 | { 99 | struct tree *temp; 100 | int val; 101 | temp=node; 102 | while (1) 103 | { 104 | while(temp!=NULL) 105 | { 106 | pushNode(temp,0); 107 | temp=temp->left; 108 | } 109 | while(top >=0) 110 | { 111 | temp=popNode(); 112 | val=popVal(); 113 | if (val==0) 114 | { 115 | if (temp->right !=NULL) 116 | { 117 | pushNode(temp,1); 118 | temp=temp->right; 119 | break; 120 | } 121 | } 122 | printf("%d\n",temp->data); 123 | continue; 124 | } 125 | 126 | if((temp==NULL) || (top <0)) break; 127 | else continue; 128 | } 129 | } 130 | 131 | void pushNode (struct tree *node, int val) 132 | { 133 | top++; 134 | stack.nodeArray[top]=node; 135 | stack.valueArray[top]=val; 136 | } 137 | 138 | struct tree *popNode() 139 | { 140 | return (stack.nodeArray[top]); 141 | } 142 | 143 | int popVal() 144 | { 145 | return(stack.valueArray[top--]); 146 | } 147 | -------------------------------------------------------------------------------- /Chapter11/stacklinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *next; 8 | }; 9 | 10 | void push(struct node *NewNode,struct node **Top); 11 | struct node *pop(struct node **Top); 12 | 13 | int main() 14 | { 15 | struct node *newNode,*top, *recNode; 16 | int n=0; 17 | top=NULL; 18 | while(n!=3) 19 | { 20 | printf("\n1. Pushing an element into the stack\n"); 21 | printf("2. Popping out an element from the stack\n"); 22 | printf("3. Quit\n"); 23 | printf("Enter your choice 1/2/3:"); 24 | scanf("%d",&n); 25 | switch(n) 26 | { 27 | case 1: newNode=(struct node *)malloc(sizeof(struct node)); 28 | printf("Enter the value to push: "); 29 | scanf("%d",&newNode->data); 30 | push(newNode,&top); 31 | printf("Value %d is pushed to stack\n", newNode->data); 32 | break; 33 | case 2: recNode=pop(&top); 34 | if(recNode==NULL)printf("Stack is empty\n"); 35 | else 36 | printf("The value popped is %d\n",recNode->data); 37 | break; 38 | } 39 | } 40 | return 0; 41 | } 42 | 43 | void push(struct node *NewNode,struct node **Top) 44 | { 45 | NewNode->next=*Top; 46 | *Top=NewNode; 47 | } 48 | 49 | struct node *pop(struct node **Top) 50 | { 51 | struct node * temp; 52 | if(*Top==NULL) return(NULL); 53 | else 54 | { 55 | temp=*Top; 56 | (*Top)=(*Top)->next; 57 | return(temp); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Chapter12/ballanim.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define pi 3.142857 5 | 6 | void animball (void) 7 | { 8 | int x,y; 9 | glClearColor(0.0, 0.0, 0.0, 1.0); 10 | glColor3f(0.0, 1.0, 0.0); 11 | glPointSize(1.0); 12 | glMatrixMode(GL_PROJECTION); 13 | glLoadIdentity(); 14 | gluOrtho2D(-350, 350, -350, 350); 15 | for (float j = 0; j < 1000; j += 0.01) 16 | { 17 | glClear(GL_COLOR_BUFFER_BIT); 18 | glBegin(GL_POINTS); 19 | for (int i=0; i <360; i++) 20 | { 21 | x = 100 * cos(i); 22 | y = 100 * sin(i); 23 | glVertex2i(x / 2 - 1 * cos(j), y / 2 - 150* sin(j)); 24 | } 25 | glEnd(); 26 | glFlush(); 27 | } 28 | } 29 | 30 | int main (int argc, char** argv) 31 | { 32 | glutInit(&argc, argv); 33 | glutCreateWindow("Animating a ball"); 34 | glutInitWindowSize(1000, 1000); 35 | glutInitWindowPosition(0, 0); 36 | glutDisplayFunc(animball); 37 | glutMainLoop(); 38 | } -------------------------------------------------------------------------------- /Chapter12/opengldrawbar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void display(){ 4 | float x,y,width, result[] = {10.0, 15.0, 5.0}; 5 | int i, barCount = 3; 6 | x=1.0; 7 | y = 0.0; 8 | width = 2.0; 9 | glColor3f(1.0, 0.0, 0.0); 10 | glClearColor(1.0, 1.0, 1.0, 1.0); 11 | gluOrtho2D(-5, 20, -5, 20); 12 | glBegin(GL_LINES); 13 | glVertex2f(-30, 0.0); 14 | glVertex2f(30, 0.0); 15 | glVertex2f(0.0, -30); 16 | glVertex2f(0.0, 30); 17 | glEnd(); 18 | for(i=0; i 2 | 3 | void drawshapes() { 4 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 5 | glClear(GL_COLOR_BUFFER_BIT); 6 | 7 | glBegin(GL_QUADS); 8 | glColor3f(0.0f, 0.0f, 1.0f); 9 | glVertex2f(0.0f, 0.0f); 10 | glVertex2f( 0.0f, .75f); 11 | glVertex2f( -.75f, .75f); 12 | glVertex2f(-.75f, 0.0f); 13 | glEnd(); 14 | glLineWidth(2.0); 15 | glColor3f(1.0, 0.0, 0.0); 16 | glBegin(GL_LINES); 17 | glVertex2f(-0.5, -0.5); 18 | glVertex2f(0.5,-0.5); 19 | glEnd(); 20 | glColor3f(1.0, 0.0, 0.0); 21 | glPointSize(3.0); 22 | glBegin(GL_POINTS); 23 | glVertex2f(-.25f, -0.25f); 24 | glVertex2f(0.25f, -0.25f); 25 | glEnd(); 26 | glBegin(GL_TRIANGLES); 27 | glColor3f( 0, 1, 0 ); 28 | glVertex2f( 0,0 ); 29 | glVertex2f( .5,.5 ); 30 | glVertex2f( 1,0); 31 | glEnd(); 32 | glFlush(); 33 | } 34 | 35 | int main(int argc, char** argv) { 36 | glutInit(&argc, argv); 37 | glutCreateWindow("Drawing some shapes"); 38 | glutInitWindowSize(1500, 1500); 39 | glutInitWindowPosition(0, 0); 40 | glutDisplayFunc(drawshapes); 41 | glutMainLoop(); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Chapter12/opengldrawshapes2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define pi 3.142857 4 | 5 | void drawshapes() { 6 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 7 | glClear(GL_COLOR_BUFFER_BIT); 8 | glColor3f(0.0f, 1.0f, 0.0f); 9 | glBegin(GL_LINE_LOOP); 10 | for (int i=0; i <360; i++) 11 | { 12 | float angle = i*pi/180; 13 | glVertex2f(cos(angle)*0.5,sin(angle)*0.5); 14 | } 15 | glEnd(); 16 | glFlush(); 17 | } 18 | 19 | int main(int argc, char** argv) { 20 | glutInit(&argc, argv); 21 | glutCreateWindow("Drawing some shapes"); 22 | glutInitWindowSize(1500, 1500); 23 | glutInitWindowPosition(0, 0); 24 | glutDisplayFunc(drawshapes); 25 | glutMainLoop(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter12/openglmouseclick.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int noOfClicks = 0; 4 | int coord[2][2]; 5 | int leftPressed = 0; 6 | 7 | void drawLine(void) 8 | { 9 | glClearColor(0.0, 0.0, 0.0, 1.0); 10 | glClear(GL_COLOR_BUFFER_BIT); 11 | glBegin(GL_LINES); 12 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | void main() { 7 | MYSQL *conn; 8 | char *server = "127.0.0.1"; 9 | char *user = "root"; 10 | char *password = "Bintu2018$"; 11 | char *database = "ecommerce"; 12 | char emailaddress[30], upassword[30],deliveryaddress[255],sqlquery[255]; 13 | conn = mysql_init(NULL); 14 | if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { 15 | fprintf(stderr, "%s\n", mysql_error(conn)); 16 | exit(1); 17 | } 18 | printf("Enter email address: "); 19 | scanf("%s", emailaddress); 20 | printf("Enter password: "); 21 | scanf("%s", upassword); 22 | printf("Enter address of delivery: "); 23 | getchar(); 24 | gets(deliveryaddress); 25 | strcpy(sqlquery,"INSERT INTO users(email_address, password, address_of_delivery)VALUES (\'"); 26 | strcat(sqlquery,emailaddress); 27 | strcat(sqlquery,"\', \'"); 28 | strcat(sqlquery,upassword); 29 | strcat(sqlquery,"\', \'"); 30 | strcat(sqlquery,deliveryaddress); 31 | strcat(sqlquery,"\')"); 32 | if (mysql_query(conn, sqlquery) != 0) 33 | { 34 | fprintf(stderr, "Row could not be inserted into users table\n"); 35 | exit(1); 36 | } 37 | printf("Row is inserted successfully in users table\n"); 38 | mysql_close(conn); 39 | } -------------------------------------------------------------------------------- /Chapter13/deleteuser.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main() { 7 | MYSQL *conn; 8 | MYSQL_RES *resultset; 9 | MYSQL_ROW row; 10 | char *server = "127.0.0.1"; 11 | char *user = "root"; 12 | char *password = "Bintu2018$"; 13 | char *database = "ecommerce"; 14 | char emailaddress[30], sqlquery[255],k[10]; 15 | conn = mysql_init(NULL); 16 | if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { 17 | fprintf(stderr, "%s\n", mysql_error(conn)); 18 | exit(1); 19 | } 20 | printf("Enter email address of the user to delete: "); 21 | scanf("%s", emailaddress); 22 | strcpy(sqlquery,"SELECT * FROM users where email_address like \'"); 23 | strcat(sqlquery,emailaddress); 24 | strcat(sqlquery,"\'"); 25 | if (mysql_query(conn, sqlquery) != 0) 26 | { 27 | fprintf(stderr, "No row found in the users table with this email address\n"); 28 | exit(1); 29 | } 30 | resultset = mysql_store_result(conn); 31 | if(mysql_num_rows(resultset) >0) 32 | { 33 | printf("The details of the user with this email address are as follows:\n"); 34 | while ((row = mysql_fetch_row(resultset)) != NULL) 35 | { 36 | printf("Email Address: %s \n", row[0]); 37 | printf("Password: %s \n", row[1]); 38 | printf("Address of delivery: %s \n", row[2]); 39 | } 40 | mysql_free_result(resultset); 41 | printf("Are you sure you want to delete this record yes/no: "); 42 | scanf("%s", k); 43 | if(strcmp(k,"yes")==0) 44 | { 45 | strcpy(sqlquery,"Delete from users where email_address like \'"); 46 | strcat(sqlquery,emailaddress); 47 | strcat(sqlquery,"\'"); 48 | if (mysql_query(conn, sqlquery) != 0) 49 | { 50 | fprintf(stderr, "The user account could not be deleted\n"); 51 | exit(1); 52 | } 53 | printf("The user with the given email address is successfully deleted from the users table\n"); 54 | } 55 | } 56 | else 57 | printf("No user found with this email address\n"); 58 | mysql_close(conn); 59 | } -------------------------------------------------------------------------------- /Chapter13/mysql1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main() { 6 | MYSQL *conn; 7 | MYSQL_RES *res; 8 | MYSQL_ROW row; 9 | char *server = "127.0.0.1"; 10 | char *user = "root"; 11 | char *password = "Bintu2018$"; /* replace it with your MySQL root's password */ 12 | char *database = "mysql"; 13 | conn = mysql_init(NULL); 14 | if (!mysql_real_connect(conn, server, 15 | user, password, database, 0, NULL, 0)) { 16 | fprintf(stderr, "%s\n", mysql_error(conn)); 17 | exit(1); 18 | } 19 | 20 | if (mysql_query(conn, "show tables")) { 21 | fprintf(stderr, "%s\n", mysql_error(conn)); 22 | exit(1); 23 | } 24 | res = mysql_use_result(conn); 25 | printf("MySQL Tables in mysql database:\n"); 26 | while ((row = mysql_fetch_row(res)) != NULL) 27 | printf("%s \n", row[0]); 28 | 29 | mysql_free_result(res); 30 | mysql_close(conn); 31 | } -------------------------------------------------------------------------------- /Chapter13/searchuser.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main() { 7 | MYSQL *conn; 8 | MYSQL_RES *resultset; 9 | MYSQL_ROW row; 10 | char *server = "127.0.0.1"; 11 | char *user = "root"; 12 | char *password = "Bintu2018$"; 13 | char *database = "ecommerce"; 14 | char emailaddress[30], sqlquery[255]; 15 | conn = mysql_init(NULL); 16 | if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { 17 | fprintf(stderr, "%s\n", mysql_error(conn)); 18 | exit(1); 19 | } 20 | printf("Enter email address to search: "); 21 | scanf("%s", emailaddress); 22 | strcpy(sqlquery,"SELECT * FROM users where email_address like \'"); 23 | strcat(sqlquery,emailaddress); 24 | strcat(sqlquery,"\'"); 25 | if (mysql_query(conn, sqlquery) != 0) 26 | { 27 | fprintf(stderr, "No row found in the users table with this email address\n"); 28 | exit(1); 29 | } 30 | printf("The details of the user with this email address are as follows:\n"); 31 | resultset = mysql_use_result(conn); 32 | while ((row = mysql_fetch_row(resultset)) != NULL) 33 | { 34 | printf("Email Address: %s \n", row[0]); 35 | printf("Password: %s \n", row[1]); 36 | printf("Address of delivery: %s \n", row[2]); 37 | } 38 | mysql_free_result(resultset); 39 | mysql_close(conn); 40 | } -------------------------------------------------------------------------------- /Chapter13/updateuser.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main() { 7 | MYSQL *conn; 8 | MYSQL_RES *resultset; 9 | MYSQL_ROW row; 10 | char *server = "127.0.0.1"; 11 | char *user = "root"; 12 | char *password = "Bintu2018$"; 13 | char *database = "ecommerce"; 14 | char emailaddress[30], sqlquery[255], upassword[30],deliveryaddress[255]; 15 | conn = mysql_init(NULL); 16 | if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { 17 | fprintf(stderr, "%s\n", mysql_error(conn)); 18 | exit(1); 19 | } 20 | printf("Enter email address of the user to update: "); 21 | scanf("%s", emailaddress); 22 | strcpy(sqlquery,"SELECT * FROM users where email_address like \'"); 23 | strcat(sqlquery,emailaddress); 24 | strcat(sqlquery,"\'"); 25 | if (mysql_query(conn, sqlquery) != 0) 26 | { 27 | fprintf(stderr, "No row found in the users table with this email address\n"); 28 | exit(1); 29 | } 30 | resultset = mysql_store_result(conn); 31 | if(mysql_num_rows(resultset) >0) 32 | { 33 | printf("The details of the user with this email address are as follows:\n"); 34 | while ((row = mysql_fetch_row(resultset)) != NULL) 35 | { 36 | printf("Email Address: %s \n", row[0]); 37 | printf("Password: %s \n", row[1]); 38 | printf("Address of delivery: %s \n", row[2]); 39 | } 40 | mysql_free_result(resultset); 41 | printf("Enter new password: "); 42 | scanf("%s", upassword); 43 | printf("Enter new address of delivery: "); 44 | getchar(); 45 | gets(deliveryaddress); 46 | strcpy(sqlquery,"UPDATE users set password=\'"); 47 | strcat(sqlquery,upassword); 48 | strcat(sqlquery,"\', address_of_delivery=\'"); 49 | strcat(sqlquery,deliveryaddress); 50 | strcat(sqlquery,"\' where email_address like \'"); 51 | strcat(sqlquery,emailaddress); 52 | strcat(sqlquery,"\'"); 53 | if (mysql_query(conn, sqlquery) != 0) 54 | { 55 | fprintf(stderr, "The desired row in users table could not be updated\n"); 56 | exit(1); 57 | } 58 | printf("The information of user is updated successfully in users table\n"); 59 | } 60 | else 61 | printf("No user found with this email address\n"); 62 | mysql_close(conn); 63 | } -------------------------------------------------------------------------------- /Chapter14/atexistprog1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *str; 5 | void freeup() 6 | { 7 | free(str); 8 | printf( "Allocated memory is freed \n"); 9 | } 10 | 11 | int main() 12 | { 13 | int retvalue; 14 | retvalue = atexit(freeup); 15 | if (retvalue != 0) { 16 | printf("Registration of function for atexit () function failed\n"); 17 | exit(1); 18 | } 19 | str = malloc( 20 * sizeof(char) ); 20 | if( str== NULL ) 21 | { 22 | printf("Some error occurred in allocating memory\n"); 23 | exit(1); 24 | } 25 | printf("Enter a string "); 26 | scanf("%s", str); 27 | printf("The string entered is %s\n", str); 28 | } -------------------------------------------------------------------------------- /Chapter14/atexistprog2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *str; 5 | void freeup1() 6 | { 7 | free(str); 8 | printf( "Allocated memory is freed \n"); 9 | } 10 | 11 | void freeup2() 12 | { 13 | printf( "The size of dynamic memory can be increased and decreased \n"); 14 | } 15 | 16 | int main() 17 | { 18 | int retvalue; 19 | retvalue = atexit(freeup1); 20 | if (retvalue != 0) { 21 | printf("Registration of function freeup1() for atexit () function failed\n"); 22 | exit(1); 23 | } 24 | retvalue = atexit(freeup2); 25 | if (retvalue != 0) { 26 | printf("Registration of function freeup2() for atexit () function failed\n"); 27 | exit(1); 28 | } 29 | str = malloc( 20 * sizeof(char) ); 30 | if( str== NULL ) 31 | { 32 | printf("Some error occurred in allocating memory\n"); 33 | exit(1); 34 | } 35 | printf("Enter a string "); 36 | scanf("%s", str); 37 | printf("The string entered is %s\n", str); 38 | 39 | } -------------------------------------------------------------------------------- /Chapter14/dynamicmem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int* ptr; 7 | int m,n, i; 8 | 9 | printf("How many elements are there? "); 10 | scanf("%d", &n); 11 | ptr = (int*)calloc(n, sizeof(int)); 12 | if (ptr == NULL) { 13 | printf("Memory could not be allocated.\n"); 14 | exit(0); 15 | } 16 | printf("Enter %d elements \n", n); 17 | for (i = 0; i < n; ++i) 18 | scanf("%d",&ptr[i]); 19 | printf("\nThe elements entered are: \n"); 20 | for (i = 0; i < n; ++i) 21 | printf("%d\n", ptr[i]); 22 | printf("\nHow many elements you want to add more? "); 23 | scanf("%d",&m); 24 | ptr = realloc(ptr, (m+n) * sizeof(int)); 25 | printf("Enter values for %d elements\n",m); 26 | for (i = n; i < (m+n); ++i) 27 | scanf("%d",&ptr[i]); 28 | printf("\nThe complete set of elements now are: \n"); 29 | for (i = 0; i < (m+n); ++i) 30 | printf("%d\n", ptr[i]); 31 | printf("\nHow many elements you want to keep ? "); 32 | scanf("%d", &m); 33 | ptr = realloc(ptr, (m) * sizeof(int)); 34 | printf("\nThe new set of elements now are: \n"); 35 | for (i = 0; i < m; ++i) 36 | printf("%d\n", ptr[i]); 37 | free(ptr); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter14/signalhandling.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void sighandler1(int signum) { 7 | printf("Ctrl + C is auto pressed \n"); 8 | } 9 | 10 | void sighandler2(int signum) { 11 | printf("You have pressed Ctrl+c\n"); 12 | printf("Press Ctrl+c again to exit\n"); 13 | (void) signal(SIGINT, SIG_DFL); 14 | } 15 | 16 | int main () { 17 | int x=1; 18 | signal(SIGINT, sighandler1); 19 | while(x<=5) { 20 | printf("Signal will be raised automatically after 5 seconds\n"); 21 | x++; 22 | sleep(1); 23 | } 24 | raise(SIGINT); 25 | signal(SIGINT, sighandler2); 26 | while(1) { 27 | printf("Infinite loop, press Ctrl+C to raise signal\n"); 28 | sleep(1); 29 | } 30 | return(0); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter14/timecalc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void somefunction() 5 | { 6 | for (int i=0; i<32000; i++) 7 | { 8 | for (int j=0; j<32000; j++) ; 9 | } 10 | } 11 | 12 | int main() 13 | { 14 | clock_t clocktickstart, clocktickend; 15 | double timeconsumed; 16 | clocktickstart = clock(); 17 | somefunction(); 18 | clocktickend = clock(); 19 | timeconsumed = (double)(clocktickend - clocktickstart) / CLOCKS_PER_SEC; 20 | printf("Number of clocks ticks required in running the function is : %.3f\n", (double)(clocktickend - clocktickstart)); 21 | printf("Time taken by program is : %.2f sec\n", timeconsumed); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Chapter15/fastinp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int getdata() { 4 | char cdigit = getchar_unlocked(); 5 | int cnumb = 0; 6 | while(cdigit<'0' || cdigit>'9') cdigit = getchar_unlocked(); 7 | while(cdigit >='0' && cdigit <='9') { 8 | cnumb = 10 * cnumb + cdigit - 48; 9 | cdigit = getchar_unlocked(); 10 | } 11 | return cnumb; 12 | } 13 | 14 | int main() 15 | { 16 | int numb; 17 | printf("Enter a number "); 18 | numb=getdata(); 19 | printf("The number entered is %d\n",numb); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter15/loopunrolling.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int sum,i,limit,rem,quot,incr,x, count; 5 | sum = 0; 6 | printf("Enter limit "); 7 | scanf("%d", &limit); 8 | for(i=9;i>=1;i--) 9 | { 10 | rem=limit % i; 11 | if (rem==0) break; 12 | } 13 | incr=i; 14 | count=0; 15 | for(i=1;i<=limit; i+=incr) 16 | { 17 | x=0; 18 | while(x 2 | #include 3 | 4 | int main() { 5 | int distance; 6 | char car_type[20]; 7 | register int Acperkm,Nonacperkm,servicetax; 8 | float carRent, totalrent; 9 | 10 | printf("How many kilometers? "); 11 | scanf("%d", &distance); 12 | printf("AC car or non AC ac/non? "); 13 | scanf("%s", car_type); 14 | Acperkm=3; 15 | Nonacperkm=2; 16 | servicetax=1; 17 | if(strcmp(car_type, "ac")==0) 18 | carRent=distance*Acperkm; 19 | else 20 | carRent=distance*Nonacperkm; 21 | totalrent=carRent + (carRent*servicetax/100); 22 | printf("The total rent for the car will be $ %.2f\n",totalrent); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter16/asmdivide.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | int var1=19,var2=4, var3=0, remainder, quotient; 5 | asm("divl %%ebx;" 6 | "movl %%edx, %0" 7 | : "=b" (remainder) , "=r" (quotient) 8 | : "a" (var1), "b" (var2), "d" (var3) 9 | 10 | ); 11 | printf ("On dividing %d by %d, you get %d quotient and %d remainder\n", var1, var2, quotient, remainder); 12 | } 13 | -------------------------------------------------------------------------------- /Chapter16/binintodec.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int num,bin,temp,dec=0,topower=0; 6 | 7 | printf("Enter the binary number: "); 8 | scanf("%d",&bin); 9 | temp=bin; 10 | while(bin >0) 11 | { 12 | num=bin %10; 13 | num=num< 2 | 3 | void main() 4 | { 5 | int num,i,x,temp;; 6 | int p[10]; 7 | printf("Enter Decimal Number : "); 8 | scanf("%d",&num); 9 | temp=num; 10 | x=0; 11 | while(num > 0) 12 | { 13 | if((num & 1) == 0 ) 14 | { 15 | p[x]=0; 16 | x++; 17 | } 18 | else 19 | { 20 | p[x]=1; 21 | x++; 22 | } 23 | num = num >> 1; 24 | } 25 | printf("Binary of %d is ",temp); 26 | for(i=x-1;i>=0;i--)printf("%d",p[i]); 27 | } -------------------------------------------------------------------------------- /Chapter16/decintobin.c: -------------------------------------------------------------------------------- 1 | /* Converting Decimal to Binary using masking */ 2 | 3 | #include 4 | void main() 5 | { 6 | int i, totbits; 7 | unsigned mask,num; 8 | 9 | printf("Enter decimal value: "); 10 | scanf("%d", &num); 11 | totbits=32; 12 | mask = 1 << (totbits - 1); 13 | for(i = 0; i < totbits; i++) 14 | { 15 | if((num & mask) == 0 ) 16 | printf("0"); 17 | else 18 | printf("1"); 19 | mask >>= 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter16/multiasm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char **argv) 5 | { 6 | int32_t var1=10, var2=20, multi = 0; 7 | asm volatile ("imull %%ebx,%%eax;" 8 | : "=a" (multi) /* output: multi = EAX */ 9 | : "a" (var1), "b" (var2) /* inputs: EAX = var1, EBX = var2 */ 10 | ); 11 | printf("Multiplication = %d\n", multi); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Chapter17/ArduinoLedBlink/ArduinoLedBlink.ino: -------------------------------------------------------------------------------- 1 | int Led = 13; 2 | 3 | void setup() { 4 | pinMode(Led, OUTPUT); 5 | 6 | } 7 | 8 | void loop() { 9 | digitalWrite(Led, HIGH); 10 | delay(1000); 11 | digitalWrite(Led, LOW); 12 | delay(1000); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter17/ArduinoTakinginput/ArduinoTakinginput.ino: -------------------------------------------------------------------------------- 1 | 2 | int Led = 13; 3 | void setup() { 4 | pinMode(Led,OUTPUT); 5 | Serial.begin(9600); 6 | Serial.println("Enter 0 to switch Off LED and 1 to switch it On"); 7 | } 8 | 9 | void loop() { 10 | if(Serial.available()) 11 | { 12 | int input=Serial.read(); 13 | input=input-48; 14 | if(input==0) 15 | { 16 | Serial.println("LED is OFF"); 17 | digitalWrite(Led,LOW); 18 | } 19 | else if(input==1) 20 | { 21 | Serial.println("LED is ON"); 22 | digitalWrite(Led,HIGH); 23 | } 24 | else 25 | { 26 | Serial.println("Enter 0 to switch Off LED and 1 to switch it On"); 27 | } 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Chapter17/SensorApp/SensorApp.ino: -------------------------------------------------------------------------------- 1 | float voltage; 2 | int tempPin = 0; 3 | 4 | void setup() { 5 | Serial.begin(9600); 6 | } 7 | 8 | void loop() { 9 | voltage = analogRead(tempPin); 10 | float tempInCelsius = voltage * 0.48828125; 11 | float tempinFahrenheit = (tempInCelsius*9)/5 + 32; 12 | Serial.print("Temperature in Celsius is: "); 13 | Serial.print(tempInCelsius); 14 | Serial.print("*C"); 15 | Serial.println(); 16 | Serial.print("Temperature in Fahrenheit is: "); 17 | Serial.print(tempinFahrenheit); 18 | Serial.print("*F"); 19 | Serial.println(); 20 | delay(1000); 21 | } 22 | -------------------------------------------------------------------------------- /Chapter17/blinkingLed.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | sbit LED = P1^0; 4 | void Delay(int); 5 | void main (void) 6 | { 7 | while(1) 8 | { 9 | LED = 0; 10 | Delay(500); 11 | LED = 1; 12 | Delay(500); 13 | } 14 | } 15 | 16 | void Delay(int n) 17 | { 18 | int i,j; 19 | for(i=0;i 2 | #include 3 | 4 | void delay(void); 5 | void main() 6 | { 7 | unsigned char i; 8 | i=0x00; 9 | while(++i) 10 | { 11 | P3=i; 12 | delay(); 13 | } 14 | } 15 | 16 | void delay(void) 17 | { 18 | int j; 19 | int i; 20 | for(i=0;i<1000;i++) 21 | { 22 | for(j=0;j<10000;j++) 23 | { 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter18/fileproblem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define BUFFSIZE 255 7 | 8 | void main (int argc, char* argv[]) 9 | { 10 | FILE *fp; 11 | char str[BUFFSIZE]; 12 | if (symlink("file2.txt","file1.txt") != 0) { 13 | perror("symlink() error"); 14 | unlink("file2.txt"); 15 | exit(1); 16 | } 17 | else 18 | { 19 | fp = fopen ("file1.txt","w"); 20 | if (fp == NULL) { 21 | perror ("An error occurred in creating the file\n"); 22 | exit(1); 23 | } 24 | printf("Enter content for the file\n"); 25 | gets(str); 26 | while(strcmp(str, "stop") !=0){ 27 | fputs(str,fp); 28 | gets(str); 29 | } 30 | } 31 | fclose(fp); 32 | } 33 | -------------------------------------------------------------------------------- /Chapter18/filesolved.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define BUFFSIZE 255 8 | 9 | void main (int argc, char* argv[]) 10 | { 11 | int ifp; 12 | FILE *fp; 13 | char str[BUFFSIZE]; 14 | if (symlink("file2.txt","file1.txt") != 0) { 15 | perror("symlink() error"); 16 | unlink("file2.txt"); 17 | exit(1); 18 | } 19 | else 20 | { 21 | unlink("file1.txt"); 22 | ifp = open("file1.txt", O_WRONLY | O_CREAT | O_EXCL, 0600); 23 | if (ifp == -1) { 24 | perror("An error occurred in creating the file\n"); 25 | exit(1); 26 | } 27 | fp = fdopen(ifp, "w"); 28 | if (fp == NULL) { 29 | perror ("Could not be linked to the stream\n"); 30 | exit(1); 31 | } 32 | printf("Enter content for the file\n"); 33 | gets(str); 34 | while(strcmp(str, "stop") !=0){ 35 | fputs(str,fp); 36 | gets(str); 37 | } 38 | } 39 | fclose(fp); 40 | } 41 | -------------------------------------------------------------------------------- /Chapter18/getsproblem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct users 4 | { 5 | char name[10]; 6 | int orderid; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct users user1, user2; 12 | printf("Enter order number "); 13 | scanf("%d", &user1.orderid); 14 | fpurge(stdin); 15 | printf ("Enter first user name "); 16 | gets(user1.name); 17 | printf("Enter order number "); 18 | scanf("%d", &user2.orderid); 19 | fpurge(stdin); 20 | printf ("Enter second user name "); 21 | gets(user2.name); 22 | printf("Information of first user - Name %s, Order number %d\n", user1.name, user1.orderid); 23 | printf("Information of second user - Name %s, Order number %d\n", user2.name, user2.orderid); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter18/getssolved.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct users 6 | { 7 | char name[10]; 8 | int orderid; 9 | }; 10 | 11 | int main(void) 12 | { 13 | struct users user1; 14 | int n; 15 | printf("Enter order number "); 16 | scanf("%d", &user1.orderid); 17 | fpurge(stdin); 18 | printf ("Enter user name "); 19 | fgets(user1.name, sizeof(user1.name), stdin); 20 | n = strlen(user1.name)-1; 21 | if(user1.name[n] == '\n') 22 | user1.name[n] = '\0'; 23 | printf("Information of the user is - Name %s, Order number %d\n", user1.name, user1.orderid); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter18/sprintfproblem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct users 4 | { 5 | char name[10]; 6 | int orderid; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct users user1; 12 | user1.orderid=101; 13 | sprintf(user1.name, "%s","bintuharwani"); 14 | printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter18/sprintfsolved.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct users 4 | { 5 | char name[10]; 6 | int orderid; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct users user1; 12 | user1.orderid=101; 13 | snprintf(user1.name, sizeof(user1.name), "%s","bintuharwani"); 14 | printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); 15 | } 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Chapter18/strcpyproblem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct users 5 | { 6 | char name[10]; 7 | int orderid; 8 | }; 9 | 10 | int main(void) 11 | { 12 | struct users user1; 13 | char userid[]="administrator"; 14 | user1.orderid=101; 15 | strcpy(user1.name, userid); 16 | printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /Chapter18/strcpysolved.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct users 5 | { 6 | char name[10]; 7 | int orderid; 8 | }; 9 | 10 | int main(void) 11 | { 12 | int strsize; 13 | struct users user1; 14 | char userid[]="administrator"; 15 | user1.orderid=101; 16 | strsize=sizeof(user1.name); 17 | strncpy(user1.name, userid,strsize); 18 | if (user1.name[strsize-1] != '\0') 19 | user1.name[strsize-1] = '\0'; 20 | printf("Information of the user - Name %s, Order number %d\n", user1.name, user1.orderid); 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Practical C Programming 5 | 6 | Practical C Programming 7 | 8 | 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. 9 | 10 | **Solutions for modern C developers to create efficient and well-structured programs** 11 | 12 | ## What is this book about? 13 | 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. 14 | 15 | This book covers the following exciting features: 16 | * Discover how to use arrays, functions, and strings to create large apps 17 | * Perform preprocessing and conditional compilation for efficient programming 18 | * Understand how to use pointers and memory optimally 19 | * Use general-purpose utilities and improve the performance of the code 20 | * Implement multitasking using threads and process synchronization 21 | * Explore low-level programming and inline assembly language 22 | * Understand how to use graphics for animation 23 | * Implement security while developing C programs 24 | * Establish communication between two or more processes using different techniques 25 | 26 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1789617456) today! 27 | 28 | https://www.packtpub.com/ 30 | 31 | ## Instructions and Navigations 32 | All of the code is organized into folders. For example, Chapter02. 33 | 34 | The code will look like the following: 35 | ``` 36 | for(i=0;i<2;i++) 37 | { 38 | for(j=0;j<4;j++) 39 | { 40 | matR[i][j]=0; 41 | for(k=0;k<3;k++) 42 | { 43 | matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j]; 44 | } 45 | } 46 | } 47 | ``` 48 | 49 | **Following is what you need for this book:** 50 | 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. 51 | 52 | With the following software and hardware list you can run all code files present in the book (Chapter 1-18). 53 | ### Software and Hardware List 54 | | Chapter | Software required | OS required | 55 | | -------- | ------------------------------------ | ----------------------------------- | 56 | | 1-18 | cygwin | Windows 8 or above | 57 | | 13 | MySQL Community Server | Windows 8 or above | 58 | 59 | 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). 60 | 61 | ### Related products 62 | * 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) 63 | 64 | * 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/) 65 | 66 | ## Get to Know the Author 67 | **B. M. Harwani** 68 | 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. 69 | 70 | ## Other books by the authors 71 | [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) 72 | 73 | ### Suggestions and Feedback 74 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 75 | 76 | 77 | ### Download a free PDF 78 | 79 | 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.
80 |

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

--------------------------------------------------------------------------------