losing decimals

This is a discussion on losing decimals within the C++ Programming forums, part of the General Programming Boards category; i'm trying to display the values (min, max, mean, standard deviation, etc. ) and it's giving me most things back ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    losing decimals

    i'm trying to display the values (min, max, mean, standard deviation, etc. ) and it's giving me most things back with 0 in the decimal place. the sum of the array is coming out to be 210.1, when in reality it is 215.6. so all of the decimals are being dropped off. except for .1, it is keeping that, but it is dropping .7 down to 0. so it's throwing off all my equations. is it that in the for loop to find the sum, i set "i" as int? i've tried using double and float, but it gives me error messages saying i can't do that. i'm not sure what to do. and yeah i know my standard deviation equation is wrong, i'll fix that.
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    #include <math.h>
    using namespace std;
    
    
    int i; //declaring i as int. 
    
    void sortarrays (float ar[]); //declaring function prototypes
    void calcByAddr( float ar[], int, double*, double*, double*, double*, double* );
    
    
    
    
    
    
    int main()
    {
    
    
    	float ar[15] = {2.4, 6.5, 1.2, 0.7, 15.3, 3.9, 78.1, 12.0, 5.4, 10.1, 24.0, 7.8, 46.8, 1.3, 0.1}; //initializing array
    	int num;//declaring int and double values to be used
    	double min;
    	double max;
    	double average;
    	double standarddev;
    	double median;
    	
    	sortarrays(ar);//calling the two functions to be used. first sort the array, then calculate the values by address
    	calcByAddr(ar, num, &min, &max, &average, &standarddev, &median);
    	
    	
    return 0;
    
    }
    
    
    /******************************************************************
    Function: sortarrays
    
    Use:      Sorts the array in ascending order 
    
    Arguments: Takes a double array as its argument
    
    Returns:   None 
    ***************************************************************/
    
    void sortarrays (float ar[])
    
    {
    
    int i, j;
    for (i=0; i<15; i++)
      {
       for(j=i+1; j<15; j++)
          { 
           if(ar[i]>ar[j]) //CONDITION :
             {
              int temp;
              temp = ar[i]; //save a copy of value in i           
              ar[i] = ar[j]; //copy value from j to i               
              ar[j] = temp; //copy saved value from i to j     
             }
    }
    }
    }
    
    
    /******************************************************************
    Function: calcByAddr
    Use:      Calculates various values such as minimum, maximum, median, etc. 
    
    Arguments: 1 array, 1 integer that holds the number of values in the array,
    		   and five pointers to doubles. .
    
    Returns:   None 
    ***************************************************************/
    
    
    void calcByAddr( float ar[], int num, double* min, double* max, double* average, double* standarddev, double* median )
    {
    	
    	int i; //declaring variables to be used
    	double sum=0;
    	double var=0;
    	double a=0;
    	double b=0;
    	double size=15;
    	double x=0;
    	
    	*min=ar[0]; //setting value for min and max
    	*max=ar[14];
    
    	
    
    for (i=0; i<15; i++) //setting for loop to calculate the sum of the array
    	{
    	sum+=ar[i];
    	};
    		
    
    *median = ar[7]; //setting value for median
    
    *average=sum/size; //setting value for the average
    
    for (i=0; i<15; i++)
    	{
    	x=ar[i]*ar[i];
    	};
    *standarddev=sqrt(((x)-(sum*sum)/14)/13);
      
    	
    
    cout<<"Results Using Pointers";
    
    cout<<"\nMinimum: "<<right<<setw(20)<<fixed<<setprecision(1)<<*min;
    cout<<"\nMaximum: "<<right<<setw(20)<<*max;
    cout<<"\nMean: "<<right<<setw(23)<<*average;
    cout<<"\nMedian: "<<right<<setw(21)<<*median;
    cout<<"\nStandard Deviation: "<<right<<setw(9)<<*standarddev; //displaying the final results
    
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > x=ar[i]*ar[i];
    Well this is NOT accumulating any kind of result.

    It's just the square of the last element.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    void sortarrays (float ar[])
    {
        int i, j;
        for (i=0; i<15; i++)
        {
            for(j=i+1; j<15; j++)
            { 
                if(ar[i]>ar[j]) //CONDITION :
                {
                   int temp;
                   temp = ar[i]; //save a copy of value in i           
                   ar[i] = ar[j]; //copy value from j to i               
                   ar[j] = temp; //copy saved value from i to j     
                }
            }
        }
    }
    Notice anything!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with Decimals
    By ls1211 in forum C++ Programming
    Replies: 10
    Last Post: 09-11-2009, 02:04 PM
  2. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 03:13 PM
  3. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  4. Lining up decimals & text - code included inside.
    By INFERNO2K in forum C Programming
    Replies: 7
    Last Post: 11-27-2004, 03:49 PM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21