0

This code does work on Code blocks but no on the school online compiler.

A memory with a length S is given. Each single bit can be marked with index from 0 to S-1. On the beginning the memory is empty and it has 0 everywhere.

In the first line you have to input 3 integers: N,L and S. In the following N lines there will be data with length L given, coded in hamming code with minimal distance D=4. And also a positive integer which marks the position in the memory where the hamming code should be copied if it it correctly received.

You have to check if there is a mistake, if there is no mistake you copy the code in the memory, if there is one mistake , you fix it and than copy it, if there are 2 mistakes than you do nothing. Print the final content of the memory..

Input example:

1 5 128
0011010111 2

Output example:

00101010000000

(there are bit less than 128 zero's so i wont type em all). :)

Input example (2):

10 4 100
01111000 15
00110011 62
01110001 36
01101001 61
11100001 60
10101010 1
11111111 47
01101111 85
11111110 2
01010101 44

Output example (2):

0111110000000001100000000000000000000000000000101110000000001000010000000000000000000000000000000000

My solution (for the 1st example):

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[100], b[5], c[200], N,L,S;
int i,j,k,r;
scanf("%d %d %d", &N, &L, &S);
for(i=0; i<N; i++)
{
    int p1,p2,p3,p4;
    for(j=1; j<L+7; j++)
    {
        scanf("%d", &a[j]);
    }
    p1 = (a[1]+a[3]+a[5]+a[7]+a[9])%2;
    p2 = (a[2]+a[3]+a[6]+a[7])%2;
    p3 = (a[4]+a[5]+a[6]+a[7])%2;
    p4 = (a[8]+a[9]+a[10])%2;
    if(p1==0 && p2==0 && p3==0 && p4==1) //first position
    {
        if(a[1]==0) a[1]=1;
        else if(a[1]==1) a[1]=0;
    }
    if(p1==0 && p2==0 && p3==1 && p4==0) //second position
    {
        if(a[2]==0) a[2]=1;
        else if(a[2]==1) a[2]=0;
    }
    if(p1==0 && p2==0 && p3==1 && p4==1) //third position
    {
        if(a[3]==0) a[3]=1;
        else if(a[3]==1) a[3]=0;
    }
    if(p1==0 && p2==1 && p3==0 && p4==0) //fourth position
    {
        if(a[4]==0) a[4]=1;
        else if(a[4]==1) a[4]=0;
    }
    if(p1==0 && p2==1 && p3==0 && p4==1) //fifth position
    {
        if(a[5]==0) a[5]=1;
        else if(a[5]==1) a[5]=0;
    }
    if(p1==0 && p2==1 && p3==1 && p4==0) //sixth position
    {
        if(a[6]==0) a[6]=1;
        else if(a[6]==1) a[6]=0;
    }
    if(p1==0 && p2==1 && p3==1 && p4==1) //seventh position
    {
        if(a[7]==0) a[7]=1;
        else if(a[7]==1) a[7]=0;
    }
    if(p1==1 && p2==0 && p3==0 && p4==0) //eight position
    {
        if(a[8]==0) a[8]=1;
        else if(a[8]==1) a[8]=0;
    }
    if(p1==1 && p2==0 && p3==0 && p4==1) //ninth position
    {
        if(a[9]==0) a[9]=1;
        else if(a[9]==1) a[9]=0;
    }
    b[0]=a[3];
    b[1]=a[5];
    b[2]=a[6];
    b[3]=a[7];
    b[4]=a[9];
    for(k=0; k<S-L; k++)
    {
        if(k==2)
        {
            for(r=0; r<5; r++)
                printf("%d", b[r]);
        }
        else c[i]=0; printf("%d", c[i]);
    }
    }
    return 0;
    }
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 2
    "it doesn't on the school online compiler" - what's the error, please? Can you give us any more details how it doesn't work? – Rup Apr 22 '14 at 15:26
  • 2
    You at least have an indentation error (or missing braces?) on this line: `else c[i]=0; printf("%d", c[i]);` – ooga Apr 22 '14 at 15:38
  • 1
    There is a possibility of un-initialized variables. Many IDEs will initialize all variables for you in Debug mode. – Thomas Matthews Apr 22 '14 at 15:57
  • Here is the output from the school compiler : 00325993276725253012832767325990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 – user3075088 Apr 22 '14 at 16:40
  • And here is the output from CodeBlocks (it is correct): http://postimg.org/image/joen9shhf/ – user3075088 Apr 22 '14 at 16:44
  • Best try compiling with all warnings enabled. – Deduplicator Apr 22 '14 at 16:52
  • If you scanf("%d",&a[0]) `00001111` at once, you will have `a[0]==15` and things will become strange...Could you try `scanf("%1d",&a[j])` ? – francis Apr 22 '14 at 16:53
  • @francis When did `scanf` start parsing binary with `%d`? I think you might rather find that `a[0]==1111`... – twalberg Apr 22 '14 at 16:56
  • @twalberg : sorry, my fault : i rewrite it : If you scanf("%d",&a[0]) `00001111` at once, you will have `a[0]==1111` and things will become strange... @user Could you try `scanf("%1d",&a[j])` ? – francis Apr 22 '14 at 16:59
  • THANK YOU ! IT worked for the first example, now i need to work on the second example :) – user3075088 Apr 22 '14 at 17:00

0 Answers0