Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lab#2 Multidimensional Array

Objective:

Purpose of this lab is to learn about multi-dimensional arrays in C++. More specifically, how to
declare them, access them and use them efficiently in your program

Equipment/Software:

MS Visual C++

Theory:

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.

You can think this array as table with 3 rows and each row has 4 columns as shown below.

Three dimensional array also works in a similar way. For example:

float x[2][4][3];

This array x can hold a maximum of 24 elements. You can think this example as: Each of the 2
elements can hold 4 elements, which makes 8 elements and each of those 8 elements can hold 3
elements. Hence, total number of elements this array can hold is 24.

1
Procedure:

Below are the steps mentioned for multidimensional array initialization in C++

You can initialise a multidimensional array in more than one way.

 Initialisation of two dimensional array


int test[2][3] = {2, 4, -5, 9, 0, 9};

Better way to initialise this array with same array elements as above.

int test[2][3] = { {2, 4, 5}, {9, 0 0}};

 Initialisation of three dimensional array


int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,
2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

Better way to initialise this array with same elements as above.

int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};

Tasks:

Task 1: Two Dimensional Array

C++ Program to display all elements of an initialised two dimensional array.

1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int test[3][2] =
7. {
8. {2, -5},
9. {4, 0},
10. {9, 1}
11. };
12.
13. // Accessing two dimensional array using
14. // nested for loops
15. for(int i = 0; i < 3; ++i)
16. {
17. for(int j = 0; j < 2; ++j)
18. {

2
19. cout<< "test[" << i << "][" << j << "] = " << test[i][j]
<< endl;
20. }
21. }
22.
23. return 0;
24. }

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

Task 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. }

3
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

Task 3: Three Dimensional Array

C++ Program to Store value entered by user in three dimensional array and display it.

1. }#include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. // This array can store upto 12 elements (2x3x2)
7. int test[2][3][2];
8.
9. cout << "Enter 12 values: \n";

4
10.
11. // Inserting the values into the test array
12. // using 3 nested for loops.
13. for(int i = 0; i < 2; ++i)
14. {
15. for (int j = 0; j < 3; ++j)
16. {
17. for(int k = 0; k < 2; ++k )
18. {
19. cin >> test[i][j][k];
20. }
21. }
22. }
23.
24. cout<<"\nDisplaying Value stored:"<<endl;
25.
26. // Displaying the values with proper index.
27. for(int i = 0; i < 2; ++i)
28. {
29. for (int j = 0; j < 3; ++j)
30. {
31. for(int k = 0; k < 2; ++k)
32. {
33. cout << "test[" << i << "][" << j << "][" << k << "] =
" << test[i][j][k] << endl;
34. }
35. }
36. }
37.
38. return 0;
39.

Output

Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12

Displaying Value stored:


test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7

5
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

As the number of dimension increases, the complexity also increases tremendously although the
concept is quite similar.

Task 4: Add Two Matrices using Multi-dimensional Arrays

1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int r, c, a[100][100], b[100][100], sum[100][100], i, j;
7.
8. cout << "Enter number of rows (between 1 and 100): ";
9. cin >> r;
10.
11. cout << "Enter number of columns (between 1 and 100): ";
12. cin >> c;
13.
14. cout << endl << "Enter elements of 1st matrix: " << endl;
15.
16. // Storing elements of first matrix entered by user.
17. for(i = 0; i < r; ++i)
18. for(j = 0; j < c; ++j)
19. {
20. cout << "Enter element a" << i + 1 << j + 1 << " : ";
21. cin >> a[i][j];
22. }
23.
24. // Storing elements of second matrix entered by user.
25. cout << endl << "Enter elements of 2nd matrix: " << endl;
26. for(i = 0; i < r; ++i)
27. for(j = 0; j < c; ++j)
28. {
29. cout << "Enter element b" << i + 1 << j + 1 << " : ";
30. cin >> b[i][j];
31. }
32.
33. // Adding Two matrices
34. for(i = 0; i < r; ++i)
35. for(j = 0; j < c; ++j)
36. sum[i][j] = a[i][j] + b[i][j];
37.
38. // Displaying the resultant sum matrix.
39. cout << endl << "Sum of two matrix is: " << endl;
40. for(i = 0; i < r; ++i)
41. for(j = 0; j < c; ++j)
42. {
43. cout << sum[i][j] << " ";
44. if(j == c - 1)

6
45. cout << endl;
46. }
47.
48. return 0;
49. }

Output

Enter number of rows (between 1 and 100): 2


Enter number of columns (between 1 and 100): 2

Enter elements of 1st matrix:


Enter element a11: -4
Enter element a12: 5
Enter element a21: 6
Enter element a22: 8

Enter elements of 2nd matrix:


Enter element b11: 3
Enter element b12: -9
Enter element b21: 7
Enter element b22: 2

Sum of two matrix is:


-1 -4
13 10

Conclusion:

______________________________________________________________________________
______________________________________________________________________________
_____________________________________________________________________________

You might also like