Docsity

Multidimensional Arrays-Introduction to Programming-Lab Mannual, Exercises of Computer Programming

Partial preview of the text

Download Multidimensional Arrays-Introduction to Programming-Lab Mannual and more Exercises Computer Programming in PDF only on Docsity!

Multidimensional Arrays

๎€Š weโ€™ve looked at arrays of one dimension: A single variable specifies each array element. ๎€Š Arrays can have higher dimensions.

#include using namespace std; const int DISTRICTS = 4; const int MONTHS = 3; int main() { int d, m; double sales[DISTRICTS][MONTHS]; for(d=0; d<DISTRICTS; d++) for(m=0; m<MONTHS; m++) { cout << โ€œEnter sales for district โ€ << d+1; cout << โ€œ, month โ€ << m+1 << โ€œ: โ€; cin >> sales[d][m]; }

Multi Dimensional Arrays

๎€Š The array is defined with two size specifiers, each enclosed in brackets: ๎€Š double sales[DISTRICTS][MONTHS]; ๎€Š You can think about sales as a two-dimensional array, laid out like a checkerboard.

Accessing Multidimensional Array Elements ๎€Š Array elements in two-dimensional arrays require two indexes: ๎€Š sales[d][m] ๎€Š Notice that each index has its own set of brackets. Commas are not used. Donโ€™t write sales[d,m] ; this works in some languages, but not in C++.

Initializing Multidimensional Arrays ๎€Š As you might expect, you can initialize multidimensional arrays. ๎€Š The only prerequisite is a willingness to type a lot of braces and commas.

LAB TASK

๎€Š Write a code to generate a 5 x 5 matrice. Program should ask for every value and then display it in a matrice form.

LAB TASK

๎€Š Write a program to add two matrices of 5 x 5 order. Make your logic using loops