-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.c++
135 lines (91 loc) · 3.14 KB
/
arrays.c++
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
#include <iostream>
#include <string>
using namespace std;
int main(){
//String arrays
string cars[3] = {"Volvo", "BMW", "Ford"};
string names[4] = { "John", "Paul", "George", "Ringo"};
int myNum[3] = {1, 2, 3};
cars[0] = "Bugatti";
cout << "The first car is: " << cars[0] << endl;
cout<< "The second number is: " << myNum[1] << endl;
//C++ Arrays and Loops
//Loop Through and Array
//You can loop through the array elements with the for loop
//The following example outputs all elements in the cars array:
string sport[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < 4; i++)
{
cout << sport[i] << endl;
}
//Omit Array Size
//You don't hae to specify the size of the array.
//But if you don't, it will only be as big as the elements that are inserted into it:
string cars2[] = {"Volvo", "BMW", "Ford"};
string cars3[5] = {"Volvo", "BMW", "Ford"};
//Omit Elements on Declaration
//It is also possilbe to declare an array without specifying the elements on declaration and add them later
string sports[5];
sports[0] = "Basketball";
sports[1] = "Football";
sports[2] = "Netball";
sports[3] = "F1Racing";
sports[4] = "Swimming";
for(int i=0; i < 5; i++){
cout << sports[i] << endl;
}
//C++ Array Size
//Get the size of an array
//To get the size of an array, you can use the sizeOf() operator:
int myFavNumber[5] = {1, 2, 3, 4, 5};
cout << sizeof(myFavNumber) / sizeof(int)<< endl;
//C++ Multi-Dimensional Arrays
// Multi-Dimensional Arrays
// A multi-dimensional array is an array of arrays
// To declare a multi-dimensional array, define the variable type, specify the
// name of the array followed by square brackets which specify how many
// elements the main array has, followed by another set of square brackets which
// indicates how many elements the sub-arrays has
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
cout << letters[1][2];
// We put "1" to indicate there is a ship.
bool ships[4][4] = {
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
};
// Keep track of how many hits the player has and how many turns they have played in these variables
int hits = 0;
int numberOfTurns = 0;
// Allow the player to keep going until they have hit all four ships
while (hits < 4) {
int row, column;
cout << "Selecting coordinates\n";
// Ask the player for a row
cout << "Choose a row number between 0 and 3: ";
cin >> row;
// Ask the player for a column
cout << "Choose a column number between 0 and 3: ";
cin >> column;
// Check if a ship exists in those coordinates
if (ships[row][column]) {
// If the player hit a ship, remove it by setting the value to zero.
ships[row][column] = 0;
// Increase the hit counter
hits++;
// Tell the player that they have hit a ship and how many ships are left
cout << "Hit! " << (4-hits) << " left.\n\n";
} else {
// Tell the player that they missed
cout << "Miss\n\n";
}
// Count how many turns the player has taken
numberOfTurns++;
}
cout << "Victory!\n";
cout << "You won in " << numberOfTurns << " turns";
}