-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.c
122 lines (92 loc) · 2.28 KB
/
q1.c
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
/* q1.c
*/
#include <stdlib.h>
#include <stdio.h>
#define NUM_UNIQUE_VALUES 256
#define NUM_SORT_OPTIONS 4
typedef unsigned int uint;
/* PURPOSE: To create from the heap and return an array of 'n' unsigned
* integers initialized to random values between 0 to
* 'NUM_UNIQUE_VALUES-1'.
*/
uint* initializeBigArray (uint n
)
{
uint i;
uint* dataPtr = (int*)calloc(n,sizeof(int));
for (i = 0; i < n; i++)
dataPtr[i] = (rand() % NUM_UNIQUE_VALUES);
return(dataPtr);
}
/* PURPOSE: To sort the 'arrayLen' elements in array 'array' by the O(N^2)
* expected running time 'bubble-sort' algorithm. No return value.
*/
void inefficientBubbleSort (uint arrayLen,
uint* array
)
{
uint i;
uint haveSwapped;
do
{
haveSwapped = 0;
for (i = 0; i < arrayLen-1; i++)
if (array[i] > array[i+1])
{
uint temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
haveSwapped = 1;
}
}
while (haveSwapped);
}
/* PURPOSE: To return the number of times that 'number' appears in array
* 'array' of length 'arrayLen'.
*/
uint countNum (uint arrayLen,
const uint* array,
uint number
)
{
uint count = 0;
uint i;
for (i = 0; i < arrayLen; i++)
if (array[i] == number)
count++;
return(count);
}
/* PURPOSE: To compute and return some arbitrary function that came out of the
* mind of Joe Phillips based upon array of integers 'array' of length
* 'arrayLen'.
*/
uint uselessCount (uint arrayLen,
uint* array
)
{
uint i;
uint j;
uint sum = 0;
for (i = 0; i < arrayLen/2; i++)
for (j = 0; j < arrayLen; j++)
if (array[i*2] == array[j])
sum += countNum(arrayLen,array,6);
inefficientBubbleSort(arrayLen,array);
for (i = 0; i < arrayLen/2; i++)
for (j = 0; j < arrayLen; j++)
if (array[i*2] == array[j])
sum -= countNum(arrayLen,array,7);
return(sum);
}
/* PURPOSE: To compute and print the value of some arbitrary function
* thought of by Joe Phillips. Ignores parameters. Returns 'EXIT_SUCCESS'
* to OS.
*/
int main (int argc, const char* argv[])
{
uint n = 18000;
uint* intArray= initializeBigArray(n);
printf("Useless value == %d\n",uselessCount(n,intArray));
free(intArray);
return(EXIT_SUCCESS);
}