-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment6.c
More file actions
482 lines (398 loc) · 11.5 KB
/
Copy pathAssignment6.c
File metadata and controls
482 lines (398 loc) · 11.5 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//A
#include "stdio.h"
#define SIZE 10
int main(void) {
unsigned int h[SIZE] = {0};
scanf("%u%u%u%u%u%u%u%u%u%u", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], &h[8], &h[9]);
for(int i=0; i<SIZE;i++){
for(int j = 0; j<h[i];j++){
printf("%s","b");
}
printf("\n");
}
return 0;
}
//B
#include "stdio.h"
#define SIZE 10
int main(void) {
unsigned int h[SIZE] = {0};
double avg = 0;
scanf("%u%u%u%u%u%u%u%u%u%u", &h[0], &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], &h[8], &h[9]);
for(int i=0; i<SIZE;i++){
avg += h[i];
}
printf("%.1f",avg/10);
return 0;
}
//C
/******************************************************************************
Create a program to read a variable number of elements, store them in the array
a, passes a to the function computeMode which computes the mode number over the
array, and then, the program prints the result of computeMode.
*******************************************************************************/
#include <stdio.h>
void computeMode(int size,int a[]){
int ans = 0;
int mode = 0;
for(int i = 0; i < size; i++){
int counter = 0;
for(int j = 0; j < size; j++){
if(a[i] == a[j]){
counter ++;
}
}
if(counter>mode){
ans = a[i];
mode = counter;
}
}
printf("%i", ans);
}
int main(void)
{
int size = 0;
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
computeMode(size, a);
return 0;
}
//D
/******************************************************************************
Create a program to read a variable number of elements, store them in the array a,
passes a to the function computeMax which computes the maximun number iterating
over the array, and then, the program prints the result of computeMax.
*******************************************************************************/
#include <stdio.h>
void computeMax(int size, int a[]){
int max = a[0];
for(int i = 0; i < size; i++){
max = (a[i]>max)?a[i]:max;
}
printf("%i", max);
}
int main(void)
{
int size = 0;
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
computeMax(size,a);
return 0;
}
//E
/******************************************************************************
Complete the program to read a variable number of elements, store them in the
array a, passes a to the function computeMin which computes the minimum number
iterating over the array, and then, the program prints the result of computeMin.
*******************************************************************************/
#include <stdio.h>
void computeMin(int size, int a[]){
int min = a[0];
for(int i = 0; i < size; i++){
min = (a[i]<min)?a[i]:min;
}
printf("%i", min);
}
int main(void)
{
int size = 0;
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
computeMin(size,a);
return 0;
}
//F
/******************************************************************************
Create a program to read a variable number of elements, store them in the array
a, passes a to the function computeMedina which sorts the array, and then,
computes the median.
*******************************************************************************/
#include <stdio.h>
int * bubbleSort (int size, int a[]){
for(int i = 0; i< size-1; i++){
for(int j = 0; j < size-i-1; j++){
int n1 = a[j];
int n2 = a[j+1];
if(n1 > n2){
a[j] = n2;
a[j+1] = n1;
}
}
}
return a;
}
void computeMedina(int size,int a[]){
float ans = 0;
int *b;
b = bubbleSort(size, a);
if(size % 2 == 0){
int mid1 = (size-1)/2;
int mid2 = mid1+1;
int n1 = a[mid1];
int n2 = a[mid2];
ans = (float)(n1+n2)/2;
}else{
int mid = size/2;
ans = (float) a[mid];
}
printf("%.1f", ans);
}
int main(void)
{
int size = 0;
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
computeMedina(size, a);
return 0;
}
//G
/******************************************************************************
Create a program to read a variable number of elements, store them in the array
a, passes a to the function searchValue which sorts the array, performs binary
search, and returns the index of the closes value.
*******************************************************************************/
#include <stdio.h>
int * bubbleSort (int size, int a[]){
for(int i = 0; i< size-1; i++){
for(int j = 0; j < size-i-1; j++){
int n1 = a[j];
int n2 = a[j+1];
if(n1 > n2){
a[j] = n2;
a[j+1] = n1;
}
}
}
/*
for(int i = 0; i< size; i++){
printf("%i ",a[i]);
}*/
return a;
}
int binarySearch(int a[], int leftIndex, int rightIndex, int n, int closest){
if (rightIndex >= leftIndex) {
int mid = leftIndex + (rightIndex - leftIndex) / 2;
if(a[mid]== n){
//printf(" closest: %i\n", closest);
return mid;
}
else if(a[mid] > n ){
rightIndex = mid - 1;
//printf(" closest: %i\n", closest);
return binarySearch(a, leftIndex, rightIndex, n , mid);
}
else{
leftIndex = mid + 1;
//printf(" closest: %i\n", closest);
return binarySearch(a, leftIndex, rightIndex, n , mid);
}
}
return closest;
}
int searchValue(int size, int value, int a[]){
int ans = 0;
int *b;
b = bubbleSort(size, a);
ans = binarySearch(a,0,size,value,0);
return ans;
}
int main()
{
int size = 0;
int value = 0;
scanf("%i",&size);
scanf("%i",&value);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
int ans = searchValue(size,value, a);
printf("%i",ans);
return 0;
}
//H
/******************************************************************************
Create a program to read a variable number of elements, store them in the array a,
passes a to the function bubbleSort which sorts the array, and then, the program
prints the sorted array.
*******************************************************************************/
#include <stdio.h>
void bubbleSort (int size, int a[]){
for(int i = 0; i< size-1; i++){
for(int j = 0; j < size-i-1; j++){
int n1 = a[j];
int n2 = a[j+1];
if(n1 > n2){
a[j] = n2;
a[j+1] = n1;
}
}
}
for(int i = 0; i< size; i++){
printf("%i \n",a[i]);
}
}
int main(void)
{
int size = 0;
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
bubbleSort(size, a);
return 0;
}
//I
/******************************************************************************
Write a static method named range that takes an array of integers as a parameter
and returns the range of values contained in the array. The range of an array is
defined to be one more than the difference between its largest and smallest element.
For example, if the largest element in the array is 15 and the smallest is 4,
the range is 12. If the largest and smallest values are the same, the range is 1.
*******************************************************************************/
#include <stdio.h>
int * bubbleSort (int size, int a[]){
for(int i = 0; i< size-1; i++){
for(int j = 0; j < size-i-1; j++){
int n1 = a[j];
int n2 = a[j+1];
if(n1 > n2){
a[j] = n2;
a[j+1] = n1;
}
}
}
return a;
}
int range(int size, int a[]){
int ans = 0;
int *b;
b = bubbleSort(size, a);
ans = b[size-1] - b[0] +1;
return ans;
}
int main()
{
int size = 0;
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
int ans = range(size, a);
printf("%i",ans);
return 0;
}
//J
/******************************************************************************
Write a method called countInRange that accepts an array of integers, a minimum
value, and a maximum value as parameters and returns the count of how many elements
from the array fall between the minimum and maximum (inclusive). For example, in
the array {14, 1, 22, 17, 36, 7, -43, 5}, there are four elements whose values
fall between 4 and 17. IMPORTANT: Do not sort the array to solve this problem.
The first value entered in the console corresponds to the minimum value; the
second, to the maximum; the third, to the array size; and the rest, to the
elements of the array.
*******************************************************************************/
#include <stdio.h>
int countInRange (int min, int max, int size, int a[]){
int ans = 0;
for(int i = 0; i < size; i++){
if(a[i]<=max && a[i]>= min){
ans ++;
}
}
return ans;
}
int main()
{
int size = 0;
int min = 0;
int max = 0;
scanf("%i",&min);
scanf("%i",&max);
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
int ans = countInRange(min ,max, size, a);
printf("%i",ans);
return 0;
}
//K
/******************************************************************************
Write a method called countInRange that accepts an array of integers bids and
an integer price as parameters.
The function returns the element in the bids array that is closest in value
to price without being larger than price.
For example, if bids stores the elements {200, 300, 250, 999, 40}, then
priceIsRight(bids, 280) should return 250, since 250 is the bid closest to 280
without going over 280. If all bids are larger than price, then your method
should return -1.
You may assume there is at least 1 element in the array, and you may assume that
the price and the values in bids will all be greater than or equal to 1. Do not
modify the contents of the array passed to your method as a parameter.
Assume that the first element entered in the console is the price, the second
is the array size, while the rest are the bids.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int countInRange(int price, int size,int a[]){
int ans = -1;
int closestDif = 9999999;
for(int i = 0; i<size; i++){
int n = a[i];
int dif = abs(price - n);
if(dif < closestDif && n <= price){
ans = n;
closestDif = dif;
}
}
return ans;
}
int main(void)
{
int size = 0;
int price = 0;
scanf("%i",&price);
scanf("%i",&size);
int a[size];
for(int i = 0; i < size; i++){
int n = 0;
scanf("%i", &n);
a[i] = n;
}
int bid = countInRange(price, size, a);
printf("%i", bid);
return 0;
}