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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2012, 09:40 AM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
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;
}

Reply With Quote
  #2  
Old October 2nd, 2012, 11:48 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,348 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC 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 
Rate This Thread:


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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap