Professional Documents
Culture Documents
◼ Two-Dimensional Arrays
◼ Multi-Dimensional Arrays
One-Dimensional Array
◼ A one-dimensional array is one in which only one
subscript specification is needed to specify a
particular element of the array.
◼ One dimensional array can be declared as
follows:
Datatype Array_Name [size];
◼ Data type is the type of elements to be stored in
the array.
◼ Variable name specifies the name of array, it may
be given any name like other simple variable.
Arrays
Array Declaration:
data-type name [size];
{ printf(“%d”,arr[i]);
printf(“\t”);}
return 0;
}
8/29/2022 CSE 2103 Department of CSE 12
Initializing one-dimensional array
int number[3] ={0,0,0}; or {0} ;
→ declares the variable number as an array of size 3
and will assign 0 to each element.
For example
printf(“%d\n”,x[0]); Output:
9
printf(“%d\n”,x[1]);
11
printf(“%d\n”,x[2]); 13
printf(“%d\n”,x[i]);
8/29/2022 CSE 2103 Department of CSE 15
Program to read n elements into an array and
print it
int a[10], i, n;
printf("enter no of numbers“); Output:
scanf(“%d”,&n); enter no of numbers
3
printf(“enter n numbers \n”);
enter n numbers
for(i=0;i<n;i++) 9
scanf(“%d\n”,&a[i]); 11
13
Numbers entered are:
printf(“\nNumbers entered are:\n”); 9
for(i=0;i<n;i++) 11
printf(“%d\n”,a[i]); 13
8/29/2022 CSE 2103 Department of CSE 16
Program to add two array elements and store the
corresponding elements sum in another array
int a[10], n, i;
Example : a[ ]={1, 2, 3, 4, 5}
printf(“Enter values\n“); Enter values
n=5
for(i=0;i<n;i++) 12345
Reverse printing of array
scanf(“%d”,&a[i]); 5 4 3 2 1
Array before Array after
printf(“\nReverse order printing a[0]=1 a[0]=1
a[1]=2 a[1]=2
of array\n”); a[2]=3 a[2]=3
a[3]=4 a[3]=4
for(i=n-1;i>=0;i--) // reverse loop a[4]=5 a[4]=5
printf(“%d\n”,a[i]);
8/29/2022 CSE 2103 Department of CSE 18
Write a program to reverse an array using only
one array
{
Example : a[ ]={1, 2, 3, 4, 5}
int a[20], i, j, n, temp;
Enter values
printf("enter n \n“); n=5
12345
scanf(“%d”,&n); Reversed array
5 4 3 2 1
printf("\n Enter values for an array“); Array Reversed
array
for(i=0;i<n;i++) a[0]=1 a[0]=5
a[1]=2 a[1]=4
scanf(“%d”,&a[i]); a[2]=3 a[2]=3
a[3]=4 a[3]=2
Contd… a[4]=5 a[4]=1
8/29/2022 23
Address Calculation
8/29/2022 24
Address Calculation
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
Index 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
8/29/2022 25