Thread: Simple enough task

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10

    Simple enough task

    Hi all,
    1st time poster.

    I have a fairly simple task that I need to carry out, however I am not terribly good at C programming, so bare with me!!!!
    Basically I need to take in a string of 15 binary bits, from this input I will be adding bits 1,6,11 bits 2,7,12 bits 3,8,13 bits 4,9,14 and bits 5,10,15.
    These will all give me parity which will either be a 0 or a 1. I then have to output the original 15 bits along with the 5 new bits, so my output will be 20 bits long.

    I hope that all makes sense.......so far I have been trying to do it as such........
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    int main()
    {
    	int codeword_array[20];
    	int i=0;
    
    	printf("Welcome to the 5-bit parity code calculator\nPlease enter the 1st bit of the 15 bit codeword now: \n");
    	
    	for (int i>=0; i<16; i++)
    	{
    		int bit_array[i];	
    		scanf("%d", bit_array[i])
    			if bit !=(0||1)
    			{
    				printf("All bits must be 0 or a 1");
    			}
    			codeword_array[i] = bit[i];
    	}	
    
    	if (bit_array[0] + bit_array[5] + bit_array[10] = (0||2))
    		{	
    			codeword_array[15] = 0;
    		}
    	else if (bit_array[0] + bit_array[5] + bit_array[10] = (1||3))
    		{
    			codeword_array[15] = 1;	
    		}	
    	else
    		{
    			printf("Error\n");
    		}
    	system("pause");
    	return 0;
    }
    The thinking behind this is that if I use a 20 bit array, and start off filling the 1st 15 blocks, then do the parity check on them and with my answers fill the final 5.
    However I am getting a good few errors on this.

    Hope someone can help.

    Regards,

    Niall.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Firt of all you should use == in your if statements. = is an assignment operator which means all your if tests will be true.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You also use "bit" but never declare it anywhere...

    What errors are you getting?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10
    Code:
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : error C2143: syntax error : missing ')' before 'type'
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : warning C4552: '<' : operator has no effect; expected operator with side-effect
    (14) : error C2059: syntax error : ')'
    (15) : error C2143: syntax error : missing ';' before '{'
    (16) : error C2057: expected constant expression
    (16) : error C2466: cannot allocate an array of constant size 0
    (16) : error C2133: 'bit_array' : unknown size
    (18) : error C2143: syntax error : missing ';' before 'if'
    (18) : error C2061: syntax error : identifier 'bit'
    (22) : error C2109: subscript requires array or pointer type
    (25) : error C2059: syntax error : 'if'
    (29) : error C2059: syntax error : 'else'
    (33) : error C2059: syntax error : 'else'
    (39) : error C2143: syntax error : missing ')' before 'string'
    (39) : error C2143: syntax error : missing '{' before 'string'
    (39) : error C2059: syntax error : '<Unknown>'
    (39) : error C2059: syntax error : ')'
    (40) : error C2059: syntax error : 'return'
    (41) : error C2059: syntax error : '}'
    These are all the errors, (after I changed the = to == and declared bit) A lot of them seem to be linked to the one thing, so I think I might just be missing something simple thats causing a lot of them!!!

    P.s. I declared bit outside the for loop, is this correct?

    Code:
    int codeword_array[20];
    	int i=0;
    	int bit;
    
    	printf("Welcome to the 5-bit parity code calculator\nPlease enter the 1st bit of the 15 bit codeword now: \n");
    	
    	for (int i>=0; i<16; i++)

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    you need to use a & before bit_array[i] in your scanf statement.

    And you need a ; after the scanf line too.

    compile again and post your errors.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10
    Ok, that got rid of a couple, thanks for that. The next one's are:

    Code:
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : error C2143: syntax error : missing ')' before 'type'
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : warning C4552: '<' : operator has no effect; expected operator with side-effect
    (14) : error C2059: syntax error : ')'
    (15) : error C2143: syntax error : missing ';' before '{'
    (16) : error C2057: expected constant expression
    (16) : error C2466: cannot allocate an array of constant size 0
    (16) : error C2133: 'bit_array' : unknown size
    (18) : error C2061: syntax error : identifier 'bit'
    (22) : error C2109: subscript requires array or pointer type
    (25) : error C2059: syntax error : 'if'
    (29) : error C2059: syntax error : 'else'
    (33) : error C2059: syntax error : 'else'
    (39) : error C2143: syntax error : missing ')' before 'string'
    (39) : error C2143: syntax error : missing '{' before 'string'
    (39) : error C2059: syntax error : '<Unknown>'
    (39) : error C2059: syntax error : ')'
    (40) : error C2059: syntax error : 'return'
    (41) : error C2059: syntax error : '}'
    There seems to be a good few on line 14. Line 14 is the beginning of the for loop,
    Code:
    	for (int i>=0; i<16; i++)
    Sorry about this, I know its very messy, but just trying to get my head around it!!

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    the fist part of the for statement sets the intial value of i.

    for (int i = 0; i<16; i++) will loop from 0 - 15

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10
    Ok, thats grand thanks,

    But still giving the errors that i'm missing ;'s around line 14. Dunno where this is happening though,

    Any ideas?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    post your fixed code

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    int main()
    {
    	
    	int codeword_array[20];
    	int i=0;
    	int bit;
    
    	printf("Welcome to the 5-bit parity code calculator\nPlease enter the 1st bit of the 15 bit codeword now: \n");
    	
    	for (int i = 0; i<16; i++)
    	{
    		int bit_array[i];	
    		scanf("%d", &bit_array[i]);
    			if bit !==(0||1)
    			{
    				printf("All bits must be 0 or a 1");
    			}
    			codeword_array[i] = bit[i];
    	}	
    
     	if (bit_array[0] + bit_array[5] + bit_array[10] == (0||2))
    		{	
    			codeword_array[15] == 0;
    		}
    	else if (bit_array[0] + bit_array[5] + bit_array[10] == (1||3))
    		{
    			codeword_array[15] == 1;	
    		}	
    	else
    		{
    			printf("Error\n");
    		}
    	
    	
    	system("pause");
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You can't declare bit_array inside the for loop. Doing so will declare it each iteration.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You test bit after the scanf, but never assign bit a value. You should do...

    Code:
    scanf("%d", &bit);
    if (bit != 0 && bit !=1)
    {
      printf("All bits...");
    }
    else
    {
      codeword_array[i] = bit;
    }
    You don't need bit_array at all. Change bit_array in your subsequent if statements to codeword_array.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10
    Ok, should it be declared globally? I have done it like this now....
    Code:
    int codeword_array[20];
    	int i=0;
    	int bit;
    	int bit_array[i];	
    
    	printf("Welcome to the 5-bit parity code calculator\nPlease enter the 1st bit of the 15 bit codeword now: \n");
    	
    	for (int i = 0; i<16; i++)
    	{		
    		scanf("%d", &bit_array[i]);
    			if bit !==(0||1)
    			{
    				printf("All bits must be 0 or a 1");
    			}
    			codeword_array[i] = bit[i];
    	}	
    
     	if (bit_array[0] + bit_array[5] + bit_array[10] == (0||2))
    		{	
    			codeword_array[15] == 0;
    		}
    	else if (bit_array[0] + bit_array[5] + bit_array[10] == (1||3))
    		{
    			codeword_array[15] == 1;	
    		}	
    	else
    		{
    			printf("Error\n");
    		}
    	
    	
    	system("pause");
    	return 0;
    }
    With some new errors

    Code:
    (11) : error C2057: expected constant expression
    (11) : error C2466: cannot allocate an array of constant size 0
    (11) : error C2133: 'bit_array' : unknown size
    (15) : error C2143: syntax error : missing ';' before 'type'
    (15) : error C2143: syntax error : missing ';' before 'type'
    (15) : error C2143: syntax error : missing ')' before 'type'
    (15) : error C2143: syntax error : missing ';' before 'type'
    (15) : warning C4552: '<' : operator has no effect; expected operator with side-effect
    (15) : error C2059: syntax error : ')'
    (16) : error C2143: syntax error : missing ';' before '{'
    (18) : error C2061: syntax error : identifier 'bit'
    (22) : error C2109: subscript requires array or pointer type
    (25) : error C2059: syntax error : 'if'
    (29) : error C2059: syntax error : 'else'
    (33) : error C2059: syntax error : 'else'
    (39) : error C2143: syntax error : missing ')' before 'string'
    (39) : error C2143: syntax error : missing '{' before 'string'
    (39) : error C2059: syntax error : '<Unknown>'
    (39) : error C2059: syntax error : ')'
    (40) : error C2059: syntax error : 'return'
    (41) : error C2059: syntax error : '}'

  14. #14
    Registered User
    Join Date
    Feb 2009
    Location
    Dublin Ireland
    Posts
    10
    Ok, had posted that last one before I realised there was a reply!!!!

    Changing bit_array to codeword_array worked for a lot of them, down to 7 errors now.
    code is
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    int main()
    {
    	
    	int codeword_array[20];
    	int i=0;
    	int bit;
    	
    	printf("Welcome to the 5-bit parity code calculator\nPlease enter the 1st bit of the 15 bit codeword now: \n");
    	
    	for (int i = 0; i<16; i++)
    	{		
    		scanf("%d", &bit);
    		if (bit != 0 && bit !=1)
    			{
    				printf("All bits must be a 0 or a 1/n");
    			}
    		else
    			{
    				codeword_array[i] = bit;
    			}
    		
    		
    	
    
     	if (codeword_array[0] + codeword_array[5] + codeword_array[10] == (0||2))
    		{	
    			codeword_array[15] = 0;
    		}
    	else if (codeword_array[0] + codeword_array[5] + codeword_array[10] == (1||3))
    		{
    			codeword_array[15] = 1;	
    		}	
    	else
    		{
    			printf("Error\n");
    		}
    	
    	
    	system("pause");
    	return 0;
    }
    
    /*scanf( "%d", &bit1 );
    	
    	if ( bit1 != (0 || 1))
    	{
    		printf( "All bits must be either 0 or 1\n" );
    	}
    	else
    	{	
    		printf( "You entered %d", bit1);
    	}
    and the remaining errors are

    Code:
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : error C2143: syntax error : missing ')' before 'type'
    (14) : error C2143: syntax error : missing ';' before 'type'
    (14) : warning C4552: '<' : operator has no effect; expected operator with side-effect
    (14) : error C2059: syntax error : ')'
    (15) : error C2143: syntax error : missing ';' before '{'
    (43) : warning C4013: 'system' undefined; assuming extern returning int
    (94) : fatal error C1075: end of file found before the left brace '{' at 'c:\users\niall\documents\college\4th year\2nd semester\ee402\5bit parity code\5bit parity code\source.c(6)' was matched

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You don't need bit_array at all. If you're storing the bit array in codeword_array, why store it in bit_array too?

    and you can't use [i] in the declaration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  2. Where do a task get "wakeuped", blocked, preempted?
    By micke_b in forum Linux Programming
    Replies: 4
    Last Post: 12-28-2007, 04:49 PM
  3. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Help with simple physics task
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-23-2004, 02:22 PM

Tags for this Thread