KEMBAR78
Array | PDF | Computer Engineering | Computer Programming
0% found this document useful (0 votes)
24 views6 pages

Array

An array is a collection of items of the same type stored in contiguous memory locations, with elements accessed via zero-based indexing. There are three main types of arrays: one-dimensional, two-dimensional, and three-dimensional, each allowing for random access of elements. The document also covers array declaration, memory allocation, and methods for calculating the address of elements in both one-dimensional and multi-dimensional arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Array

An array is a collection of items of the same type stored in contiguous memory locations, with elements accessed via zero-based indexing. There are three main types of arrays: one-dimensional, two-dimensional, and three-dimensional, each allowing for random access of elements. The document also covers array declaration, memory allocation, and methods for calculating the address of elements in both one-dimensional and multi-dimensional arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Arrays:

An array is a collection of items of the same variable type that are stored at
contiguous memory locations. It’s one of the most popular and simple data
structures and is often used to implement other data structures. Each item in an
array is indexed starting with 0.

Basic terminologies of the array:


 Array Index: In an array, elements are identified by their indexes. Array
index starts from 0.
 Array element: Elements are items stored in an array and can be accessed by
their index.
 Array Length: The length of an array is determined by the number of
elements it can contain.
Representation of Array:
The representation of an array can be defined by its declaration. A declaration
means allocating memory for an array of a given size.

Representation of Array

Arrays can be declared in various ways in different languages. For better


illustration, below are some language-specific array declarations.

int arr[5]; // This array will store integer type element

char arr[10]; // This array will store char type element

float arr[20]; // This array will store float type element

However, the above declaration is static or compile-time memory allocation,


which means that the array element’s memory is allocated when a program is
compiled. Here only a fixed size (i,e. the size that is mentioned in square
brackets []) of memory will be allocated for storage, but don’t you think it will
not be the same situation as we know the size of the array every time, there might
be a case where we don’t know the size of the array. If we declare a larger size
and store a lesser number of elements will result in a wastage of memory or either
be a case where we declare a lesser size then we won’t get enough memory to
store the rest of the elements. In such cases, static memory allocation is not
preferred.
Types of arrays:
There are mainly three types of arrays:
1. One Dimensional Array
2. Two Dimensional Array
3. Three-Dimensional Array
1. One Dimensional Array:
 It is a list of the variable of similar data types.
 It allows random access and all the elements can be accessed with the help of
their index.
 The size of the array is fixed.
 For a dynamically sized array, vector can be used in C++
Representation of 1D array:

Representation of the 1D array

2. Two Dimensional Array:


 It is a list of lists of the variable of the same data type.
 It also allows random access and all the elements can be accessed with the
help of their index.
 It can also be seen as a collection of 1D arrays. It is also known as the Matrix.
 Its dimension can be increased from 2 to 3 and 4 so on.
 They all are referred to as a multi-dimension array.
 The most common multidimensional array is a 2D array. Representation of 2 D

array:
Representation of 2D Array
3. Three-Dimensional Array:
A Three Dimensional Array or 3D array in C is a collection of two-dimensional
arrays. It can be visualized as multiple 2D arrays stacked on top of each other.

Representation of 3D array

Declaration of Three-Dimensional Array:


We can declare a 3D array with x 2D arrays each having y rows and z columns
using the syntax shown below.
Syntax:
data_type array_name[x][y][z];
 data_type: Type of data to be stored in each element.
 array_name: name of the array
 x: Number of 2D arrays.
 y: Number of rows in each 2D array.
 z: Number of columns in each 2D array.
Finding Adress of an Element in Array
When it comes to organizing and accessing elements in a multi-dimensional
array, two prevalent methods are Row Major Order and Column Major Order.
1. Row Major Order
Row major ordering assigns successive elements, moving across the rows and
then down the next row, to successive memory locations. In simple language, the
elements of an array are stored in a Row-Wise fashion.
To find the address of the element using row-major order uses the following
formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero),
N = Number of column given in the matrix.

2. Column Major Order


If elements of an array are stored in a column-major fashion means moving
across the column and then to the next column then it’s in column-major
order. To find the address of the element using column-major order use the
following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it as zero),
M = Number of rows given in the matrix.

Calculating the address of any element In the 1-D array:


A 1-dimensional array (or single-dimension array) is a type of linear array.
Accessing its elements involves a single subscript that can either represent a row
or column index.
Example:

1D array

To find the address of an element in an array the following formula is used:


Address of A[I] = B + W * (I – LB)
I = Subset of element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LB = Lower Limit/Lower Bound of subscript(If not specified assume zero).

Example: Given the base address of an array A[1300 …………


1900] as 1020 and the size of each element is 2 bytes in the memory, find the
address of A[1700].
Solution:
Given:
Base address B = 1020
Lower Limit/Lower Bound of subscript LB = 1300
Storage size of one element store in any array W = 2 Byte
Subset of element whose address to be found I = 1700

Formula used:
Address of A[I] = B + W * (I – LB)

Solution:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

Example 1: Array Input/Output


#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

Example 2: Calculate Average

// Program to find the average of n numbers using arrays


#include <stdio.h>
int main() {
int marks[10], i, n, sum = 0;
double average;

printf("Enter number of elements: ");


scanf("%d", &n);

for(i=0; i < n; ++i) {


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum += marks[i];
}

// explicitly convert sum to double


// then calculate average
average = (double) sum / n;

printf("Average = %.2lf", average);

return 0;
}
Run Code

Output

Enter number of elements: 5


Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39.60

You might also like