Array
Array
1. What is an Array
An array is a collection of elements, all of the same data type, stored in contiguous
memory locations. Arrays are used to group related data together.
Compared to the basic data type (int, float & char) it is an aggregate or derived
data type.
Example:
All the elements of an array occupy a set of contiguous memory locations. This
means that the elements of an array are stored one after another in memory.
int arr[3] = {10, 20, 30}; // Initializes an array with three values
3. Indexing
1
Accessing and manipulating array elements requires knowledge of indexing:
Array indices in C start from 0 and go up to size-1.
You must ensure indices stay within bounds to avoid accessing unintended
memory locations, which can lead to undefined behavior.
Example:
int arr[5] = {10, 20, 30, 40, 50};
printf("%d\n", arr[2]); // Accesses the 3rd element (30)
2
4. Accessing Array Elements
Array elements are accessed using their index. Note that indexing starts at 0.
Example:
Updating an Element:
Arrays are often used with loops to perform operations on all elements.
Example:
6. Array Size
int arr[10];
int size = sizeof(arr) / sizeof(arr[0]); // Total size divided by size of one element
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr[0]));
printf("%d", size);
7. Types of Arrays
2D Arrays: Arrays with rows and columns, used for tabular data.
3
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
8. Common Mistakes
int arr[5];
arr[5] = 10; // Incorrect! Index 5 is out of bounds.
10. Limitations
4
Write a program that takes an array of integers as input, calculates the sum of all
elements, and prints the result.
Sample Code:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int sum = 0;
Sessional Problems
4. Search in an Array:
5
o Write a program to search for a specific element in an array using linear
search.
Input:
Enter the number of elements in the array: 5
Enter 5 elements of the array: 10 20 30 40 50
Enter the element to search: 30
5. Frequency of Elements:
o Count the occurrences of a given number in an array.
o Find the element that occurs the most times in an array.
Input:
Enter the number of elements in the array: 7
Enter 7 elements of the array: 1 2 3 2 4 2 3
Output:
The most frequent element is 2, which occurs 3 times.
#include <stdio.h>
void main()
{
int n, maxCount = 0, mostFrequent;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
6
}
}
if (count > maxCount) {
maxCount = count;
mostFrequent = arr[i];
}
}
// Output the result
printf("The most frequent element is %d, which occurs %d times.\n",
mostFrequent, maxCount);
}
Input:
Enter the number of elements in the array: 7
Enter 7 elements of the array: 1 3 5 3 7 9 3
Enter the element to search for: 3
Output:
The element 3 is found at indices: 1 3 6
Solution:
#include <stdio.h>
int main() {
int n, key, found = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
7
printf("The element %d is found at indices: ", key);
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("%d ", i);
found = 1;
}
}
// If the element is not found
if (!found) {
printf("None");
}
}
7. Array Manipulation:
o Delete an element from a specific position in an array.
Input:
Enter the number of elements in the array: 6
Enter 6 elements of the array: 10 20 30 40 50 60
Enter the position of the element to delete (1 to 6): 3
8
if (pos < 1 || pos > n) {
printf("Invalid position!\n");
return 1;
}
// Delete the element by shifting elements to the left
for (int i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
// Decrease the size of the array
n--;
// Print the updated array
printf("Updated array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
11. Reversals:
o Reverse the contents of a one-dimensional array.
o Reverse the rows of a 2D array.
9
13. Specialized Searching:
o Find the second largest and second smallest elements in an array.
o Check if an array is a palindrome (reads the same forward and backward).
int main() {
int arr[5] = {10, 20, 5, 7, 50};
int max = arr[0];
10
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Original array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int temp = arr[0];
11
printf("%d ", arr[i]);
}
return 0;
}
12