-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_utils (1).c
More file actions
76 lines (62 loc) · 1.52 KB
/
array_utils (1).c
File metadata and controls
76 lines (62 loc) · 1.52 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
/*//////////////////////////////////////////////
* AUTHOR = LI LINHAN
* ZHANG WENDIAN
* JIA JIAHUA
*
* DESCRIPTION: this is a source file for array_utils.h
* for CSCE 155E HACK7.0
*
* DATE: Oct.5, 2018
//////////////////////////////////////////////*/
#include <stdio.h>
#include <stdlib.h>
#include "array_utils.h"
int contains(const int *arr, int size, int x){
for (int i = 0; i < size; i++) {
if (arr[i] == x) {
return 1;
}
}
return 0;
}
int containsWithin(const int *arr, int size, int x, int i, int j){
for ( ; i < j+1; i++) {
if (arr[i] == x) {
return 1;
}
}
return 0;
}
int * paddedCopy(const int *arr, int oldSize, int newSize){
int *newArr = (int*)malloc(sizeof(int) * newSize);
int i;
/*assign value to the new array until the index reaches the
* smaller one of the two size*/
for ( i = 0; i < ((oldSize>newSize)?newSize:oldSize); i++) {
newArr[i] = arr[i];
}
/*if the newSize is smaller, then it's over
*if not, assign zeros until the index reaches the newSize*/
if (oldSize < newSize) {
for ( ; i < newSize; i++) {
newArr[i] = 0;
}
}
return newArr;
}
void reverse(int *arr, int size){
int temp;
for (int i = 0; i < size/2; i++) {
temp = arr[i];
arr[i] = arr[size-(1+i)];
arr[size-(1+i)] = temp;
}
return;
}
int * reverseCopy(const int *arr, int size){
int *newArr = (int*)malloc(size*sizeof(int));
for (int i = 0; i < size; i++) {
newArr[i] = arr[size-(1+i)];
}
return newArr;
}