Showing posts with label CRC-CCITT. Show all posts
Showing posts with label CRC-CCITT. Show all posts

C Program to Frame sorting technique used in buffers.

How to write a C Program to Frame sorting technique used in buffers in C Programming Language ?


Solution:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define FRAM_TXT_SIZ 3
  4. #define MAX_NOF_FRAM 127
  5. char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
  6. struct frame // structure maintained to hold frames
  7. { char text[FRAM_TXT_SIZ];
  8. int seq_no;
  9. }fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
  10. int assign_seq_no() //function which splits message
  11. { int k=0,i,j; //into frames and assigns sequence no
  12. for(i=0; i < strlen(str); k++)
  13. { fr[k].seq_no = k;
  14. for(j=0; j < FRAM_TXT_SIZ && str[i]!='\0'; j++)
  15. fr[k].text[j] = str[i++];
  16. }
  17. printf("\nAfter assigning sequence numbers:\n");
  18. for(i=0; i < k; i++)
  19. printf("%d:%s ",i,fr[i].text);
  20. return k; //k gives no of frames
  21. }
  22. void generate(int *random_ary, const int limit) //generate array of random nos
  23. { int r, i=0, j;
  24. while(i < limit)
  25. { r = random() % limit;
  26. for(j=0; j < i; j++)
  27. if( random_ary[j] == r )
  28. break;
  29. if( i==j ) random_ary[i++] = r;
  30. } }
  31. void shuffle( const int no_frames ) // function shuffles the frames
  32. {
  33. int i, k=0, random_ary[no_frames];
  34. generate(random_ary, no_frames);
  35. for(i=0; i < no_frames; i++)
  36. shuf_ary[i] = fr[random_ary[i]];
  37. printf("\n\nAFTER SHUFFLING:\n");
  38. for(i=0; i < no_frames; i++)
  39. printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
  40. }
  41. void sort(const int no_frames) // sorts the frames
  42. {
  43. int i,j,flag=1;
  44. struct frame hold;
  45. for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in sequence
  46. {
  47. flag=0;
  48. for(j=0; j < no_frames-1-i; j++) //(based on seq no.) and display
  49. if(shuf_ary[j].seq_no > shuf_ary[j+1].seq_no)
  50. {
  51. hold = shuf_ary[j];
  52. shuf_ary[j] = shuf_ary[j+1];
  53. shuf_ary[j+1] = hold;
  54. flag=1;
  55. }
  56. }
  57. }
  58. int main()
  59. {
  60. int no_frames,i;
  61. printf("Enter the message: ");
  62. gets(str);
  63. no_frames = assign_seq_no();
  64. shuffle(no_frames);
  65. sort(no_frames);
  66. printf("\n\nAFTER SORTING\n");
  67. for(i=0;i<no_frames;i++)
  68. printf("%s",shuf_ary[i].text);
  69. printf("\n\n");
  70. }

OUTPUT C Program to Frame sorting technique used in buffers.:

[root@localhost nwcn]# ./a.out
Enter the message: Welcome To Acharya Institute of Technology
After assigning sequence numbers:
0:Wel 1:com 2:e T 3:o A 4:cha 5:rya 6: In 7:sti 8:tut 9:e o 10:f T 11:ech 12:nol 13:ogy
AFTER SHUFFLING:
1:com 4:cha 9:e o 5:rya 3:o A 10:f T 2:e T 6: In 11:ech 13:ogy 0:Wel 8:tut 12:nol 7:sti
AFTER SORTING
Welcome To Acharya Institute of Technology

C Program for error detecting code using CRC-CCITT (16bit)

How to write a C Program for error detecting code using CRC-CCITT (16bit) in C Programming Language ?


Solution:

  1. #include<stdio.h>
  2. char m[50],g[50],r[50],q[50],temp[50];
  3. void caltrans(int);
  4. void crc(int);
  5. void calram();
  6. void shiftl();
  7. int main()
  8. {
  9. int n,i=0;
  10. char ch,flag=0;
  11. printf("Enter the frame bits:");
  12. while((ch=getc(stdin))!='\n')
  13. m[i++]=ch;
  14. n=i;
  15. for(i=0;i<16;i++)
  16. m[n++]='0';
  17. m[n]='\0';
  18. printf("Message after appending 16 zeros:%s",m);
  19. for(i=0;i<=16;i++)
  20. g[i]='0';
  21. g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
  22. printf("\ngenerator:%s\n",g);
  23. crc(n);
  24. printf("\n\nquotient:%s",q);
  25. caltrans(n);
  26. printf("\ntransmitted frame:%s",m);
  27. printf("\nEnter transmitted freme:");
  28. scanf("\n%s",m);
  29. printf("CRC checking\n");
  30. crc(n);
  31. printf("\n\nlast remainder:%s",r);
  32. for(i=0;i<16;i++)
  33. if(r[i]!='0')
  34. flag=1;
  35. else
  36. continue;
  37. if(flag==1)
  38. printf("Error during transmission");
  39. else
  40. printf("\n\nReceived freme is correct");
  41. }
  42. void crc(int n)
  43. {
  44. int i,j;
  45. for(i=0;i<n;i++)
  46. temp[i]=m[i];
  47. for(i=0;i<16;i++)
  48. r[i]=m[i];
  49. printf("\nintermediate remainder\n");
  50. for(i=0;i<n-16;i++)
  51. {
  52. if(r[0]=='1')
  53. {
  54. q[i]='1';
  55. calram();
  56. }
  57. else
  58. {
  59. q[i]='0';
  60. shiftl();
  61. }
  62. r[16]=m[17+i];
  63. r[17]='\0';
  64. printf("\nremainder %d:%s",i+1,r);
  65. for(j=0;j<=17;j++)
  66. temp[j]=r[j];
  67. }
  68. q[n-16]='\0';
  69. }
  70. void calram()
  71. {
  72. int i,j;
  73. for(i=1;i<=16;i++)
  74. r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
  75. }
  76. void shiftl()
  77. {
  78. int i;
  79. for(i=1;i<=16;i++)
  80. r[i-1]=r[i];
  81. }
  82. void caltrans(int n)
  83. {
  84. int i,k=0;
  85. for(i=n-16;i<n;i++)
  86. m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
  87. m[i]='\0';
  88. }

OUTPUT:-

[root@localhost nwcn]# vi 1.c
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data
1011
The msg before adding checksum:
10110000000000000000
The checksum calculated:
1011000101101011
The code word is:10111011000101101011
Enter the transmitted code word
10111011000101101011
Received msg:10111011000101101011
The checksum is:0000000000000000
No error in the msg
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data
1101
The msg before adding checksum:
11010000000000000000
The checksum calculated:
1101000110101101
The code word is:11011101000110101101
Enter the transmitted code word
10111101000110101101
Received msg:10111101000110101101
The checksum is:0110000011000110
Error in the msg