Two-Dimensional Array
Two-Dimensional Array
2 Dimensional Array
Output
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1
1.#include <iostream>
2.using namespace std;
3.
4.const int CITY = 2;
5.const int WEEK = 7;
6.
7.int main()
8.{
9. int temperature[CITY][WEEK];
10.
11. cout << "Enter all temperature for a week of
first city and then second city. \n";
12.
13. // Inserting the values into the temperature
array
14. for (int i = 0; i < CITY; ++i)
15. {
16. for(int j = 0; j < WEEK; ++j)
17. {
18. cout << "City " << i + 1 << ", Day "
<< j + 1 << " : ";
19. cin >> temperature[i][j];
20. }
21. }
22.
23. cout << "\n\nDisplaying Values:\n";
24.
25. // Accessing the values from the temperature
array
26. for (int i = 0; i < CITY; ++i)
27. {
28. for(int j = 0; j < WEEK; ++j)
29. {
30. cout << "City " << i + 1 << ", Day "
<< j + 1 << " = " << temperature[i][j] << endl;
31. }
32. }
33.
34. return 0;
35. }
Output
Enter all temperature for a week of first city
and then second city.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23
Displaying Values:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23