Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
October 2nd, 2012, 10:40 AM
 lisa92
Contributing User

Join Date: Oct 2011
Posts: 43
Time spent in forums: 10 h 33 m 29 sec
Reputation Power: 3
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
October 2nd, 2012, 12:48 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,158
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 8 m
Reputation Power: 455
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;
}```
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Hex to decimal find mean wrong result?

Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |