Thread: trouble in getting this crc coding right

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    trouble in getting this crc coding right

    hi i am stuck with doing crc coding for c programming
    i ask for user to define the divisor and dataword
    and got the augemented dataword
    but have trouble in divinding the augemented dataword with the divisor and getting the remainder result.i am stuck with the algo there
    here's my coding
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include <conio.h>
    int main()
    {
    int a[20],b[20],c[20],i,k,j,n,s=0,t=0;
    printf ( "Welcome to the CRC Encoder/Decoder!\n");
    printf ( "press n to exit at anytimes\n");
    	 /* do{*/
    	
    		  
    printf("enter size of dataword\n");
    scanf("%d",&n);
    printf("enter dataword to be transfer bit by bit\n");
    for (i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    
    
    }
    printf("your input dataword is\n");
    for (i=0;i<n;i++)
    {
    	
    printf("%d\n",a[i]);
    }
    printf("enter size of divisor\n");
    scanf("%d",&k);
    printf("enter divisor to be transfer bit by bit\n");
    for (j=0;j<k;j++)
    {
    scanf("%d",&b[j]);
    
    
    }
    printf("your input divisor is\n");
    for (j=0;j<k;j++)
    {
    	
    printf("%d\n",b[j]);
    }
    
    
    
    for (i=0;i<(k-1);i++)
    {
    a[n+i]=0;
    }
    printf("the augmented dataword\n");
    for (i=0;i<(k+n-1);i++)
    {
    printf("%d\n",a[i]);
    }
    
    /*i guess this is wrong but i tried to get the remainder by using modulo 2 like ( a ^ b )*/
    for (i=0;i<k;i++)
    {
    c[i]=a[i];
    }
    for (t=0;t<(n-1);t++)
    {
    for (j=1;j<k;j++)
    {
    
    c[j-1]=c[j]^b[j];
    c[j]=a[k+s];
    
    
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    ^ is for xor
    % is for modulus

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    i am in trouble in getting the augmented dataword divided by divisor.it wont work and it will stop running.cna anyone enlighten me here

    Code:
    
    #include <stdio.h>
    #include<stdlib.h>
    #include <conio.h>
    int main()
    {
    
    
    int a[20],b[20],c[20],d[20],i,k,j,p,n,s=0,t=0;
    int msb;
    
    
    printf ( "Welcome to the CRC Encoder/Decoder!\n");
    printf ( "press n to exit at anytimes\n");
    	 /* do{*/
    	
    		  
    printf("enter size of dataword\n");
    scanf("%d",&n);
    printf("enter dataword to be transfer bit by bit\n");
    for (i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    
    
    }
    printf("your input dataword is\n");
    for (i=0;i<n;i++)
    {
    	
    printf("%d\n",a[i]);
    }
    printf("enter size of divisor\n");
    scanf("%d",&k);
    printf("enter divisor to be transfer bit by bit\n");
    for (j=0;j<k;j++)
    {
    scanf("%d",&b[j]);
    
    
    }
    printf("your input divisor is\n");
    for (j=0;j<k;j++)
    {
    	
    printf("%d\n",b[j]);
    }
    
    
    for (i=0;i<(k-1);i++)
    {
    a[n+i]=0;
    }
    printf("the augmented dataword\n");
    
    for (i=0;i<(k+n-1);i++)
    {
    
    printf("%d\n",a[i]);
    }
    
    printf("the division and remainder\n");
    for (i=k;i<(k-1);i++)
    {
    a[n+i]=0;
    }
    
    for( j = 0;j<(k-1);j++)
    {
    for (i=0;i<(k+n-1);i++)
    {
    
    printf("%d\n",a[i]%b[j]);
    }
    }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    i am in trouble in getting the augmented dataword divided by divisor.it wont work and it will stop running.cna anyone enlighten me here

    Well, yeah I can see that's a problem alright... especially since you aren't actually dividing anything anywhere in the entire program.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well thats the part i am stuck,especially with with getting the divisor b[j] to divide with the augemented dataword..
    my for loop is absolutely wrong and i am stuck there
    can anyone show me how to get the leftmost bit position and check whether it is 0 or 1 if 0 use 0000 divider?
    do we have to use a if else statement here also?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How do you expect us to find the error if you don't post the code with the error in it?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well sorry
    now from that part
    the error making nuts is how to get the remainder from the division in crc
    i see the formula is when 1 we use divisor and 0 we use 0000
    here is what i have done
    but somehow not correct
    i taken the last part of the above coding which is divisor and remainder

    Code:
    printf("the resulting remainder of XOR is \n");
    for (j=k-n;j<k;j++){
    for (i=k-n;i<k;i++)
    {
    	if (a[i] != 0) {
    		a[i] = a[i]^b[j];
    	}
    	else 
    		a[i]^=0;
    
    }
    printf("%d\n",a[i]);
    }
    the output is 01101 00 1 01100 110
    now supposing it has to be only remainder of 110..how to get rid of the initial ones?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You get rid of the initial ones by anding it with 111... a[i] &= 7;

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    ok
    can anyone tell why my output is 111 and not 110 as desired
    Code:
    printf("the resulting output of modulus is \n");
    for (j=0;j<k-1;j++){
    for (i=0;i<k-1;i++)
    {
    if (a[i] !=0)
    {
    	a[i]^=b[j];
    
    }
    else
    a[i]^=0;
    
    
    }
    printf("%d\n",a[i]);
    }
    for the a [i]&=7 where do i put it?on the print f?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    nvm.the output i got is nearly exact which is 111 but not 110...
    can you telll me why?

    Code:
    
    #include <stdio.h>
    #include<stdlib.h>
    #include <conio.h>
    int main()
    {
    
    
    int a[20],b[20],c[20],d[20],i,k,g,j,p,n,s=0,t=0;
    int msb;
    
    
    		  
    printf("enter size of dataword\n");
    scanf("%d",&n);
    printf("enter dataword to be transfer bit by bit\n");
    for (i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    
    
    }
    printf("your input dataword is\n");
    for (i=0;i<n;i++)
    {
    	
    printf("%d\n",a[i]);
    }
    printf("enter size of divisor\n");
    scanf("%d",&g);
    printf("enter divisor to be transfer bit by bit\n");
    for (j=0;j<g;j++)
    {
    scanf("%d",&b[j]);
    
    
    }
    printf("your input divisor is\n");
    for (j=0;j<g;j++)
    {
    	
    printf("%d\n",b[j]);
    }
    
    
    for (i=0;i<(g-1);i++)
    {
    a[n+i]=0;
    }
    printf("the augmented dataword\n");
    
    for (i=0;i<(g+n-1);i++)
    {
    
    printf("%d\n",a[i]);
    }
    
    
    printf("the resulting output of modulus is \n");
    for (j=0;j<g-1;j++){
    for (i=0;i<g-1;i++)
    {
    if (a[i] !=0)
    {
        a[i]^=b[j];
     
    }
    else
    a[i]^=0;
     
     
    }
    printf("%d\n",a[i]);
    }
    return 0;
    }
    
    /*
    for( i=0;i<g;i++)
    
    {
    c[i]= a[i];
    }
    for(t=0;t<(n-1);t++)
    {
    	for(j=1;j<g;j++)
    	{
    		c[j-1]=c[j]^b[j];
    		c[j]=a[g+s];
    }
    
    	for ( i =0;i<g;i++)
    		printf("%d",c[i]);
    	printf("\n");
    	s++;
    }
    //last loop
    for(i=0;i<g;i++)
    c[i]=c[i]*b[i];
    
    //print remainder
    
    for(i=0;i<g;i++)
    printf("%d",c[i]);
    
    
    /*
    
    
    printf("the resulting output of modulus is \n");
    for (j=0;j<g-1;j++){
    for (i=0;i<g-1;i++)
    {
    if (a[i] !=0)
    {
    	a[i]^=b[j];
    
    }
    else
    a[i]^=0;
    }
    printf("%d\n",a[i]);
    }
    
    for(i=0;i<n;++i)
    d[i]= a[i];
    for(i=0;i<n;++i)
    {
    if(a[i]==0)
    {
    for(j=i;j<g+i;++j)
    a[j] = a[j]^0;
    }
    else
    {
    a[i] = a[i]^b[0];
    a[i+1]=a[i+1]^b[1];
    a[i+2]=a[i+2]^b[2];
    a[i+3]=a[i+3]^b[3];
    }
    
    }
    *//*
    printf("\n\t The CRC is :");
    for(i=n;i<a;++i)
    printf("%d",a[i]);
    for(i=0;i<g-1;++i)
    d[i]=a[i];
    s=n+a;
    for(i=n;i<s;i++)
    c[i]=a[i];
    printf("\n");
    printf("\n\t The codeword is :");
    for(i=0;i<a;i++)
    printf("%d",c[i]);
    */

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    anyoe???

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Fix your indentation or I wont bother looking at the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ coding
    By rramyaasmi in forum C++ Programming
    Replies: 1
    Last Post: 10-19-2010, 01:02 PM
  2. Coding trouble
    By Guru_ in forum C++ Programming
    Replies: 11
    Last Post: 09-01-2009, 01:56 AM
  3. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. Do you need some help coding?
    By Gustaff in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 05:40 PM

Tags for this Thread