-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastTen.cpp
412 lines (302 loc) · 8.97 KB
/
lastTen.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "lastTen.h"
//11. Write a function that searches for the first occurrence of a given character c in a given string s.
//Return the index of the first occurrence or -1 if not found.
int objectLast::oneP(char* s, char c)
{
for (int i=0; i<strlen(s);i++){
if (*(s+i)==c){
return i;
}
}
return -1;
}
//12. Write a function that deletes the first occurrence of a given character c from a given string s.
//Return 1 if the deletion took place or 0 if no deletion occurred.
int objectLast::twoP(char* s, char c)
{
int deleted = oneP(s, c);
int arrayLen = strlen(s);
char tempStr[arrayLen+1];
int tempStrI=0;
bool deletedSomething = false;
if (deleted<0){
return 0;
}
for (int i=0;i<arrayLen;i++){
if (i==deleted){
deletedSomething=true;
//cout<<"hello"<<endl;
continue;
//cout<<"bye"<<endl;
}
tempStr[tempStrI] = *(s+i);
tempStrI++;
}
for (int j=0;j<arrayLen-1;j++){
*(s+j)=tempStr[j];
}
if (deletedSomething){
return 1;
}
else
{
return 0;
}
}
int objectLast::twoP(char* s, int deleted) //more overloading, specifically for question 6P.
{
//int deleted = oneP(s, c);
int arrayLen = strlen(s);
char tempStr[arrayLen+1];
int tempStrI=0;
bool deletedSomething = false;
if (deleted<0||deleted>arrayLen){
return 0;
}
for (int i=0;i<arrayLen;i++){
if (i==deleted){
deletedSomething=true;
continue;
}
tempStr[tempStrI] = *(s+i);
tempStrI++;
}
for (int j=0;j<arrayLen-1;j++){
*(s+j)=tempStr[j];
}
if (deletedSomething){
return 1;
}
else
{
return 0;
}
}
//13. Write a function that searches an unsorted array of integers for the first occurrence of the value key.
//If key is found then return the index in the array where it was found else return -1 to indicate that key was not found.
int objectLast::threeP(int* inputInts, int key, int length)
{
for (int i=0;i<length;i++){
if (*(inputInts+i)==key){
return i;
}
}
return -1;
}
//14. Repeat question 10 only now you will be given a sorted array of integers.
//10. Write a function that determines the third largest value in an array of integers. Your function must return 1 if it succeeds and 0 if it does not succeed.
//The function will not succeed if there are less than 3 elements in the array. Note that the calling function has every intention of using this third largest value! Also, YOU ARE NOT ALLOWED TO SORT THE ARRAY!
int objectLast::fourP(int* inputInts, int* output, int length)
{
if (length<3){
return 0;
}
*output = inputInts[length-3];
return 1;
}
//15. Write a function that searches a string s for a substring sub.
//If sub is found in s then return the index of the first char of sub occurring in s, otherwise return -1 to indicate that the substring was not found.
int objectLast::fiveP(char* s, char* sub)
{
int lenOne = strlen(s);
int lenTwo = strlen(sub);
bool found = false;
int i,j,k;
if (lenOne<lenTwo){
return -1;
}
j=0;
for (int i=0;i<(lenOne-lenTwo);i++){
for (j=0;j<lenTwo;j++){
found =true;
if(*(s+i+j)!=*(sub+j)){
found=false;
}
}
if (found){
return i;
}
}
return -1;
}
//16. Write a function that deletes the first occurrence of the substring sub in the given string s.
//If sub is deleted then return 1, otherwise return 0 to indicate that the substring was not deleted.
int objectLast::sixP(char* s, char* sub)
{
int lenOne = strlen(s);
int lenTwo = strlen(sub);
int deleted = fiveP(s, sub);
int deleteTrue = 0;
for (int i=0;i<lenTwo;i++){
deleteTrue=twoP(s, deleted);
if (deleteTrue==0){
return 0;
}
}
return 1;
}
//17. Write a function that returns the minimum number of coins required to make up a certain money amount given in cents.
//Assume that you can use loonies, quarters, dimes, nickels, and pennies. For example, if you pass a money total of 789 cents to your function then your function should return 15.
//It takes a minimum of 15 these coins to make up 789 cents.
int objectLast::sevenP(int centValue)
{
int valueRemaining=centValue;
int numCoins=0;
int coinSize[5]={100,25,10,5,1};
int numTypeCoin=5;
for (int i=0;i<numTypeCoin;i++){
while (valueRemaining>coinSize[i]){
valueRemaining-=coinSize[i];
numCoins++;
}
}
return numCoins;
}
//18. Write a function that takes a 32-bit binary number in the form of an eight digit hex number (stored as a string)
//and returns the two's complement of the number also as an eight digit hex number.
//Will reject anything that contains invalid characters, NOT 0-9, a-f, A-F
//output is a 9 character long array.
int objectLast::eightP(char* hexIn, char* output) //Slightly overengineered...
{
char tempArray[8+1];
unsigned char tempArrayInt[8+1];
unsigned char tempInt=0;
char temp;
for (int i=0;i<8;i++){
temp= *(hexIn+i);
if (temp>='a'&&temp<='f'){
tempInt=temp-'a'+10;
tempArrayInt[i]=tempInt;
continue;
}
if (temp>='A'&&temp<='F'){
tempInt=temp-'A'+10;
tempArrayInt[i]=tempInt;
continue;
}
if (temp>='0'&&temp<='9'){
tempInt=temp-'0';
tempArrayInt[i]=tempInt;
continue;
}
return 0; //failure, invalid character
}
tempInt=0;
for (int j=0;j<8;j++){
tempInt=(tempArrayInt[j]^0xF);
//tempInt = (~tempArrayInt[j]); Will this work?
if (tempInt<=9){
tempArray[j]='0'+tempInt;
}
if (tempInt>9&&tempInt<=15){
tempArray[j]='A'+tempInt-10;
}
}
for (int k=0;k<8;k++){
*(output+k) = tempArray[k];
}
return 1;
}
//19. Write a function that takes a single array of integers in which the values from index lo to mid
//are sorted AND the values from index mid + 1 to hi are sorted. After the function call the values from lo to hi must be sorted.
int objectLast::nineP(int* arrayInt, int lo, int mid, int hi, int length)
{
int tempArray[length+4];
int tempArrayPart[hi-lo];
int tempArraySorted[hi-lo];
int midTempArray = mid - lo;
int hiTempArray = hi - lo;
int j=0;
int* P_lo = tempArrayPart;
int* P_mid = tempArrayPart+(mid-lo+1);
for (int a=0;a<length;a++){
tempArray[a]=*(arrayInt+a);
}
for (int b=lo;b<hi;b++){
tempArrayPart[b-lo]=tempArray[b];
}
bool P_loEnd =false;
while (true){
if (P_lo==tempArrayPart+(mid-lo+1)){
P_loEnd=true;//P_lo has ended
break;
}
if (P_mid==tempArrayPart+(hi-lo+1)){
break;
}
if (*P_lo>*P_mid){
tempArraySorted[j]=*P_lo;
P_lo++;
j++;
}
if (*P_lo<*P_mid){
tempArraySorted[j]=*P_mid;
P_mid++;
j++;
}
}
while (P_loEnd==true){
tempArraySorted[j]=*P_mid;
j++;
P_mid++;
if (P_mid==tempArrayPart+(hi-lo+1)){
break;
}
}
while (P_loEnd!=true){
tempArraySorted[j]=*P_lo;
j++;
P_lo++;
if (P_mid==tempArrayPart+(mid-lo+1)){
break;
}
}
j=0;
for (int k=lo;k<hi;k++){
tempArray[k]=tempArraySorted[j];
j++;
}
for (int l=0;l<length;l++){
*(arrayInt+l)=tempArray[l];
}
return 1;
}
//20. Write a function that takes an array of integers, randomly selects an integer in the array and then processes the array
//so that this randomly selected integer is placed in the array into an index in the array such that all of the numbers at lower indices
//are less than or equal to this number and all of numbers at higher indices are greater than this number.
int objectLast::tenP(int* anArray, int* output, int length)
{
//Could use quicksort or binary tree?
srand (time(NULL));
int randNumValue=0;
int randNumPos=0;
int randNumPos = rand ()%(length);
int randNumValue =*(anArray+ randNumPos);
int outputArray[length];
int l=0;//counter for the above array;
int smallArray[length];//contains numbers smaller than the selected number.
int largeArray[length];//contains numbers bigger than the selected number.
int j,k;//j is for small array, k is for large array.
j=0;k=0;
for (int i=0;i<length;i++){
if (*(anArray+i)>randNumValue){
largeArray[k]=*(anArray+i);
k++;
}
if (*(anArray+i)<=randNumValue){
smallArray[j]=*(anArray+i);
j++;
}
}
for (int a = 0;a<j;a++){
outputArray[a]=smallArray[a];
}
outputArray[j+1]=randNumValue;
for (int a = j+2;a<length;a++){
outputArray[a]=smallArray[a];
}
for (int a = 0;a<length;a++){
output[a]=outputArray[a];
}
return 1;
}