-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.c
More file actions
318 lines (256 loc) · 5.96 KB
/
project.c
File metadata and controls
318 lines (256 loc) · 5.96 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* ENGGEN131, Semester Two, 2017 */
/* PROJECT SOLUTION BY: << place your name and ID number here >> */
#include "project.h"
/* HELPER FUNCTIONS */
/* If you have defined any "helper" functions, which you call from the required */
/* functions, you should place them here, at the top of the source file. This will */
/* make it easier for the markers to locate them. You should also include a */
/* comment at the top of each of the "helper" functions you define which clearly */
/* describes their purpose, and which of the tasks they support. */
void Swap(int *a, int *b) {
int temp = *b;
*b = *a;
*a = temp;
}
int GCD2Finder(int a, int b) {
int temp,remainder;
if (a < b) {
Swap(&a, &b);
}
temp = b;
remainder = a%b;
while (remainder != 0) {
temp = remainder;
remainder = b%remainder;
}
return temp;
}
/* REQUIRED FUNCTIONS */
/* Implement each of the required functions below. The code that is provided initially */
/* for each function is incorrect - you should correct this! */
/* Your comment goes here*/
int DivisorOfThree(int a, int b, int c)
{
//Checking if Number is 0 or negative if not continuing with the code
int GCD;
if (a <=0 || b<=0 || c<=0 ) {
return -1;
}
if (a > b) {
Swap(&a, &b);
}
if (b > c) {
Swap(&b, &c);
}
if (a > b) {
Swap(&a, &b);
}
GCD = GCD2Finder(a, b);
return GCD2Finder(GCD, c);
}
/* Your comment goes here*/
double AverageSheep(int *counts)
{
int i = 0;
int counter,sum;
double average;
counter = 0;
sum = 0;
while (counts[i] != 9999) {
if (counts[i] != -1) {
sum +=counts[i];
counter++;
}
i++;
}
if (sum == 0) {
return 0.0;
}
average = (double)sum / counter;
return average;
}
/* Your comment goes here*/
void Emphasise(char* word)
{
// Find incidices of underscore
int strlength,lastinstance,firstinstance,i;
lastinstance = 0;
firstinstance = 0;
strlength = strlen(word);
//Finding last instance of uncderscore
for (i = 0; i < strlength; i++) {
if (word[i]== '_') {
lastinstance=i;
}
}
//Finding first instance of underscore
i = 0;
while (word[i] != '_') {
i++;
}
firstinstance = i;
for (i = firstinstance; i < lastinstance; i++) {
if (word[i] > 97 && word[i] < 122) {
word[i] -= 32;
}
}
for (i = firstinstance; i <= lastinstance; i++) {
word[i] = word[i + 1];
}
for (i = lastinstance-1; i < strlength-1; i++) {
word[i] = word[i + 2];
}
}
/* Your comment goes here*/
int PrimeFactors(int n, int *factors)
{
int divider = 2;
int counter=0;
while (n/divider >= 1) {
if (n%divider == 0) {
factors[counter] = divider;
counter++;
n = n/divider;
}
else {
divider++;
}
}
return counter;
}
/* Your comment goes here*/
void ConnectTwo(int maze[10][10])
{
//finding 1 and 2 locations
int start[2], finish[2],rowdirection, coldirection;;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (maze[i][j] == 1) {
start[0] = i;
start[1] = j;
}
else if (maze[i][j] == 2) {
finish[0] = i;
finish[1] = j;
}
}
}
rowdirection=((finish[0] - start[0]) > 0) ? 1 : -1;
coldirection = ((finish[1] - start[1]) > 0) ? 1 : -1;
while (start[0] != finish[0] && start[1] != finish[1]) {
start[0] += rowdirection;
start[1] += coldirection;
maze[start[0]][start[1]] = 3;
}
while (start[0] == finish[0] && start[1] != finish[1]) {
start[1] += coldirection;
maze[start[0]][start[1]] = 3;
}
while (start[1] == finish[1] && start[0] != finish[0]) {
start[0] += rowdirection;
maze[start[0]][start[1]] = 3;
}
}
/* Your comment goes here*/
void DayTrader(int *prices, int numPrices, int *bestRun, int *bestRunIndex)
{
int temp = 0;
int startindicies = 0;
int stoppingindicies = 0;
int value = 0;
int i = 0;
int j = 0;
while (i < numPrices) {
for (i = stoppingindicies+1; i <= numPrices; i++) {
stoppingindicies = i;
if (value > temp) {
temp = value;
startindicies = (stoppingindicies - value-1 );
}
if ((prices[i - 1] < prices[i]) /*&& i != numPrices -1 */) {
value++;
}
else {
value = 0;
break;
}
}
}
*bestRun = temp;
*bestRunIndex = startindicies;
}
/* Your comment goes here*/
void Compress(int *input, int *output)
{
int i = 0, length=0,counter=0,value=0,j=0,doublecounter=0,notcounter=0;
while (input[i] != -1) {
length++;
i++;
}
for (i = 0; i <= length-1; i++) {
if (input[i] == input[i+1]) {
value = input[i + 1];
counter+=2;
if (input[i + 1] == input[i + 2]) {
doublecounter++;
}
}
else {
if ((input[0] != input[1]) && (i!= (length-1))) {
notcounter++;
value = input[i];
output[j] = notcounter;
output[j + 1] = value;
j += 2;
}
else {
output[j] = counter - doublecounter;
output[j + 1] = value;
j += 2;
counter = 0;
}
}
}
output[j]=-1;
}
/* Your comment goes here*/
void AddOne(char *input, char *output)
{
int strlength,copyarray[MAX_ARRAY_SIZE];
strlength = strlen(input);
int i=strlength-1,j = 0;
if (input[strlength-1] == '9' && input[0] == '9') {
while (input[i] == '9') {
input[i] = '0';
i--;
}
for (i = 0; i < strlength; i++) {
output[j + 1] = input[i];
j++;
}
output[0] = '1';
output[strlength + 1] = 0;
}
else if(input[strlength-1]=='9'){
while (input[i] == '9') {
input[i] = '0';
i--;
}
input[i] += 1;
strcpy(output, input);
}
else {
input[strlength-1] += 1;
strcpy(output, input);
}
}
/* Your comment goes here*/
void Histogram(char *result, int *values, int numValues)
{
result[0] = (char)('~' + numValues + values[0]);
}
/* Your comment goes here*/
void GoldRush(int *results, int rows, int cols, int map[MAX_MAP_SIZE][MAX_MAP_SIZE], int bonus)
{
results[0] = 99993 + rows + cols + map[0][0] + bonus;
}