#include "stdafx.h"
#include <stdlib.h>
#include <ctime>
#include <iostream>
usingnamespace std;
#define ARRAY_SIZE 1000
int main()
{
int arra[ARRAY_SIZE]; //array of thousand values
srand(time(0)); //initializing srand function with time
//filling array with thousand random values
for(int i=0;i<ARRAY_SIZE;i++)
arra[i]=rand()%ARRAY_SIZE+1;
cout<<"\nList elements before sorting: "<<endl;
for(int i=0;i<ARRAY_SIZE;i++)
cout<<arra[i]<<" ";
//sorting the array elements
for(int i=0;i<ARRAY_SIZE-1;i++)
for(int j=i+1;j<ARRAY_SIZE;j++)
{
if(arra[i]>arra[j])
{
int tmp=arra[i];
arra[i]=arra[j];
arra[j]=tmp;
}
}
cout<<"\n\nList elements after sorting: "<<endl;
for(int i=0;i<ARRAY_SIZE;i++)
cout<<arra[i]<<" ";
//Binary Search
cout<<"\n\nBinary Serach: "<<endl;
int searchElement;
cout<<"Enter Searching Element: ";
cin>>searchElement;
int min=0;
int max=ARRAY_SIZE-1;
int mid=(min+max)/2;
int flag=false;
int numOfComparisions=0;
int position;
while(min<=max)
{
if(arra[mid]==searchElement)
{
flag=true;
position=mid+1;
break;
}
elseif(arra[mid]<searchElement)
min=mid+1;
else
max=mid-1;
numOfComparisions++;
mid=(min+max)/2;
}
if(flag)
cout<<endl<<searchElement<<" is found at "<<position<< " position"<<endl;
else
cout<<searchElement<<" is not found in the list"<<endl;
cout<<"Number of comparisions: "<<numOfComparisions<<endl;
//Sequential Search for sorted list
cout<<"\nSequential Search: "<<endl;
cout<<"Enter Searching Element: ";
cin>>searchElement;
numOfComparisions=0;
flag=false;
for(int i=0;i<ARRAY_SIZE;i++)
{
if(arra[i]==searchElement)
{
flag=true;
position=i+1;
break;
}
numOfComparisions++;
}
if(flag)
cout<<endl<<searchElement<<" is found at "<<position<< " position"<<endl;
else
cout<<searchElement<<" is not found in the list"<<endl;
cout<<"Number of comparisions: "<<numOfComparisions<<endl;
system("pause");
return 0;
}
My code compiles just fine without a problem but after building the code it throws this warning:
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
which is referring to this line:
srand(time(0)); //initializing srand function with time
I dont get it, what does it want me to do. i know its not an error but i would rather fix it perfectly. Any idea what should i change it to???
time() function return time_t type, which is signed type (signed integer to be precise), which can represent negative values. srand() function takes unsigned integer, which canot represent negative values, so there can be some data loss in conversion.
But if you won't change your PC date to 60th or before, time(0) will not return negative values.
TL;DR: You'll be fine
EDIT: There will not be any loss if you look how srand() use it's argument, so you will be absolutely fine.