Arrays
Arrays
Arrays
r
ARRAYS
• Why does Array Indexing start with 0?
• Let's try to understand this by taking one example. Assume, we can declare an array of size
10 as int a[10];
• Here a itself is a pointer which contains the memory location of the first element of the
array. Now for accessing the first element, we will write a[0] which is internally decoded by
the compiler as *(a + 0).
• In the same way, the second element can be accessed by a[1] or *(a + 1). As a contains the
address of the first element so for accessing the second element we have to add 1 to it.
That's why here we have written *(a +1). In an array, the index describes the offset from the
first element, i.e. the distance from the first element.
• Now let's assume that array indexing starts at 1 instead of 0. In this case for accessing the
first element, we have to write a[1] which is internally decoded as *(a + 1 – 1).
• Observe here that we have to perform one extra operation i.e. subtraction by 1. This extra
operation will greatly decrease the performance when the program is big. That's why to
avoid this extra operation and improve the performance, array indexing starts at 0 and not at
1.
•
ARRAYS
• To Read & Print Elements Of Array
• An array is a collection or a sequential order to various elements, with or without a particular order. Arrays
usually consist of information that is stored at respective pointers in C programming
• Read the size of an array and store that value into the variable n.
• scanf() function reads the entered element and initialize that element to a[i] until all iterations of for loop
as scanf(“%d”,&a[i]) using for loop for(i=0;i<n;i++).
• printf(“%d”,a[i]) it prints the elements of an array from i=0 to i<n using for loop. Print the array elements
of a[] using for loop which iterates i=0 to i<size of an array.
#include <stdio.h>
int main()
• As we can see, in this array, the size of
{ the array is defined at first.
int a[1000],i,n; • The size of this array is 5.
printf("Enter size of array: ");
scanf("%d",&n); • After that, the elements are entered
printf("Enter %d elements in the array : ", n); accordingly for the array.
• The elements entered in this array are
for(i=0;i<n;i++)
{ as follows:
scanf("%d", &a[i]); • 1, 2, 3, 4 and 5.
} •
printf("\nElements in array are: ");
Hence, after it is given a command to
print the elements, all the five elements
for(i=0;i<n;i++) will be printed as per the order they
{
printf("%d ", a[i]); were entered into the array.
}
return 0;
}
ARRAYS
• Array Memory Allocation in C
• An array is a collection of homogeneous (same type) data items stored in contiguous memory locations. For example if an array is of type
“int”, it can only store integer elements and cannot allow the elements of other types such as double, float, char etc.
• The following diagram represents an integer array that has 12 elements. The index of the array starts with 0, so the array having 12
elements has indexes from 0 to 11.
ARRAYS
• Array Memory Allocation in C
• It has been already discussed that whenever an array is declared in the program, contiguous memory to it elements are allocated.
• Initial address of the array – address of the first element of the array is called base address of the array.
• Each element will occupy the memory space required to accommodate the values for its type, i.e.; depending on elements datatype, 1, 4 or 8 bytes of memory is
allocated for each elements.
• Next successive memory address is allocated to the next element in the array. This process of allocating memory goes on till the number of element in the array gets
over.
• If the array is a character array, then its elements will occupy 1 byte of memory each.
• If it is a float array then its elements will occupy 8 bytes of memory each.
• But this is not the total size or memory allocated for the array.
• They are the sizes of individual elements in the array. If we need to know the total size of the array, then we need to multiply the number of elements with the size of
individual element.
• i.e.; Total memory allocated to an Array = Number of elements * size of one element
• Total memory allocated to an Integer Array of N elements
= Number of elements * size of one element
= N * 4 bytes
= 10 * 4 bytes = 40 Bytes, where N = 10
= 500 * 4 bytes = 2000 Bytes, where N = 500
• Total memory allocated to an character Array of N elements
= Number of elements * size of one element
= N * 1 Byte
= 10 * 1 Byte = 10 Bytes, where N = 10
= 500 * 1 Byte = 500 Bytes, where N=500
ARRAY OPERATIONS
• There are a number of operations that can be performed on an array which are:
• Traversal, Copying, Reversing, Sorting
• Insertion, Deletion, Searching, Merging
• Traversal:
• Traversal means accessing each array element for a specific purpose, either to perform an operation on them , counting the total number of
elements or else using those values to calculate some other result.
• Since array elements is a linear data structure meaning that all elements are placed in consecutive blocks of memory it is easy to traverse
them.
• In the first step we initialise the index for our array elements and it will
• Algorithm: Consider A[]is the array: be incremented until last index ( step 4) to traverse all array elements.
• Step 1: Initialise counter c = lower_bound_index
• Step 2: Repeat steps 3 to 4 while c < upper_bound
• Step 2 specifies the condition that traversal will continue till upper
• Step 3: Apply the specified operation on A[c] bound of array is reached.
• Step 4: Increment counter : C = C + 1 [Loop Ends] • Step 3 we perform the operation using each array element in every
• Step 5: Exit iteration.
#include<stdio.h> #include<conio.h>
int main()
{ clrscr(); Output:
int i, marks[5], n, sum = 0;
float avg; Enter the no.of subjects: 5
printf("Enter the no.of subjects:\n"); scanf("%d",&n); Enter the marks obtained in your 5
printf("Enter the marks obtained in your %d subjects\n", n);
for(i=0;i<n;i++) subjects 10 14 15 18 16
{ scanf("%d", &marks[i]);
}
for(i=0;i<n;i++)
Average of marks is : 14.00
{ sum = sum + marks[i];
}
avg = (sum / n);
printf("Average of marks is : %.2f \n", avg); return 0; }
ARRAY OPERATIONS
• Copying elements of an array
• Copying array elements to another array will yield an array of the same length and elements as the original one.
• In order to so, we need to know the length of original array in advance.
• The destination array should also be of the same or greater size as that of original array in order to hold the array contents.
• The copying of elements would be done on index by index basis.
• ALGORITHM:(We are using integer array)
• START
• Step 1 : Take two arrays A, B
• Step 2 : Store values in A
• Step 3 : Loop for each value of A
• Step 4 : Copy each index value to B array at the same index location #include<stdio.h>
• int main()
STOP {
int arr1[20], arr2[20], i, num;
printf("\nEnter the no of elements in the array :");
scanf("%d", &num);
//Accepting values into Array
printf("\nEnter the array elements :");
for (i = 0; i < num; i++) {
scanf("%d", &arr1[i]);
}
// Copying data from source array A to destination array 'b
for (i = 0; i < num; i++) {
Output: arr2[i] = arr1[i];
}
Enter the no of elements in the array :6 //Printing of all elements of array
Enter the array elements :7 8 9 10 11 12 printf("The copied array is as follows:");
The copied array is as follows: arr2[0] = 7 arr2[1] = 8 arr2[2] = 9 for (i = 0; i < num; i++)
printf("\narr2[%d] = %d", i, arr2[i]);
arr2[3] = 10 arr2[4] = 11 arr2[5] = 12 return (0);
}
ARRAY OPERATIONS
• Reversing elements of an array
• Reversing an array means that the sequence of elements of array will be reversed.
• Reversing an array means that the sequence of elements of array will be reversed.
• For instance if your array ‘A’ has two elements : A[0] = 1; A[1] = 2; then after reversal A[0] = 2 and A[1] = 1.
• There are two methods to perform reversal of array.