03-DS-Array-2024
03-DS-Array-2024
n Any element in the array can be directly accessed by using the index.
Memory Allocation of the Array
n All the data elements of an array are stored at contiguous locations in the main
memory
n Name of the array represents the base address or the address of first element in the
main memory
n Each element of the array is represented by a proper indexing
Accessing Elements of an Array
n To access any random element of an array it needs the following information:
n Base Address of the array.
n 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 the 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
§ 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.
n Declaration:
n 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;
}