-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.cpp
76 lines (62 loc) · 1.5 KB
/
arrays.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
//
// arrays.cpp
// arrays
//
// Created by Michelle Talley on 1/3/19.
// Copyright © 2019 Michelle Talley. All rights reserved.
//
#include <iostream>
using namespace std;
void printArray(int n, int arr[])
{
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
}
int main(int argc, const char * argv[]) {
const int CAPACITY = 10;
typedef int IntegerArray[CAPACITY];
// Static Array
IntegerArray a;
// Initialize static array
for (int i = 0; i < CAPACITY; i++) {
a[i] = i+1;
}
// Print array
for (int i = 0; i < CAPACITY; i++) {
cout << a[i] << ",";
}
cout << endl;
// Dynamic array
int * arrayPtr = new int[CAPACITY];
// Dynamic array with [] access, initializatoion
for (int i = 0; i < CAPACITY; i++) {
arrayPtr[i] = i;
}
// Print Dynamic array
for (int i = 0; i < CAPACITY; i++) {
cout << arrayPtr[i] << ",";
}
cout << endl;
// Dynamic array with ptr access, set all to zero
int * ptr = arrayPtr;
for (int i = 0; i < CAPACITY; i++) {
*ptr = 0;
ptr++;
}
// Print out with ptr access
ptr = arrayPtr;
for (int i = 0; i < CAPACITY; i++) {
cout << *ptr << ",";
ptr++;
}
cout << endl;
// Print out with [] access
for (int i = 0; i < CAPACITY; i++) {
cout << arrayPtr[i] << ",";
}
cout << endl;
printArray(CAPACITY, a);
delete [] arrayPtr;
return 0;
}