Decimal problems

This is a discussion on Decimal problems within the C++ Programming forums, part of the General Programming Boards category; In the latest incarnation of my program, I'm calculating the mean and median (among other information) of a set of ...

  1. #1
    Registered User fjf314's Avatar
    Join Date
    Jan 2004
    Posts
    13

    Decimal problems

    In the latest incarnation of my program, I'm calculating the mean and median (among other information) of a set of numbers, which are being read from a file. My problem is that the mean and the median won't output as decimals. I have them set as doubles and to show the first decimal place, but instead, a 0 is inserted where the number should be. For example, my list of numbers is: 25, 61, 65, 72, 82, 98. While my median should be 68.5, it is displayed as 68.0 My mean should be 67.2 but is displayed as 67.0. Here is my code:

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<fstream.h>
    
    void program_description();
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items );
    void display_array( int arr[], int no_items );
    void sort_ascending( int arr[], int no_items );
    int get_total( int arr[], int no_items );
    int calc_range( int arr[], int no_items );
    double calc_mean( int sum, int no_items );
    double calc_median( int arr[], int no_items );
    
    const int max_list = 50;
    
    int main()
    {
    	//Variable Declarations
    	int no_values;
    	int total;
    	int array[max_list];
    	ifstream in_file;
    
    	no_values = 0;
    
    	program_description();
    
    	get_info_from_file( in_file, array, no_values );
    
    	display_array( array, no_values );
    
    	sort_ascending( array, no_values );
    
    	get_total( array, no_values );
    
    	total = get_total( array, no_values );
    
    	calc_range( array, no_values );
    
    	calc_mean( total, no_values );
    
    	calc_median( array, no_values );
    
    	return 0;
    }
    
    //Function Definitions
    
    void program_description()
    /*========================================================================
    This function outputs a description of the program to the user.
    	INPUT: None
    	OUTPUT: None
    ========================================================================*/
    {
    	cout << "Greetings!  This program is designed to take the ";
    	cout << "data currently stored in\nanother file and display it to you.  It will then ";
    	cout << "sort the data, number it,\nand calculate the sum, range, mean, and median.  ";
    	cout << "From there a histogram\nof the data is displayed.\n\n";
    }
    
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items )
    /*========================================================================
    This function opens the input stream, gets the data from the file one item at a time, and 
    stores each item in an array element.  It also counts the number of items in the file.  
    Lastly, the input stream is closed.
    	INPUT: A parameter of type ifstream, an array of integers, an integer which
    			represents the number of items in the file.
    	OUTPUT: This function returns nothing.
    ========================================================================*/
    {
    	int data;
    	no_items = 0;
    
    	i_file.open("stat_data");
    	i_file >> data;
    	arr[no_items] = data;
    	no_items ++;
    	
    	
    	while ( ! i_file.eof() )
    	{
    		i_file >> data;
    		
    		if ( ! i_file.eof() )
    		{
    			arr[no_items] = data;
    			no_items ++;
    		}
    	}
    
    	i_file.close();
    
    	cout << "The number of items in this array is: " << no_items;
    	cout << endl << endl;
    
    	cout << "The array is:\n\n";
    
    	
    }
    
    void display_array( int arr[], int no_items )
    /*========================================================================
    The function traverses the array of integers, outputting each element in the array.
    	INPUT: an array of integers, an integer which represents the number of items in
    			the file.
    	OUTPUT: This function returns nothing.
    ========================================================================*/
    {
    	for ( int i = 0; i < no_items; i++ )
    	{
    		cout << arr[i] << " ";
    	}
    
    	cout << endl << endl;
    }
    
    void sort_ascending( int arr[], int no_items )
    /*========================================================================
    This function sorts an array of integers in ascending order using a selection sort.
    	INPUT: an array of integers, an integer which represents the number of items.
    	OUPUT: This function returns nothing.
    ========================================================================*/
    {
    	int i, hold;
    
    	cout << "This is the array sorted in ascending order: " << endl << endl;
    	
    	for ( int pass = 0; pass < no_items - 1; pass++ )
    
    		for ( i = 0; i < no_items - 1; i++ )
    
    			if (arr[i] > arr[i + 1] )
    			{
    				hold = arr[i];
    				arr[i] = arr[i +1];
    				arr[i+1] = hold;
    			}
    
    	for ( i = 0; i < no_items; i++ )
    		cout << arr[i] << " ";
    		cout << endl;
    
    	cout << endl << endl;
    
    }
    
    int get_total( int arr[], int no_items )
    /*========================================================================
    This function traverses the array of integers and totals the elements in the array.
    	INPUT: an array of integers, an integer which represents the number of items 
    			in the file
    	OUPUT: This function returns an integer which represents the total of all of
    			the elements.
    ========================================================================*/
    {
    	int sum;
    
    	sum = 0;
    
    	for ( int i = 0; i < no_items; i++ )
    	{
    		sum = arr[i]+sum;
    	}
    
    	cout << "The sum of all the data is: " << sum << endl << endl;
    
    	return sum;
    }
    
    int calc_range( int arr[], int no_items )
    /*========================================================================
    This function calculates the range of the values (highest value - lowest value) for
    an array of integers.  It is assumed that the list has already been sorted.
    	INPUT: an array of integers, an integer which represents the number of items.
    	OUTPUT: This function returns an integers which reprsents the range.
    ========================================================================*/
    {
    	int range;
    
    	range = arr[no_items - 1] - arr[0];
    	
    	cout << "The range of the data is: " << range << endl << endl;
    
    	return range;
    }
    
    double calc_mean( int sum, int no_items )
    /*========================================================================
    This function calculates the mean (arthimetic average) given the total of the elements
    and the number of items.
    	INPUT: an integer that represents a sum total, an integer which represents the
    			number of items
    	OUTPUT: this function returns a double that represents the mean
    ========================================================================*/
    {
    	double mean;
    
    	cout << setiosflags (ios:: fixed);
    	cout << setiosflags (ios:: showpoint);
    	cout << setprecision(1);
    
    	mean = sum / (no_items);
    
    	cout << "The mean of the data is: " << mean;
    	cout << endl << endl;
    
    	return mean;
    }
    
    double calc_median( int arr[], int no_items )
    /*========================================================================
    This function calculates the media (middle score) of a set of numbers.  It is assumed
    that the list has already been sorted.
    	INPUT: an array of integers, an integer which represents the number of items
    	OUTPUT: this function returns an integer which represents the median
    ========================================================================*/
    {
    	int median;
    	double median_average;
    	int median_1;
    	int median_2;
    	int no_scores;
    
    	cout << setiosflags (ios:: fixed);
    	cout << setiosflags (ios:: showpoint);
    	cout << setprecision(1);
    	
    	no_scores = no_items;
    
    	no_scores = no_scores % 2;
    
    	if ( no_scores == 0 )
    	{
    		cout << setiosflags (ios:: fixed);
    		cout << setiosflags (ios:: showpoint);
    		cout << setprecision(1);
    		
    		median_1 = arr[(no_items / 2) - 1];
    		median_2 = arr[no_items / 2];
    
    		median_average = (median_1 + median_2) / 2;
    
    		cout << "There is an even number of items. " << endl;
    		cout << "The median of the data is: " << median_average << endl << endl;
    	}
    
    	else
    	{
    
    		median = arr[no_items / 2];
    		
    		cout << "There is an even odd of items." << endl;
    		cout << "The median of the data is: " << median << endl << endl;
    
    	}
    
    	return median;
    }
    Programming With: Microsoft Visual C++ 6.0

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    An integer divided by an integer will result in an integer. So, te solution is..
    Code:
    mean = static_cast<double>(sum) / (no_items);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User fjf314's Avatar
    Join Date
    Jan 2004
    Posts
    13
    Thanks. I had never seen "static_cast<double>" before, but it did the trick. While I never would have figured out what was wrong with the mean, the median was my own fault, in leaving median_1 and median_1 as integers. All I had to do was change the code in my median function to:

    Code:
    	int median;
    	double median_average;
    	double median_1;
    	double median_2;
    	int no_scores;
    Programming With: Microsoft Visual C++ 6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary to decimal
    By jk81 in forum C Programming
    Replies: 1
    Last Post: 09-13-2002, 05:20 AM
  2. Replies: 10
    Last Post: 06-12-2002, 03:15 PM
  3. Decimal to binary conversion help needed
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 02-06-2002, 12:03 PM
  4. Help with decimal to hexidecimal program
    By CheeseMonkey in forum C++ Programming
    Replies: 1
    Last Post: 12-22-2001, 02:15 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

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