How would you write in function in C program in Linux enviro

How would you write in function in C program in Linux environment? Thanks for help!!

Write a function double calc_ber(char* orig, char* recv); that accepts the original message and the received message. It should calculate the bit error rate of the transmission, which is the percent of bits in the binary representations of the two input strings that disagree. Please keep in mind that this is different than the percent of characters that don’t agree. [Think about why these are different things].

Solution

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


typedef int bool;
#define true 1
#define false 0

double calc_ber(char* orig, char* recv);

int main()
{
char *orig = \"a original string\";
char *recv = \"b riginal sTring\";

double ber = calc_ber(orig, recv);
printf(\"Ber is: %f\ \", ber);
return 0;
}

/* A utility function to reverse a string */
void reverse(char str[], int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
char a = *(str+start);
*(str+start) = *(str+end);
*(str+end) = a;
  
start++;
end--;
}
}

char* itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;

/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = \'0\';
str[i] = \'\\0\';
return str;
}

// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}

// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + \'a\' : rem + \'0\';
num = num/base;
}

// If number is negative, append \'-\'
if (isNegative)
str[i++] = \'-\';

str[i] = \'\\0\'; // Append string terminator

// Reverse the string
reverse(str, i);

return str;
}


int computeDiff(char *output1, char *output2, int *totalBit)
{
int bitDiff = 0;
while((*output1 != \'\\0\') || (*output2 != \'\\0\'))
{
*totalBit = *totalBit + 1;
if ((*output1) != (*output2))
{
bitDiff++;
}
*output1++;
*output2++;
}

while(*output1 != \'\\0\')
{
*totalBit = *totalBit + 1;
bitDiff++;
*output1++;
}

while(*output2 != \'\\0\')
{
*totalBit = *totalBit + 1;
bitDiff++;
*output2++;
}
return bitDiff;
}

int countBit(char *output, int *totalBit)
{
int bitDiff = 0;
while(*output != \'\\0\')
{
*totalBit = *totalBit + 1;
bitDiff++;
*output++;
}
return bitDiff;
}

double calc_ber(char* orig, char* recv)
{
char *str1 = orig;
char *str2 = recv;
  
int bitDiff = 0;
int totalBit = 0;
while((*str1 != \'\\0\') || (*str2!= \'\\0\'))
{
char output1[9];
char output2[9];

itoa(*str1, output1, 2);
itoa(*str2, output2, 2);   
bitDiff += computeDiff(output1, output2, &totalBit);
*str1++;
*str2++;
}

while(*str1 != \'\\0\')
{
char output1[9];
itoa(*str1, output1, 2);
bitDiff += countBit(output1, &totalBit);
}

while(*str2 != \'\\0\')
{
char output2[9];
itoa(*str2, output2, 2);
bitDiff += countBit(output2, &totalBit);
}

return (double)(bitDiff)/totalBit;
}

How would you write in function in C program in Linux environment? Thanks for help!! Write a function double calc_ber(char* orig, char* recv); that accepts the
How would you write in function in C program in Linux environment? Thanks for help!! Write a function double calc_ber(char* orig, char* recv); that accepts the
How would you write in function in C program in Linux environment? Thanks for help!! Write a function double calc_ber(char* orig, char* recv); that accepts the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site