-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17-arrays.cpp
More file actions
33 lines (28 loc) · 954 Bytes
/
Copy path17-arrays.cpp
File metadata and controls
33 lines (28 loc) · 954 Bytes
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
//arrays are a collection of data items of the same type.
#include <iostream>
#include <string>
using namespace std;
void main() {
//arrays can be declared in multiple ways
//method 1:
string cars[4];
//method 2:
string cars[4] = {"volvo", "BMW", "Ford"};
//an array of integers:
int myNum[3] = {10, 20, 30};
//accessing the elements of an array:
cout << cars[0]; //this will return the string volvo.
//changing an array element:
cars[0] = "tata";
cout << cars[0]; // outputs tata instead of volvo.
//looping in an array:
for(int i = 0; i < 4; i++) {
cout << cars[i] << "/n"; // the << "/n" part just adds a new line/ break line after every array element.
}
//declaring array size can be ommited
string bike[] = {"yamaha", "honda"}; // the size is fixed to 2
// declaration can also done in this way:
string TV[2];
TV[0] = "samsung";
TV[1] = "panasonic";
}