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.