-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.cpp
More file actions
50 lines (44 loc) · 1.37 KB
/
exercises.cpp
File metadata and controls
50 lines (44 loc) · 1.37 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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
/* TODO: read the first two sections of l4.pdf, and do the exercises therein.
* Also look at ../whatstheoutput/{6,7}.cpp
* */
/* TODO: take the example from class to grow a vector and wrap it in
* a function, perhaps using the following prototype: */
void growArray(int*& A, size_t cursize, size_t newsize)
{
/* A initially points to a buffer of cursize integers; your job is
* to find A a "new home" of newsize integers. */
int* B = new int[newsize];
for (size_t i = 0; i < cursize; i++) {
B[i] = A[i];
}
A = B;
delete B;
}
/* TODO: write a main program that reads all of stdin into an array
* of integers by allocating some small number initially, and then
* growing the array as necessary to accomodate the entire input.
* Use the function you wrote above to accomplish this. */
int main()
{
size_t size = 3, i = 0;
int* A = new int[size];
int input;
while (cin >> input) {
if (i >= size - 1) {
growArray(A, size, size * 1);
size++;
}
A[i] = input;
}
for (size_t j = 0; j < size; j++) {
cout << A[j] << " ";
}
}
/* TODO: browse `man 3 malloc` if you want to learn about dynamic allocations
* in C. */
/* TODO: while your in that man page, also read about "realloc", which is a
* library function that basically does the same thing as growArray above. */