-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_Array_Manipulation.c
More file actions
258 lines (191 loc) · 7.23 KB
/
Dynamic_Array_Manipulation.c
File metadata and controls
258 lines (191 loc) · 7.23 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIMIT 50 //macroconstant which is used throughout the program
typedef struct{
char * fname; //first name
char * lname; //last name
char * show; //favorite show
}record_t;
//all function prototypes
record_t * createArray(int maxsize);
record_t * insert(record_t *myarray, int *maxsize, int *currentsize);
void display(record_t *myarray, int currentsize);
record_t *doubleIt(record_t *arr, int size);
int removeRecord(record_t *arr, int size, int index);
void freeRecords(record_t *arr, int size);
int main(void)
{
int maxsize;
printf("Enter a size for the dynamic array: ");
scanf("%d", &maxsize);
record_t *myarray = createArray(maxsize); //allocates memory in the heap which the pointer points to
int currentsize = 0;
int option = -1;
while(option != 0)
{
printf("What would you like to do?\n");
printf("1: Insert a record\n");
printf("2: Display records\n");
printf("3: Remove record\n");
printf("4: Exit\n");
printf("Enter an option: ");
scanf("%d", &option);
while(!(option >= 1 && option <= 4))
{
printf("Enter an option: ");
scanf("%d", &option);
}
switch(option)
{
case 1:
printf("Insert was selected...\n");
myarray = insert(myarray, &maxsize, ¤tsize); //function call
break;
case 2:
printf("Display was selected...\n");
display(myarray, currentsize); //function call
break;
case 3:
printf("Remove was selected...\n");
printf("Select an index of record to remove...\n");
int index;
scanf("%d", &index);
while(!(index >= 0 && index < currentsize)) //while loop is used to check is the users input it valid or not.
{
printf("Select an index of record to remove...\n");
scanf("%d", &index);
}
currentsize = removeRecord(myarray,currentsize,index); //the currentsize changes after the removeRecord function is called.
break;
case 4:
printf("Exiting...\n");
option = 0;
break;
default:
printf("Invalid Selection...\n");
}
}
freeRecords(myarray, currentsize); //all memory is the heap will be returned back
free(myarray);
myarray = NULL; //makes sure the pointer is not pointing to anything.
return 0;
}
//this function uses the malloc function to allocate memory equal to the size of the typedef struct declared.
record_t * createArray(int maxsize)
{
record_t * arr = (record_t *) malloc(maxsize * sizeof(record_t));
//if statement is used to make sure the pointer is pointing to something in the heap.
if(arr == NULL)
{
printf("malloc error in createArray...program exiting\n");
exit(1);
}
return arr;
}
//display function is used to display all of the elements on the array.
void display(record_t *myarray, int currentsize)
{
printf("---------------------------------\n");
//if statement is used to see which address the pointer is pointing to.
if(myarray == NULL)
printf("Hey this is null\n");
//iterates through every index of the array to print out all of the eleements.
for(int x = 0; x < currentsize; ++x)
{
printf("myarray[%d].fname = %s\n", x, myarray[x].fname);
printf("myarray[%d].lname = %s\n", x, myarray[x].lname);
printf("myarray[%d].show = %s\n", x, myarray[x].show);
}
printf("---------------------------------\n");
}
//main part of the program, it is used to add to *myarray, and add a record to the system
record_t * insert(record_t *myarray, int *maxsize, int *currentsize)
{
//if the currentsize is greater then the maxsize then the arrays total element count needs to be doubled by allocating more memory.
if(*currentsize >= *maxsize)
{
printf("Array is full...Need to doubleIt...\n");
myarray = doubleIt(myarray, *currentsize);
*maxsize = *maxsize * 2; //maxsize is doubled because double the memory has been allocated into the heap.
}
char FIRST[LIMIT];
char LAST[LIMIT];
char SHOW[LIMIT];
char stdinINPUT;
printf("Enter the first name: ");
scanf(" %s", FIRST);
printf("Enter the last name: ");
scanf(" %s", LAST);
stdinINPUT = getc(stdin); //this is used because scanf adds a newline character in the buffer. getc reads the newline character.
printf("Enter favorite show: ");
fgets(SHOW, LIMIT, stdin);
//each pointer in the struct allocates memory so the pointer can point to memory in the heap.
myarray[*currentsize].fname = (char *) malloc(sizeof(char) * LIMIT);
myarray[*currentsize].lname = (char *) malloc(sizeof(char) * LIMIT);
myarray[*currentsize].show = (char *) malloc(sizeof(char) * LIMIT);
//checks if the pointer is pointing to an address or not.
if(myarray == NULL)
{
printf("The array is pointing to a null val\n");
}
//strcpy is used to copy the hardcoded strings into the dynamic array.
strcpy(myarray[*currentsize].fname, FIRST);
strcpy(myarray[*currentsize].lname, LAST);
strcpy(myarray[*currentsize].show, SHOW);
(*currentsize)++; //*currentsize = *currentsize + 1
return myarray;
}
//function removes a record in the system.
int removeRecord(record_t *arr, int size, int index)
{
//makes sure the index is valid.
if (index < 0 || index >= size) {
printf("Invalid Index\n");
return -1;
}
//because an index is being deleted, the memory needs to be returned back to the computer from the heap.
free(arr[index].fname);
free(arr[index].lname);
free(arr[index].show);
//loop goes through the array and performs a shift because an index will be deleted.
for(int i = 0; i < size-1; ++i)
{
if(i >= index)
{
arr[i] = arr[i + 1];
}
}
(size) --; //size is decremented.
return size;
}
record_t * doubleIt(record_t *arr, int size)
{
record_t * new_arr = (record_t *) malloc(size * sizeof(record_t) * 2); //double the amount of memory is allocated into a new array
//appends all of *arr to the new array.
for(int x = 0; x < size; ++x)
{
new_arr[x] = arr[x];
}
free(arr); //the previous array is being freed because a new array was created and memory was allocated to it.
arr = NULL;
return new_arr;
}
//this function iterates through array to free all of the memory in the heap
void freeRecords(record_t *arr, int currsize)
{
//frees all of the memory in the heap.
for(int i = 0; i < currsize; i++)
{
free(arr[i].lname);
free(arr[i].fname);
free(arr[i].show);
}
//makes sure all of the pointers point to a NULL value.
for(int j = 0; j < currsize; j++)
{
arr[j].fname = NULL;
arr[j].lname = NULL;
arr[j].show = NULL;
}
}