### Thread: Hex to decimal find mean wrong result?

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
43
Rep Power
7

#### Find mean/average wrong result?

The program first converts a given 5 hex numbers to decimals and then it should find the mean of those numbers. I was thinking it might be because of int, double conversion. I tried several ways but still it doesnt work.

Code:
```#include <stdio.h>
#include <math.h>

int HextoDec(char *hexArray); //function prototype
int calculateMean(int res1, int res2,int res3,int res4,int res5 );
int calculateStdDev(int res1, int res2,int res3,int res4,int res5, int var );
int calculateVariance(int res1, int res2,int res3,int res4,int res5,int mean );

main()
{

char *hexnumber1 = "41226666";
char *hexnumber2 = "41A0CCCD";
char *hexnumber3 = "41574BC7";
char *hexnumber4 = "4144CCCD";
char *hexnumber5 = "4146E979";

//Calling the conversion function
int result1 = HextoDec(hexnumber1);
int result2 = HextoDec(hexnumber2);
int result3 = HextoDec(hexnumber3);
int result4 = HextoDec(hexnumber4);
int result5 = HextoDec(hexnumber5);

printf("Calculating mean, std deviation and variance for the following hex numbers:\n\n 0x41226666 \n 0x41A0CCCD \n 0x41574BC7 \n 0x4144CCCD \n 0x4146E979\n\n");
printf("Their respective decimal equivalent is: \n\n %i \n %i \n %i \n %i \n %i \n\n", result1, result2,result3,result4,result5);

//calculating and printing out the mean of the decimal numbers
int mean = calculateMean(result1,result2,result3,result4,result5);
printf("Mean: %i \n", mean);

//calculating and printing out the variance of the decimal numbers
//int variance = calculateVariance(result1,result2,result3,result4,result5,mean);
//printf("Variance: %i \n", variance);

//calculating and printing out the standard deviation of the decimal numbers
//int stdDev = calculateStdDev(result1,result2,result3,result4,result5, variance);
//printf("Standard deviation: %i \n", stdDev);
}

//Converting the hex numbers into their decimal equivalent. Those values will then be used to
//calculate the mean, the standard deviation and the variance
int HextoDec(char *hexArray){

int decimal;
int bit;
int i=0;
int j=0;
i = strlen(hexArray);

//while there is a next bit in the array
while(i!=0)
{
bit = hexArray[j];

//if the bit is a digit do the following
if(('0' <= bit && bit <= '9'))
{
decimal = decimal * 16;
decimal = decimal + (bit - '0');
}

//if the bit is a letter do the following
if(('A' <= bit && bit <= 'F'))
{
decimal = decimal * 16;
decimal = decimal + (bit - '7');
}

i--;
j++;

}

return decimal;
}

//Calculate the mean of the decimal values
int calculateMean(int res1, int res2,int res3,int res4,int res5 ){

int meanresult =(res1+res2+res3+res4+res5)/5;

return meanresult;
}

//Calculating the standard deviation
int calculateStdDev(int res1, int res2,int res3,int res4,int res5,int var ){

int stdDevresult = sqrt(var);

return stdDevresult;
}

//Calculating the variance
int calculateVariance(int res1, int res2,int res3,int res4,int res5, int mean ){

int sum1 = (res1 - mean)*(res1 - mean);
int sum2 = (res2 - mean)*(res2 - mean);
int sum3 = (res3 - mean)*(res3 - mean);
int sum4 = (res4 - mean)*(res4 - mean);
int sum5 = (res5 - mean)*(res5 - mean);

int vsum = sum1+sum2+sum3+sum4+sum5;

int varresult = vsum/4;

return varresult;
}```
2. In particular, look at the new averaging function.
Code:
```// main needs to return a value.  Your operating system needs to know.
// arrays are a programmer's friend.
// yes, different data type helps.  The 4 byte integer overflow prevented you from getting an answer anywhere near correct.
// This program still sux.

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

//Converting the hex numbers into their decimal equivalent. Those values will then be used to
//calculate the mean, the standard deviation and the variance
int HextoDec(char *hexArray) {
int decimal;
int bit;
int i=0;
int j=0;
i = strlen(hexArray);
while(i!=0) {
bit = hexArray[j];
//if the bit is a digit do the following
if(('0' <= bit) && (bit <= '9')) {
decimal = decimal * 16;
decimal = decimal + (bit - '0');
}

//if the bit is a letter do the following
if(('A' <= bit && bit <= 'F')) {
decimal = decimal * 16;
decimal = decimal + (bit - '7');
}
i--;
j++;
}
printf("%d = 16b%s\n",decimal,hexArray);
return decimal;
}

int calculateMean(float*a,int n) {
int i;
float sum = 0;
for (i = 0; i < n; ++i)
sum += a[i];
return sum/n;
}

int calculateStdDev(int res1, int res2,int res3,int res4,int res5,int var ){
int stdDevresult = sqrt(var);
return stdDevresult;
}

int calculateVariance(int res1, int res2,int res3,int res4,int res5, int mean ){
int sum1 = (res1 - mean)*(res1 - mean);
int sum2 = (res2 - mean)*(res2 - mean);
int sum3 = (res3 - mean)*(res3 - mean);
int sum4 = (res4 - mean)*(res4 - mean);
int sum5 = (res5 - mean)*(res5 - mean);
int vsum = sum1+sum2+sum3+sum4+sum5;
int varresult = vsum/4;
return varresult;
}

main() {
char *hexnumber1 = "41226666";
char *hexnumber2 = "41A0CCCD";
char *hexnumber3 = "41574BC7";
char *hexnumber4 = "4144CCCD";
char *hexnumber5 = "4146E979";
int result1 = HextoDec(hexnumber1);
int result2 = HextoDec(hexnumber2);
int result3 = HextoDec(hexnumber3);
int result4 = HextoDec(hexnumber4);
int result5 = HextoDec(hexnumber5);

float array[300];
int i=0;
array[i++] = result1;
array[i++] = result2;
array[i++] = result3;
array[i++] = result4;
array[i++] = result5;

printf("Calculating mean, std deviation and variance for the following hex numbers:\n\n 0x41226666 \n 0x41A0CCCD \n 0x41574BC7 \n 0x4144CCCD \n 0x4146E979\n\n");
printf("Their respective decimal equivalent is: \n\n %i \n %i \n %i \n %i \n %i \n\n", result1, result2,result3,result4,result5);

//calculating and printing out the mean of the decimal numbers
float mean = calculateMean(array,i);
printf("Mean: %g\n", mean);

//calculating and printing out the variance of the decimal numbers
//int variance = calculateVariance(result1,result2,result3,result4,result5,mean);
//printf("Variance: %i \n", variance);

//calculating and printing out the standard deviation of the decimal numbers
//int stdDev = calculateStdDev(result1,result2,result3,result4,result5, variance);
//printf("Standard deviation: %i \n", stdDev);
return 0;
}```