0% found this document useful (0 votes)
8 views5 pages

Multidimensional Arrays in C++

This lesson covers the concept of multidimensional arrays in C++, focusing on two-dimensional arrays. It provides syntax examples, graphical representations, and methods for initializing arrays, including explicit and implicit sizing. Additionally, it includes a sample program to calculate the sum of rows in a two-dimensional array.

Uploaded by

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

Multidimensional Arrays in C++

This lesson covers the concept of multidimensional arrays in C++, focusing on two-dimensional arrays. It provides syntax examples, graphical representations, and methods for initializing arrays, including explicit and implicit sizing. Additionally, it includes a sample program to calculate the sum of rows in a two-dimensional array.

Uploaded by

nutwjdf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)

MULTIDIMENSIONAL ARRAY
▪ The multi-dimensional array is that array in which data is
arranged in the form of array of arrays. The multi-dimensional
array can have as many dimensions as it required.
▪ So, two dimensional and three dimensional arrays are
commonly used.
LET US HAVE A LOOK AT THE SYNTAX:
datatype array_name [a1][a2][a3]…[an];
TWO - DIMENSIONAL ARRAYS

Syntax:

datatype arrayname[row][col];
Example:

int score[2][3];

Graphical Representation and Analogy:


Column
0 1 2
0 10 80 30
Row
1 40 50 90

The two-dimensional array score[r][c] is an integer data type and it can hold only an integer data with a
maximum of 6 values ( [2] X [3] ). here is the individual value of array variable score[2][3].

Page 1 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)

score[0][0] = 10
score[0][1] = 80
score[0][2] = 30
score[1][0] = 40
score[1][1] = 50
score[1][2] = 90

The first index-number of two dimensional arrays specifies the maximum row(r), while the second index-number
specifies the maximum column(c). The value of each array is in between the intersection of row and column.

Example #1 Getting the Row Sum


#include<iostream.h>
int main()
{

int r,c,n[3][3],sum;

for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
cout << "num " << r << " of " << c << ": ";
cin >> n[r][c];
}
cout << "------------------------\n";
}

for(r=0;r<=2;r++)
{
sum=0;
cout << " sum of row# " << r << ": ";
for(c=0;c<=2;c++)
{
cout << n[r][c] << ",";
sum = sum + n[r][c];
}
cout << " = " << sum << endl;

}
return 0;

Output:

num 0 of 0: 1
num 0 of 1: 2
num 0 of 2: 3
Page 2 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)

------------------------
num 1 of 0: 4
num 1 of 1: 5
num 1 of 2: 6
------------------------
num 2 of 0: 7
num 2 of 1: 8
num 2 of 2: 9
------------------------
sum of row# 0: 1,2,3, = 6
sum of row# 1: 4,5,6, = 15
sum of row# 2: 7,8,9, = 24

Initializing Arrays

Initialization – means assigning a default value to a variable.

Example:

in Variables;

int sum = 0;

in Arrays;

int n[5];

n[0]=1;
n[1]=10;
n[2]=15;
n[3]=20;
n[4]=11;

int n[1][4];
n[0][0]= 20;
n[0][1]= 25;
n[0][2]= 30;
n[0][3]= 35;

Page 3 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)

or if same value;

int num[100],n;

for(n=0;n<=99;n++)
{
num[n] = 0;
}

2 Alternative Methods for Initializing an Array

1. Explicit Array Sizing – means defining the size of an array by specifying a numerical constant within the square
bracket that explicitly specifies the size of that array.

int num[3]={2,4,6};
float ave[5] = {90.3, 75.79, 74.5, 80.1, 80.8};
char s1[5]={'h','e','l','l','o'};
char burger[12]={‘c’,’h’, ‘e’, ‘e’, ‘s’};
char s2[12] = "cheeseburger";
string str1[5]={"red","blue","yellow","green", “black”};

2. Implicit Array Sizing – the size of the array is indicated implicitly by the number of elements on the right side
of the assignment operator, which means that the square brackets are empty.

int num[]={2,4,6};
char s1[]={'h','e','l','l','o'};
char s3[]="hello world";
string str1[]={"red","blue","yellow","green"};

Initializing Multi-Dimensional Arrays

int num[3][2]={
{1,2},
{10,20},
{100,200}
};

Page 4 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)

Prepared by:

Jordan L. Salenga, MIT


IT - Instructor

Page 5 of 5

Common questions

Powered by AI

Initialization assigns a starting value to a variable or array, ensuring that they do not contain unpredictable, garbage values. For single variables, initialization assigns a single value directly (e.g., int sum = 0;). For arrays, especially larger ones, initialization can define a state for multiple elements at once (e.g., int n[5] = {1,2,3,4,5};), supporting batch assignment and memory allocation according to specified or defaults values .

The primary benefit of implicit array sizing is its flexibility—allowing the array size to adjust automatically to the number of initializer elements. This provides ease of maintenance and reduces the risk of mismatches between the declared size and the number of initial values. For example, using implicit sizing, int num[]={2,4,6}; will automatically allocate a three-element array without explicitly declaring the size. This approach lessens the chance of errors when elements are added or removed, and simplifies dynamic content initialization .

Multidimensional arrays allow data to be organized in a way that naturally represents complex structures like matrices or tables, which reflects more connected datasets compared to dispersed one-dimensional arrays. This simplification is crucial when handling operations that benefit from row-column interactions or multi-axis calculations without the need for cumbersome tracking of indices across several one-dimensional arrays. For example, accessing a specific element in terms of a row and column improves readability and efficiency, rather than manually maintaining parallel structures .

The graphical representation of a two-dimensional array illustrates the arrangement of data in rows and columns, resembling a table. This visual aid helps in understanding how indices map to actual data and facilitates better conceptualization of operations such as iteration or data access patterns. By providing a clear view of data positions and relationships, it simplifies the logic needed for algorithms that traverse or modify array contents, reducing cognitive load during programming .

To calculate the sum of each row in a two-dimensional array using C++, you iterate over each row, initializing a sum variable to zero at the start of each row. For each element in the row, add its value to the sum variable. This is demonstrated in the code: int n[3][3], sum; for(int r=0; r<3; r++) { sum=0; for(int c=0; c<3; c++) { sum += n[r][c]; } } where 'sum' accumulates the values in row 'r' .

Initializing a two-dimensional array in C++ involves specifying the data type, followed by the array name and dimensions in square brackets. Elements are initialized with values within curly braces, separated by commas, and grouped further into sub-braces for each row. For example, int num[3][2] = {{1,2}, {10,20}, {100,200}} initializes a 3x2 array with specified integer values. Each subgroup in curly braces corresponds to a row, establishing a clear and structured initial state .

Explicit array sizing involves defining the size of an array by specifying a numerical constant within square brackets, which precisely determines the allocation size of that array (e.g., int num[3]={2,4,6};). On the other hand, implicit array sizing allows the array size to be determined by the number of elements assigned, leaving the square brackets empty (e.g., int num[]={2,4,6};). Both approaches initialize the array, but explicit sizing requires a predetermined size, while implicit allows flexibility based on initial values .

Multidimensional arrays are arrays in which data is organized in the form of array of arrays. Unlike one-dimensional arrays, which are a linear sequence of elements, multidimensional arrays can store data in a tabular form, such as two-dimensional arrays (with rows and columns), or even higher dimensions. For example, a two-dimensional array is accessed using two indices corresponding to row and column, as demonstrated by the integer array 'score[2][3]' which holds data across two rows and three columns .

Initializing multi-dimensional arrays involves setting up a series of nested values, representing elements organized in rows and columns, compared to a simple linear flow in single-dimensional arrays. In multi-dimensional arrays, each row is encapsulated within its own set of curly braces, forming a matrix-like structure. For instance, an array int num[3][2]={ {1,2}, {3,4}, {5,6} } uses nested sets of braces to group pairs into distinct rows, contrasting with a single-dimensional array initialized like int num[]={1,2,3,4,5,6}; where elements are in a single line .

In two-dimensional arrays, indices are critical for data access and manipulation. The first index typically denotes the row, and the second index denotes the column. For example, in an array 'score[2][3]', 'score[0][1]' accesses the element in the first row and second column. These indices are used to navigate through the tabular structure, enabling operations like iteration, updates, and retrieval of specific elements, which are fundamental for computing tasks such as row summation .

You might also like