Warning C4244 'argument" conversion from time_t......

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#include "stdafx.h"

#include <stdlib.h>

#include <ctime>

#include <iostream>

using namespace 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;

}

else if(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???

Thanks guys
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.