DS_Array_1-2-49
DS_Array_1-2-49
DS_Array_1-2-49
Any element in the array can be directly accessed by using the index.
Memory Allocation of the Array
All the data elements of an array are stored at contiguous locations in the main
memory
Name of the array represents the base address or the address of first element in the
main memory
Each element of the array is represented by a proper indexing
Accessing Elements of an Array
To access any random element of an array it needs the following information:
Base Address of the array.
Size of an element in bytes.
Example:
void modify(int arr[], int n) {
Input: arr[] = {2, 3, 4, 5, 6}
if (n <= 1) return;
int prev = arr[0]; Output: arr[] = {6, 8, 15, 24, 30}
arr[0] = arr[0] * arr[1]; arr[] = {2*3, 2*4, 3*5, 4*6, 5*6}
for (int i=1; i<n-1; i++) {
int curr = arr[i];
arr[i] = prev * arr[i+1];
prev = curr;
}
arr[n-1] = prev * arr[n-1];
}
Program Input: arr[] = {10, 4, 3, 50, 23, 90}
Output: 90, 50, 23
Find the three largest elements in an array
Column-major
puts the first column in contiguous memory, then the second, etc.
In column-major layout, row indices change faster.
Calculate Memory Addresses in
2D-Arrays
The 2-dimensional arrays are stored as 1-dimensional arrays in the computer’s
memory.
There are two ways to achieve this:
Row-major Implementation
Column-major Implementation
Calculate Memory Addresses in
2D-Arrays
The 2-dimensional arrays are stored as 1-dimensional arrays in the computer’s
memory.
There are two ways to achieve this:
Row-major Implementation
Column-major Implementation
Calculate Memory Addresses in
2D-Arrays
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
Note that:
B is the base address (address of the first block in the array).
W is the width in bytes (size in bytes for each element in the array).
Lr is the index of the first row.
Lc is the index of the first column.
R is the total number of rows.
C is the total number of columns.
Problem
Each element of an array arr[15][20] requires ‘W’ bytes of storage. If the
address of arr[6][8] is 4440 and the base address at arr[1][1] is 4000, find the
width ‘W’ of each cell in the array arr[][] when the array is stored as column
major wise.
Number of columns, C = 8 – 3 + 1 = 6.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
Address of ARR[3][6] = 1430 + 4[6(3 – (-4)) + (6 – 3)]
Address of ARR[3][6] = 1430 + 4[6(3 + 4) + 3]
Address of ARR[3][6] = 1430 + 4[6(7) + 3]
Address of ARR[3][6] = 1430 + 4[42 + 3]
Address of ARR[3][6] = 1430 + 4[45]
Address of ARR[3][6] = 1430 + 180
Address of ARR[3][6] = 1610
Problem
A matrix A[m][m] is stored in the memory with each element requiring 4
bytes of storage. If the base address at A[1][1] is 1500 and the address of
A[4][5] is 1608, determine the order of the matrix when it is stored in Column
Major Wise.
1-D array
Both the expressions (arr + i) and *(arr + i) are pointers, but their base type are
different
base type of (arr + i) is ‘an array of 4 elements’
while the base type of *(arr + i) or arr[i] is int
Pointers and 2-D Arrays
To access an element of 2-D array, access any jth
element of ith 1-D array
base type of *(arr + i) is int and it contains the
address of 0th element of ith 1-D array
get the addresses of subsequent elements in the ith
1-D array by adding integer values to *(arr + i)
Example:
*(arr + i) + 1 will represent the address of 1st element
of ith 1-D array and
*(arr+i)+2 will represent the address of 2nd element
of ith 1-D array
*(arr + i) + j will represent the address of jth element
of ith 1-D array
On dereferencing this expression, can get the jth
element of the ith 1-D array
Pointers and 2-D Arrays
Print the values and address of elements of a 2-D array Output:
#include<stdio.h> Address of 0th array =
int main() { 0x7ffe50edd580 0x7ffe50edd580
int arr[3][4] = {{ 10, 11, 12, 13}, {20, 21, 22, 23}, 10 10 11 11 12 12 13 13
{30, 31, 32, 33} Address of 1th array =
}; 0x7ffe50edd590 0x7ffe50edd590
int i, j; 20 20 21 21 22 22 23 23
for (i = 0; i < 3; i++) { Address of 2th array =
printf("Address of %dth array = %p %p\n", 0x7ffe50edd5a0 0x7ffe50edd5a0
i, arr[i], *(arr + i)); 30 30 31 31 32 32 33 33
for (j = 0; j < 4; j++)
printf("%d %d ", arr[i][j], *(*(arr + i) + j));
printf("\n");
}
return 0;
}
Array of pointers
“Array of pointers” is an array of the pointer variables
Also known as pointer arrays.
Syntax:
int *var_name[array_size];
Declaration:
int *ptr[3];
Array of pointers
#include <stdio.h>
Output:
const int SIZE = 3;
Value of arr[0] = 10
int main() {
Value of arr[1] = 20
int arr[] = { 10, 20, 30 };
Value of arr[2] = 30
int i, *ptr[SIZE];
for (i = 0; i < SIZE; i++)
{
ptr[i] = &arr[i];
}
for (i = 0; i < SIZE; i++) {
printf("Value of arr[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
Array of pointers
#include <stdio.h>
Output:
const int size = 4;
Amit
int main() {
Amar
char* names[] = {
"Amit", Ankit
"Amar", Ashish
"Ankit",
"Ashish"
};
int i = 0;
for (i = 0; i < size; i++) {
printf("%s\n", names[i]);
}
return 0;
}
Pointer to Structure
To access members of a structure using int main() {
pointers struct person *pPtr, p1;
use the -> operator. pPtr = &p1;
printf("Enter age: ");
#include <stdio.h> scanf("%d", &pPtr->age);
struct person { printf("Enter weight: ");
int age; scanf("%f", &pPtr->wt);
float wt; printf("Displaying...\n");
}; printf("Age: %d\n", pPtr->age);
printf("Weight: %f", pPtr->wt);
return 0;
}