Arrays
Arrays
Arrays
Multidimensional Array
For example: two dimensional array
datatype arrayname [row][col];
int data [2][2];
5 Declaring an One Dimensional Array
number[0] = 20
number[1] = 14
number[5] 20 14 32 88 45
number[2] = 32
number[3] = 88
number[4] = 45
If the size of an array is n, to access the last element, the n-1 index is
used. In this example, data[4]
7 Initializing One Dimensional Arrays
You are given 10 integers such as, 10, 12, 8, 34, 3, 7, 22, 17, 23, 44.
Compute the sum of the given numbers using appropriate data
structure and write the program.
Output
11 Compute the sum of the given numbers (Contd.)
12 Finding Max-Min Values in an Array
Write a C program to print the maximum and minimum values in an
array.
Output
13 Finding Max-Min Values in an Array
14 Search Element in an Array
Write a C program to search element in an array.
Output
15 Search Element in an Array
16 Search Element in an Array
17 Multidimensional Array
A multi-dimensional array can be termed as an array of arrays that
stores homogeneous data in tabular form.
General form of declaring N-dimensional arrays is:
data_type array_name[size1][size2]....[sizeN];
Example: Two dimensional array: int two_d[10][20];
Three dimensional array: int three_d[10][20][30];
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000
elements.
18 Multidimensional Array (Contd.)
• Second Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
• Third Method:
int x[3][4];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
cin >> x[i][j];
}
}
21 Storing elements in a matrix and printing it.
Write a program to store elements in a matrix and print it.
Output:
Storing elements in a matrix and printing it.
22 (Contd.)
23 Pattern Printing Problem