Please write this functions in C code language Function 1 : int isNumber(char *c
ID: 3727068 • Letter: P
Question
Please write this functions in C code language
Function 1 :
int isNumber(char *c) Function should return 1 is the argument passed is a digit and zero otherwise. Please note the asci equivalents of numeric characters from an asci table.
Function 2 :
Given the following C function prototype that accepts two integer arguments. Complete the function and return 1 if a > b , return -1 if a < b and return 0 if a is equal to b. int compare(int a, int b)
Function 3 :
Given the following C function prototype which accepts an integer argument, complete the implementation of the function to return the number of 1’s in the binary representation of the number passed. For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long. int countOnes(int number)
Function 4:
Write a C language function to determine the hamming distance ( the number of different bits) between two characters passed as parameter. Please note that the function prototype is given below. int returnHammingDistance( char firstparameter, char secondparameter)
NOTE: (defining Hamming Distance) for function #4
The Hamming distance between two integers or strings is the number of positions at which the corresponding symbols differ. It is used in telecommunication to count the number of flipped bits in a fixed-length binary word as an estimate of error, and therefore is sometimes called the signal distance. Hamming distance is the count of minimum number of bits that need to be inverted in order to change one number into the other. Example: Hamming distance between the two numbers: 11110100 and 10100101 1 1 1 1 0 1 0 0 1 0 1 0 0 1 0 1 The highlighted 3 bits differ from each other in the given example, and thus, its hamming distance is 3.
Explanation / Answer
PLEASE REFER BELOW CODE
#include<stdlib.h>
#include<stdio.h>
//checking
int isNumber(char *c)
{
//checking if character is number or not
if(*c >= '0' && *c <= '9')
return 1;
else
return 0;
}
int compare(int a, int b)
{
//comparing two numbers
if(a > b)
return 1;
else if(a < b)
return -1;
else
return 0;
}
int countOnes(int number)
{
int count = 0;
//loop through all digits of number and checking digits by right shifting and bitwise ANDing with 1
while(number)
{
count += number & 1;
number >>=1;
}
return count;
}
int returnHammingDistance( char firstparameter, char secondparameter)
{
int result,fp,sp;
char val;
//convert char to number
fp = firstparameter - '0';
sp = secondparameter - '0';
//XORing both numbers so that it gives the output as 1 when there is change in bit and count the
//number of 1s in result.
val = fp^sp;
result = 0;
while(val)
{
++result;
val &= val - 1;
}
return result;
}
int main()
{
int result,a,b;
char ch,firstparameter,secondparameter;
ch = '8';
result = isNumber(&ch);
if(result)
printf("charcter is Digit ");
else
printf("charcter is non-digit ");
a = 15;
b=10;
result = compare(a,b);
if(result == 1)
printf("a > b ");
else if(result == -1)
printf("a < b ");
else
printf("a = b ");
a = 011100011;
result = countOnes(a);
printf("Number of 1s = %d ", result);
firstparameter = '9';
secondparameter = '8';
result = returnHammingDistance(firstparameter,secondparameter);
printf("Hamming distance between the two numbers = %d ", result);
return 0;
}
PLEASE REFER BELOW OUTPUT
charcter is Digit
a > b
Number of 1s = 5
Hamming distance between the two numbers = 1
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.