Hmm! Now all I get are segmentation faults. Sorry for the size of this thing, I'm going to post it all:
This is the main interface!
Code:
/* Assignment 2 1402 */
#include<stdio.h>
#include"functions.h"
main()
{
/*void encodeChar (char c, int pointer);
void setCheckBits (int code[]);
void flipBit(int pointer, int bitNum);*/
char theChar;
int theNumber;
int flip;
int bits[15];
//int *pointer = bits;
//int *pointerChar = theChar;
int charbs[8];
printf("Please enter a character to be transmitted: ");
scanf("%c",&theChar);
encodeChar(theChar,charbs);
printf("Please enter a bit number (0-15, zero means no bit flipped): ");
scanf("%d",&theNumber);
//flipBit(charbs,flip); //DECOMMENT later
flipBit(bits,theNumber);
decodeChar(charbs);
printArray(charbs,8); //this was added
printf("");
correctTransmission(flip,bits);
}
This is the functions.c file:
Code:
#include<stdio.h>
#include"functions.h"
//I'll try to work you guys through this with comments.
/*I'm calling a function to unpack the bits of the input character into an array of 15 ints, each of which
stores a bit. I need to store the 8-character bits of input character into
elements 3,5,6,7,9,10,11,12 of the ints array, and leaving other elements zero. Elements 13
through 15 are "dummy" data and are going to be set to zero.*/
void encodeChar (char theChar, int bits[]){
unsigned char temp;
unsigned char mask = 0x80;
int size=8;
int i;
for (i=0; i<size; i++){
temp = theChar & mask;
if (temp == 0)
{
bits[i]=0;
//printf("%d", temp);
}
else {
bits[i]=1;
//printf("%d", temp);
}
mask = mask >> 1;
}
//printf("\n");
}
//Call a function to set the 4 check bits...
void setCheckBits(int *code){
code[0] = code[2] ^ code[4] ^ code[6] ^ code[8] ^ code[10] ^ code[12] ^ code[14];
code[1] = code[2] ^ code[5] ^ code[6] ^ code[9] ^ code[10] ^ code[13] ^ code[14];
code[3] = code[4] ^ code[5] ^ code[6] ^ code[11] ^ code[13] ^ code[14] ^ code[15];
code[7] = code[8] ^ code[9] ^ code[10] ^ code[11] ^ code[12] ^ code[13] ^ code[14];
}
/*The array is now assumed to be transmitted. OK, time to simulate noise. Call a function
which gets as input the array and the number of the bit to be flipped in the array. This may be
any of the 15 bits.*/
void flipBit(int *numberArray, int flipped){
if(numberArray[flipped]==0) {
numberArray[flipped] = 1;
} else {
numberArray[flipped] = 0;
}
}
/*Now call a function to simulate the error detection. The array gets passed to the function
which returns an int with the incorrect bit number or zero if there are no errors.*/
void clearArray(int *array, int size){
int i;
for (i=0; i<size; i++)
{
array[i]=0;
}
}
// Actually, I don't remember writing this. I'm not sure what it's doing here, or what it does.
void printArray(int *array, int size){
int i;
char c;
for (i=0; i<size; i++)
{
printf("%d ", array[i]);
}
printf ("\n");
}
void correctTransmission(int wrongBit, int *theBit){
int corruptVal = 0;
int fixedVal = 0;
corruptVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
printf("Pre-CHAR corrupted value is: %d\n",corruptVal);
printf("Pre-CHAR fixed value is: %d\n",fixedVal);
printf("The corrupt value is: %c\n",(char)corruptVal);
flipBit(theBit,wrongBit);
fixedVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
printf("the corrected value is: %c\n",(char)fixedVal);
}
/*Now I'm supposed to call a function to simulate the error detection. The array gets passed to the function which returns an int with the incorrect bit number or zero if there are no errors.*/
int checkCode(int *bit) {
int brokenCode = (bit[0]*1) + (bit[1]*2) + (bit[3]*4) + (bit[7]*8);
return brokenCode;
}
#include<stdio.h>
#include"functions.h"
//void encodeChar (char c, int *code);
//void setCheckBits (int code[]);
//void flipBit(int *code, int bitNum);
void flipBit(int *numberArray, int flipped){
if(numberArray[flipped]==0) {
numberArray[flipped] = 1;
} else {
numberArray[flipped] = 0;
}
}
void setCheckBits(int *code){
code[0] = code[2] ^ code[4] ^ code[6] ^ code[8] ^ code[10] ^ code[12] ^ code[14];
code[1] = code[2] ^ code[5] ^ code[6] ^ code[9] ^ code[10] ^ code[13] ^ code[14];
code[3] = code[4] ^ code[5] ^ code[6] ^ code[11] ^ code[13] ^ code[14] ^ code[15];
code[7] = code[8] ^ code[9] ^ code[10] ^ code[11] ^ code[12] ^ code[13] ^ code[14];
}
/*
void clearArray(int *array, int size){
int i;
for (i=0; i<size; i++)
{
array[i]=0;
}
}*/
void printArray(int *array, int size){
int i;
char c;
for (i=0; i<size; i++)
{
printf("%d ", array[i]);
}
printf ("\n");
}
void encodeChar (char theChar, int bits[]){
unsigned char temp;
unsigned char mask = 0x80;
int size=8;
int i;
for (i=0; i<size; i++){
temp = theChar & mask;
if (temp == 0)
{
bits[i]=0;
//printf("%d", temp);
}
else {
bits[i]=1;
//printf("%d", temp);
}
mask = mask >> 1;
}
//printf("\n");
}
void correctTransmission(int wrongBit, int *theBit){
int corruptVal = 0;
//int fixedVal = 0;
corruptVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);
printf("Pre-CHAR corrupted value is: %d\n",corruptVal);
//printf("Pre-CHAR fixed value is: %d\n",fixedVal);
printf("The corrupt value is: %c\n",(char)corruptVal);
flipBit(theBit,wrongBit);
/*fixedVal = (theBit[11]*1) + (theBit[10]*2) + (theBit[9]*4) + (theBit[8]*8) + (theBit[6]*16) + (theBit[5]*32) + (theBit[4]*64) + (theBit[2]*128);*/
//printf("the corrected value is: %c\n",(char)fixedVal);
}
/*void addCode(int charBit, int *bit) {
int i = 0;
int bitsPosition = 2;
if (bitsPosition == 3) {
bitsPosition++;
}
if (bitsPosition == 7) {
bitsPosition++;
}
bit[bitsPosition] = charBit[i];
bitsPosition++;
}*/
int checkCode(int *bit) {
int brokenCode = (bit[0]*1) + (bit[1]*2) + (bit[3]*4) + (bit[7]*8);
return brokenCode;
}
char decodeChar(int *code) {
char fixedVal = (code[11]*1) + (code[10]*2) + (code[9]*4) + (code[8]*8) + (code[6]*16) + (code[5]*32) + (code[4]*64) + (code[2]*128);
printf("the corrected value is: %c\n",fixedVal);
}
And finally this is the "functions.h" file:
Code:
void encodeChar (char theChar, int *numberArray); //yes
void setCheckBits (int code[]); //yes
void flipBit(int *numberArray, int bitNum); //yes
int checkCode(int *code); //yes
void addCode(int charBit, int *theBit);
void correctTransmission(int wrongBit, int *theBit);
// void clearArray(int pointer, int size);
char decodeChar(int *code);