Array Storage Structure

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Storage Structure for 1-D Array

A[I]=BA+(I-L)*Size
Where,
I is Index to be calculated
BA is Base Address of Array
L is Lower Bound of Array
Size is size of one element of array
1 Calculate the address of A[6]. If given array is A[9] with
base address 1050 & array is Integer type array (int
takes 2 bytes / element). Also find total element in
array.
2 Calculate the address of A[8]. If given array is A[9] with
base address 1050 & array is float type array .
1082
3 Calculate the address of A[22]. If given array is A[55]
with base address 2020 & array is float type array.
Also find total element in array.
2108,55
4 Calculate the address of A[40]. If given array is A[100]
with base address 180 & array is int type array. (int
takes 2 bytes / element).
260
5 Calculate the address of A[75]. If given array is A[176]
with base address 201 & array is char type array.
276
6 Find base address of float type array whose 23rd
element at 2108.
2016
1
7 Calculate the address of A[15]. If given array is A[-22…
22] with base address 2002& array is char type array.
2039
8 Calculate the address of A[-3]. If given array is A[-9…7]
with base address 1002& array is float type array.
1026
9 Calculate the address of A[-20]. If given array is A[-57…
42] with base address 5000& array is int type array (int
takes 2 bytes / element).
5074
1 Calculate the address of A[0]. If given array is A[-29…29]
0 with base address 1000& array is float type array.
116

Given array is A[-4…3][2…13] is a integer type array with


base address is 150. Find the address of 36th element of
array(column major and int takes 2 bytes)

220
If A[6][3] at 234 in given float array A[9][10]. Array stored
in column major then find base address and last address of
an array.

2
90,526

102
If 45th element at 1234 in given char array A[9][9]. Array
stored in row major then find base address and last address
of an array.

Practical
1// Array size and element declared dynamically (from user) & display it
with address.

#include<stdio.h>
void main()
{
int arraysize,i,n;
printf("Enter the size of an array "); //Ask array size from customer
scanf("%d",&arraysize);

int array[arraysize]; // Array Declare

for(i=0;i<arraysize;i++) //Ask array Element to User and Assin it


{
printf("enter %d element of an array ",i+1);
scanf("%d",&array[i]);
}

for(i=0;i<arraysize;i++) // Print array element


{
printf("Array at index %d = %d at %d \n",i,array[i],&array[i]);
}
3
}

2// Array size and element declared dynamically (from user) in 2D Array

#include<stdio.h>
void main()
{
int row,column,i,j;
printf("Enter the size of Row an array "); //Ask array row from customer
scanf("%d",&row);
printf("Enter the size of Column an array "); //Ask array Column from
customer
scanf("%d",&column);

int array[row][column];

for(i=0;i<row;i++) // //Ask 2D array Element to User and Assin it


{
for(j=0;j<column;j++)
{
printf("enter a[%d][%d] element of an array ",i,j);
scanf("%d",&array[i][j]);
}
}

for(i=0;i<row;i++) // Print 2D array element


{
for(j=0;j<column;j++)
{
printf("a[%d][%d] element of an array is %d at %d \n" ,i,j,
array[i][j], &array[i][j]);
}
}
}

3. // 1-D Array BASE ADDRESS calculated by using its formula

#include<stdio.h>
void main()

4
{
int i,n,lower,upper, baseaddress,index,datatypesize,ans,casevalue;
printf("Enter the lOWER BOUND of an array "); //Ask LOWER BOUND
from customer
scanf("%d",&lower);

printf("Enter the Upper BOUND of an array "); //Ask UPPER BOUND from
customer
scanf("%d",&upper);

printf("Enter the base address of an array "); //Ask BASE ADDRESS from
customer
scanf("%d",&baseaddress);

printf("Enter the index to calculate base address of an array "); //Ask


CALCULATED INDEX from customer
scanf("%d",&index);
if(index<upper)
{
printf("Enter the datatype SIZE of an array "); //Ask DATATYPE SIZE
from customer
scanf("%d",&datatypesize);

int arraysize=upper-lower;
int array[arraysize]; // Array Declare

ans=baseaddress+(index-lower)*datatypesize; // Base Address


calculation formula
printf("base address of an index %d is %d ",index,ans);
}
else
{
printf("Enter valid index number");
}
}

4. // 2-D int Array BASE ADDRESS calculated by formula its formula

#include<stdio.h>
void main()
{
int a[3][3],j,base,w=2,lr=0,lc=0,i,address,n=3;

5
/* base = Base address. , W = Storage Size of one element stored in the array
(in byte).
I,J = Subscript of an element whose address is to be found.
LR = Lower limit of row , LC= Lower linmit of COLUMN , n= no.of column
*/
printf("enter elements for int array [3][3] ");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("\nelements for array are:-\n");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ printf("%4d",a[i][j]);
} printf("\n");
}
printf("enter base address ");
scanf("%d",&base);
printf("enter index of element whose addres want to find ");
scanf("%d%d",&i,&j);
printf("Using ROW-MAJOR \n");
address=base+w*((i-lr)* n +(j-lc));
printf("address of element=%d, a[%d][%d] =%d ",a[i][j],i,j,address);

printf("\nUsing COLUMN-MAJOR \n ");


address=base+w*((j-lc)* n +(i-lr));
printf("address of element=%d, a[%d][%d] =%d ",a[i][j],i,j,address); }

Example: Given an array, arr[1………10][1………15] with base value


100 and the size of each element is 1 Byte in memory. Find the
address of arr[8][6] with the help of row-major order.
Solution: Given:
Formula: Address of A[I][J] = B + ((I – Lr) * N + (J – Lc))*size
Solution: Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210

6
Example: Given an array, arr[1………10][1………15] with base value
100 and the size of each element is 1 Byte in memory. Find the
address of arr[8][6] with the help of Column-major order.
Number of Rows given in the matrix M
= Upper Bound – Lower Bound + 1
= 10 – 1 + 1 = 10
Formula: A[I][J] = B + ((I – Lr)+ (J – Lc) * M))*size
Address of A[8][6] = 100 + 1 * ((8 – 1)+(6 – 1) * 10)
= 100 + 1 * ((7)+ (5) * 10 )
= 100 + 1 * (57)
Address of A[I][J] = 157

You might also like