-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
60 lines (47 loc) · 1.38 KB
/
main.cpp
File metadata and controls
60 lines (47 loc) · 1.38 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: homayoun
*
* Created on September 8, 2017, 9:20 AM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
void getArray(int myArray[], int size); //Arrays are always passed by reference
void printArray(int myArray[], int size); //Arrays in functions always need a size
/*
*
*/
int main(int argc, char** argv) {
int Array1[5]; //An uninitialized 5 element array
int Array2[5] = {1, 2, 3, 4, 5}; //An initialized 5 element array
int Array3[] = {1, 2, 3, 4, 5, 6}; //An initialized array with implicit size of 5
int temp;
cout << "Enter array size: ";
cin >> temp;
const int size(temp);
int Array4[size];
getArray(Array4, size);
printArray(Array4, size);
//C++ does not check for array-out-of-bound violations
getArray(Array4, 10);
printArray(Array4, 15);
return 0;
}
void getArray(int myArray[], int size) {
cout << "Please enter the " << size << " elements of the array: " << endl;
for(int i=0; i < size; i++) {
cin >> myArray[i];
}
}
void printArray(int myArray[], int size) {
for(int i=0; i < size; i++) {
cout << myArray[i] << " ";
}
cout << endl;
}