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

This is lab manual in form of lecture slides for Introduction to Programming course. It was delivered by Prof. Abhimoda Arora at Alagappa University. It includes: Multidimensional, Arrays, Variable, Specifies, Dimensions, Element, Districts, Sales, Checkerboard, Indexes

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya ๐Ÿ‡ฎ๐Ÿ‡ณ

5

(4)

32 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Multidimensional Arrays
๎€Šweโ€™ve looked at arrays of one dimension: A
single variable specifies each array element.
๎€ŠArrays can have higher dimensions.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

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