home Sandy, UT
Wednesday, 2017.11.22
04:17 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 4°C (39.2°F)
Barometer: 1029 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for Discrete Math class, short for Discrete Structures.
Assignment: a string of data is binary transmitted on a line with noise.
Split the string in chars, convert them in ASCII, then in binary,
transmit each char 3 times, every time with a random binary noise.
In the final binary number every bit will and must be repeated at least 2 times, so you can get the original value by majority.
( In this algorithm there is a problem: if a noise occurs twice in the same bit position, final character is wrong. I told instructor, so I changed the random noise generator to occur not in the same position.
Then convert back from binary to ASCII and rebuild the string.
I preferred to write a class to manage the operations on the binary chars, that's Result.cpp
For a matter of brevity I toUpper-ed the string.
The files are: Result.h, Result.cpp and noise.cpp
Download the executable: noise.exe
November 16, 2004

Noise in a Binary Transmission


Result.h
/**
  *  @title: Result.h
  *  @date: 2004.11.16 - 18:02
  *  @author: vinnie
  *  @definition for CResult class
  */

class CResult
{
  private:
    //  vars
    int     origChar [8];
    int     noiseChar[3][8];
    int     dirtyChar[3][8];
    int     resChar  [8];
    char    initChar;
    char    finalChar;

    //  methods
    void     DecToBin( int);

  public:
    CResult( );
    ~CResult( );

    //  accessor
    void    GetDirtyChar( );
    void    GetFinal( );
    void    GetNoise( );
    void    GetOrigChar( );
    void    GetResChar( );

    //  modifiers
    void    AddNoiseToChar( );
    void    BuildResult( );
    void    SetNoiseChar( const int* , int);
    void    SetOrigChar( char);
    void    SetResChar( );
};

Result.cpp
/**
  *  @title: Result.cpp
  *  @date: 2004.11.16 - 18:02
  *  @author: vinnie
  *  @implementation for CResult class
  */

#include <stdio.h>
#include "Result.h"

CResult:: CResult( )  { initChar = 0; finalChar = 0;  }
CResult::~CResult( )  {                               }
///////////////////////////////////////////////////////

//  accessor
void CResult::GetDirtyChar( )
{
  for ( int i = 0; i < 3; i++)
  {
    for ( int j = 7; j >= 0; j--)
      printf( "%d", dirtyChar[i][j] );
    printf( " ");
  }
}

void CResult::GetFinal( ) { printf( "%c", finalChar ); }

void CResult::GetNoise( )
{
  for ( int i = 0; i < 3; i++)
  {
    for ( int j = 7; j >= 0; j--)
      printf( "%d", noiseChar[i][j] );
    printf( " ");
  }
}

void CResult::GetOrigChar( )
{
  for ( int i = 7; i >= 0; i--)
    printf( "%d", origChar[i] );
  printf( " ");
}

void CResult::GetResChar( )
{
  for ( int i = 7; i >= 0; i--)
    printf( "%d", resChar[i] );
  printf( " ");
}
///////////////////////////////////////////////////////

//  modifiers
void CResult::AddNoiseToChar( )
{
  for ( int i = 0; i < 3; i++)
    for ( int j = 0; j < 8; j++)
    {
      if ( noiseChar[i][j] )
      {
        if ( origChar[j] )
          dirtyChar[i][j] = 0;
        else
          dirtyChar[i][j] = 1;
      }
      else dirtyChar[i][j] = origChar[j];
    }
}

void CResult::BuildResult( )
{
  for ( int i = 0; i < 8; i++)
  {
    int zeros = 0;
    int ones  = 0;
    for ( int j = 0; j < 3; j++)
    {
      if ( dirtyChar[j][i] ) ++ones;
      else  ++zeros;
    }
    zeros > ones ? resChar[i] = 0 : resChar[i] = 1;
  }
}

void CResult::DecToBin( int x)
{
  for( int i = 0; i < 8; i++)
    origChar[i] = (x >> i) & 0x1;
}

void CResult::SetNoiseChar( const int* iAr, int i)
{
  for ( int j = 0; j < 8; j++)
    noiseChar[i][j] = *(iAr + j);
}

void CResult::SetOrigChar( char c)
{
  initChar = c;
  DecToBin( (int) c);
}

void CResult::SetResChar( )
{
  int  i = 8;
  finalChar = 0;

  while ( i--)
 {
    finalChar <<= 1;
    finalChar += resChar[i];
  }
}

prj2.cpp
/**
  *  @title: prj2.cpp
  *  @date: 2004.11.16 - 18:02
  *  @author: vinnie
  *  @source for prj2.exe
  */

#include <conio.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "Result.h"

int main( )
{
//  vars
const int rumor[64] =
  {
    0, 0, 0, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 0, 0, 0, 1, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 0, 0,
    0, 0, 1, 0, 0, 0, 0, 0,
    0, 1, 0, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 0, 0
  };
int   iLen = 0;
char  userInput[33];
///////////////////////////////////////////////////////////////////////////

//   code
system("color 1f");

// menu' label
MYMENU:   //  begin

//  reset vars
memset( userInput, 0, 33);
iLen  = 0;

//  print intro & menu
system("cls");
printf("\t\t    SLCC - CS 2310-01 - Fall 2004 - Project 2\n");
printf("\n Given a string, some random rumor is added.");
printf("\n Then the string is retrieved.\n For the sake of brevity ");
printf("the input string is limited to 32 chars.\n");

printf("\n Please input your string: ");
gets( userInput);
///////////////////////////////////////////////////////////////////////////

//  how long the string is
iLen = strlen( userInput);

//  toupper the string
for ( int i = 0; i < iLen; i++)
  if ( isalpha( userInput[i]) )
    userInput[i] = toupper( userInput[i]);

//  start the math

//  gimme as many objects as the chars in the string
CResult *res = new CResult [iLen];

//  init the randomizer
srand( clock() );

//  scan the userInput string
//  set a noise
//  calc the char + noise
for ( int i = 0; i < iLen; i++)
{
  //  take a char at time
  res[i].SetOrigChar( *( userInput + i ) );

  //  find 3 random noises
  int noRep[3];
  int k;
  k = rand() % 8;
  noRep[0] = k;
  res[i].SetNoiseChar( rumor+8*k, 0);

  BACK0:
  k = rand() % 8;
  if ( k == noRep[0] ) goto BACK0;
  noRep[1] = k;
  res[i].SetNoiseChar( rumor+8*k, 1);

  BACK1:
  k = rand() % 8;
  if ( k == noRep[0] || k == noRep[1] ) goto BACK1;
  noRep[2] = k;
  res[i].SetNoiseChar( rumor+8*k, 2);

  // add 3 noises to char
  res[i].AddNoiseToChar( );
}
////////////////////////////////////////////////////////////

//  build result and retrieve string
for ( int i = 0; i < iLen; i++)
{
  res[i].BuildResult( );
  res[i].SetResChar( );
}
////////////////////////////////////////////////////////////

//  print results
printf("\n");
printf("C A  OrigChar 1stNoise 2ndNoise 3rdNoise 1st-Res  2nd-Res  \
3rd-Res  Final    C\n");
printf("\n");

for ( int i = 0; i < iLen; i++)
{
  printf( "%c %d ", userInput[i], userInput[i] );
  res[i].GetOrigChar( );
  res[i].GetNoise( );
  res[i].GetDirtyChar( );
  res[i].GetResChar( );
  res[i].GetFinal( );
  printf("\n");
}
///////////////////////////////////////////////////////////////////////////

delete [] res;

//  ask for an other trip
printf("\n Another one [Y/y/N/n]? ");
while ( !kbhit() );
char  c = getchar( );
if ( c == 'Y' || c == 'y' ) goto MYMENU;
///////////////////////////////////////////////////////////////////////////

printf("\n Thanks for using Prj2\n");
printf(" Vincenzo Maggio Code - released under GPL\n");
system("color 0f");
return 0;
} //  niam

2004.11.16

Vincenzo Maggio