forked from carlosemello/MuM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMuUtil.cpp
More file actions
181 lines (156 loc) · 3.06 KB
/
MuUtil.cpp
File metadata and controls
181 lines (156 loc) · 3.06 KB
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
172
173
174
175
176
177
178
179
180
181
//*********************************************
//***************** NCM-UnB *******************
//******** (c) Carlos Eduardo Mello ***********
//*********************************************
// This softwre may be freely reproduced,
// copied, modified, and reused, as long as
// it retains, in all forms, the above credits.
//*********************************************
/** @file MuUtil.cpp
*
* @brief Utility Functions Implementation
*
* @author Carlos Eduardo Mello
* @date 3/5/2009
*
* @details
* MuM utilities includes functions which are used by MuM classes
* to perform routine tasks such as initializing arrays ( Set() ),
* picking random values within a range ( Between() ), etc.
**/
#include "MuUtil.h"
void MuInit(void)
{
srand((int)time((0)));
}
void Set(int * array, int n, int value)
{
int i;
if(n > 0)
{
for(i = 0; i < n; i++)
{
array[i] = value;
}
}
else
{
if(n == 0)
{
for(i = 0; i < value; i++)
{
array[i] = i;
}
}
}
}
extern int Between( int low, int high )
{
int temp;
int range;
if(high == low)
return high;
else
{
if(high < low)
{
temp = low;
low = high;
high = temp;
}
}
high++; // to include upper limit
range = high - low;
return ( ( rand()% range ) + low );
}
extern float Between(float min, float max)
{
return (rand()*((max-min)/RAND_MAX)) + min;
}
// Tries to find a short inside an array of shorts
// returns the index number of found value, or -1
// if value not found
extern short Inside(short value, short * array, short n )
{
short index = -1;
int i = 0;
for( i = 0; i < n; i++ )
{
if( value == array[i] )
{
index = i;
break;
}
}
return index;
}
// Mixes the input array
extern void MixInts( int * array, int n)
{
int i, j, pick;
int * bag;
int limit = n-1;
bag = new int[n];
if(bag)
{
for(i = 0; i < n; i++)
bag[i] = array[i];
// fill new array one item at a time...
for(i = 0 ; i < n; i++)
{
// picking a random position in input array...
pick = Between( 0, limit );
array[i] = bag[pick];
// shrinking the number of items in input array.
for(j = pick; j < limit; j++)
bag[j] = bag[j+1];
// eliminate selected option
limit--;
}
delete [] bag;
}
}
// Sorts (bubble) an array of ints
extern void SortInts( int * array, int n)
{
int i, j;
for(i = n; i >= 1; i-- )
{
for( j = 0; j < i-1; j++ )
{
if( array[j] > array[j+1] )
{
int temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
// Sorts (bubble) an array of floats
extern void SortFloats( float * array, int n)
{
int i, j;
for(i = n; i >= 1; i-- )
{
for( j = 0; j < i-1; j++ )
{
if( array[j] > array[j+1] )
{
int temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
// Shows the contents of an integer array
extern void ShowInts( int * array, int n )
{
int i;
for( i = 0; i < n; i++ )
cout << array[i] << " ";
cout << endl;
}