double to int warning

This is a discussion on double to int warning within the C++ Programming forums, part of the General Programming Boards category; I'm getting the following warning in my comb sort program: Code: warning C4244: '/=': conversion from 'double' to 'int', possible ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    double to int warning

    I'm getting the following warning in my comb sort program:
    Code:
    warning C4244: '/=': conversion from 'double' to 'int', possible loss of data
    Here is my code:
    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    void FillArray(int array1[], int size);
    int NewGap(int gap);
    int comb_sort(int array1[], int size);
    void PrintArray(int array1[], int size);
    
    int main()
    {
    	const int size = 5000;		
    	int array1 [size];
    	int arraySizes [21] = {1,2,3,4,5,6,7,8,9,10,20,30,40,70,100,200,300,500,1000,2000,5000};		
    	int reverseArray [size];
    	int newSize = 0;
    
    	srand ((unsigned int)time((time_t)0));	
    	
    	cout<<"Randomly Generated:"<<endl;
    	for (int i = 0; i < 21; i++)
    	{
    		unsigned int sum = 0;
    
    		for(int j = 0; j <= 30; j++)
    		{
    			newSize = arraySizes[i];
    				
    			FillArray(array1, newSize);
    			sum+= comb_sort(array1, newSize);	//Averages the number of sorting operations of 30 randomly generated arrays.
    		}
    		cout.width(4);
    		cout<<newSize<<":  "<<sum/30<<endl;
    	}
    
    	cout<<endl<<"Sorted:"<<endl;
    	for (int k = 0; k < 21; k++)
    	{
    		newSize = arraySizes[k];
    		
    		cout.width(4);
    		cout<<newSize<<":  "<<comb_sort(array1, newSize)<<endl;
    	}
    
    	cout<<endl<<"Reverse Sorted:"<<endl;	
    	for(int m = 0; m < 5001; m++)
    	{
    			reverseArray[m] = array1[4999-m];	//Creates the reverse sorted array.
    	}
    	for (int l = 0; l < 21; l++)
    	{
    		newSize = arraySizes[l];		
    		
    		cout.width(4);
    		cout<<newSize<<":  "<<comb_sort(reverseArray, newSize)<<endl;
    	}	
    		
    	return 0;
    }
    //Creates the gap for use in the comb sort.
    //Input parameter: size of the array.
    int NewGap(int gap)
    {
            gap /= 1.3;
            if(gap == 9 || gap == 10)
                    gap = 11;
            if(gap < 1)
                    return 1;
            return gap;
    }
    //Sorts the array. Returns the number of operations to sort the array (count).
    //Input parameters: the inputed array, the size of the array.
    int comb_sort(int array1[], int size)
    {
    	int count = 0;
        int gap = size;                                 ++count;
        bool swapped;                                   ++count;
        do
    	{
            swapped = false;                            ++count;
            gap = NewGap(gap);                          ++count;
            for(int i=0;++count && i < size-gap; ++i)
    		{
               if(++count && array1[i] > array1[i+gap])
    		   {
    				swapped = true;                     ++count;
                    swap(array1[i], array1[i+gap]);     ++count;
               }
            }
         }while(++count && gap > 1 || swapped);
    
    	return count;
    }
    //Fills the array.
    //Input parameters: the array, the size of the array.
    void FillArray(int array1[], int size)
    {
    	int first_rand = 0;
    	
    	while(first_rand < size)
    	{
    		array1[first_rand] = rand() % 1000;	//Fills the array with random numbers.
    		first_rand++;	
    	}
    }
    The program runs fine, but i'm not suppose to have any warnings, so i need to fix it. I tried typecasting:
    Code:
    gap /= (int) 1.3;
    gap /= int (1.3);
    but, when i would run the program, it wouldn't work correctly. Maybe i'm not doing it correctly (not too familiar with typecasting).
    Any help is appreciated.
    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (int)1.3 just does what it says: cast 1.3 to int, so you end up dividing by 1. You can't cast the right-hand side when you're using the shortcut operators, so you'll have to do it the long way:
    Code:
    gap = (int)(gap/1.3);
    You're still losing precision, but at least you're doing it explicitly. (And casting should always round down to the next lowest integer; if you're supposed to round, you need to use the round function.)

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    if your program is running perfectly as is you could just add
    Code:
    #pragma warning( disable : 4244 )
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Thanks for the fix.
    IDE - Visual Studio 2005
    Windows XP Pro

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,635
    An easier fix, that doesn't rely on effectively telling the compiler to shut up and pretend a potential problem doesn't exist, is simply;
    Code:
       gap *= 10;
       gap /= 13;
    This relies on the initial value of gap being less than one tenth the value of the largest possible int supported by your implementation (so multiplying by 10 does not overflow). There will be minor differences in result due to rounding (integer division rounds towards zero) as well.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by abachler View Post
    if your program is running perfectly as is you could just add
    Code:
    #pragma warning( disable : 4244 )
    Don't disable warnings unless necessary. Disable one, disable all. Of course you can change where you can do it, but you're essentially ignoring the warning. Better to do it right than ignoring them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM

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