0% found this document useful (0 votes)
72 views

Two-Dimensional Array

In C++, a two dimensional array can be used to store data in a table-like format with multiple rows and columns. A two dimensional array is an array of arrays that allows the storage of multiple values in a grid structure. Elements in a two dimensional array can be accessed and initialized using nested for loops to loop through each row and column. Example code is provided to initialize and display the elements of a two dimensional array to store and output the temperature readings from two cities over a week.

Uploaded by

JLD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Two-Dimensional Array

In C++, a two dimensional array can be used to store data in a table-like format with multiple rows and columns. A two dimensional array is an array of arrays that allows the storage of multiple values in a grid structure. Elements in a two dimensional array can be accessed and initialized using nested for loops to loop through each row and column. Example code is provided to initialize and display the elements of a two dimensional array to store and output the temperature readings from two cities over a week.

Uploaded by

JLD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CC 103 – INTERMEDIATE PROGRAMMING

2 Dimensional Array

In C++, you can create an array of an array


known as multi-dimensional array. For
example:
int x[3][4];
Here, x is a two dimensional array. It can hold a
maximum of 12 elements.
Initialization of two dimensional array
int test[2][3] = {2, 4, -5, 9, 0, 9};
Better way to initialize this array with same
array elements as above.
int test[2][3] = { {2, 4, -5}, {9, 0, 9}};
You can think this array as table with 3 rows and each row has 4
columns as shown below.

Elements in two dimensional array in C++ Programming


Initialization of two dimensional array
int test[2][3] = {2, 4, -5, 9, 0, 9};
Better way to initialize this array with same array elements as
above.
int test[2][3] = { {2, 4, -5}, {9, 0, 9}};
this array has total 2*3 = 6 elements.

Example 1: Two Dimensional Array


C++ Program to display all elements of an initialized two
dimensional array.
#include <iostream>
using namespace std;
int main()
{
int test[3][2] =
{
{2, -5},
{4, 0},
{9, 1}
};
// Accessing two dimensional array using
// nested for loops
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cout<< "test[" << i << "][" << j << "] = " << test[i][j] <<
endl;
}
}
return 0;
}

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

Example 2: Two Dimensional Array


C++ Program to store temperature of two different cities for a
week and display it.

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

You might also like