10 Array programs in C

Unleashing the Power of Arrays in C: 10 Essential Programs! 💪🔢

Arrays, the superheroes of C programming, bring versatility to your code. Let's explore 10 crucial programs to master array manipulation!

1. Array Declaration and Initialization 🌐

c
#include <stdio.h> int main() { // Declare and initialize an array int numbers[5] = {1, 2, 3, 4, 5}; // Access and print array elements for (int i = 0; i < 5; ++i) { printf("%d ", numbers[i]); } return 0; }

Understanding how to declare, initialize, and access array elements is the first step!

2. Sum of Array Elements ➕

c
#include <stdio.h> int main() { int numbers[] = {10, 20, 30, 40, 50}; int sum = 0; // Calculate the sum of array elements for (int i = 0; i < 5; ++i) { sum += numbers[i]; } printf("Sum: %d\n", sum); return 0
}

Learn to iterate through an array and perform operations like calculating the sum!

3. Finding the Largest Element 📈

c
#include <stdio.h> 
int main() {
int numbers[] = {12, 45, 78, 23, 56}; int max = numbers[0]; // Find the largest element in the array for (int i = 1; i < 5; ++i) { if (numbers[i] > max) { max = numbers[i]; } } printf("Largest Element: %d\n", max); return 0;
 }

Uncover the technique to identify the largest element within an array!

4. Reversing an Array 🔁

c
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; // Reverse the array for (int i = 4; i >= 0; --i) { printf("%d ", numbers[i]); } return 0; }

Master the art of reversing the order of elements in an array!

5. Searching for an Element 🔍

c
#include <stdio.h> int main() { int numbers[] = {10, 20, 30, 40, 50}; int target = 30; int found = 0; // Search for an element in the array for (int i = 0; i < 5; ++i) { if (numbers[i] == target) { found = 1; break; } } printf("Element %d %s\n", target, found ? "found" : "not found"); return 0; }

Unlock the secret to locating a specific element in an array!

6. Copying an Array 📝

c
#include <stdio.h> int main() { int source[] = {2, 4, 6, 8, 10}; int destination[5]; // Copy elements from source to destination for (int i = 0; i < 5; ++i) { destination[i] = source[i]; } // Print the copied array for (int i = 0; i < 5; ++i) { printf("%d ", destination[i]); } return 0; }

7. Merging Two Arrays 🔄

c
#include <stdio.h> int main() { int arr1[] = {1, 2, 3}; int arr2[] = {4, 5, 6, 7}; int merged[7]; // Merge two arrays int i, j; for (i = 0; i < 3; ++i) { merged[i] = arr1[i]; } for (j = 0; j < 4; ++j) { merged[i + j] = arr2[j]; } // Print the merged array for (int k = 0; k < 7; ++k) { printf("%d ", merged[k]); } return 0; }

Discover the technique to combine the elements of two arrays into a new one!

8. Removing Duplicates from an Array 🚫

c
#include <stdio.h> int main() { int numbers[] = {1, 2, 2, 3, 4, 4, 5}; int n = 7; // Remove duplicates from the array for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ) { if (numbers[j] == numbers[i]) { for (int k = j; k < n; ++k) { numbers[k] = numbers[k + 1]; } --n; } else { ++j; } } } // Print the array without duplicates for (int i = 0; i < n; ++i) { printf("%d ", numbers[i]); } return 0; }

Learn the process of eliminating duplicate elements from an array!

9. Sorting an Array 📊

c
#include <stdio.h> int main() { int numbers[] = {5, 2, 9, 1, 5, 6}; int n = 6; // Sort the array in ascending order for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (numbers[j] > numbers[j + 1]) { // Swap if the element found is greater int temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } } // Print the sorted array for (int i = 0; i < n; ++i) { printf("%d ", numbers[i]); } return 0; }

Dive into the world of sorting algorithms and discover how to arrange array elements!

10. 2D Array Magic ✨

c
#include <stdio.h> int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // Access and print elements of a 2D array for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { printf("%d ", matrix[i][j]); } printf("\n"); } return 0; }

Delve into the fascinating realm of 2D arrays, opening new possibilities in programming!

Remember, arrays are your companions in solving real-world problems. Embrace these programs, and let the coding journey begin! Happy coding! 🚀💻 #CProgramming #ArrayMagic #ProgrammingFun