C Programming
About Lesson
1: Introduction to Arrays

An array in C is a collection of elements of the same data type stored in contiguous memory locations. It provides a convenient way to store and manipulate multiple values of the same type under a single name.

  • Declaration Syntax:

     
    data_type array_name[size];
  • Example:

    int numbers[5]; // Array of 5 integers
2: One-Dimensional Arrays

A one-dimensional array is a linear collection of elements stored in a contiguous block of memory.

  • Initialization Syntax:

    int numbers[5] = {10, 20, 30, 40, 50}; // Initializing array elements
  • Accessing Array Elements:

    printf("First element: %dn", numbers[0]); // Accessing first element
3: Multi-Dimensional Arrays

A multi-dimensional array is an array of arrays, allowing you to represent data in multiple dimensions (e.g., 2D, 3D).

  • Declaration Syntax:

    data_type array_name[row_size][column_size];
  • Example:

    int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
    };
  • Accessing Array Elements:

    printf("Element at row 2, column 1: %dn", matrix[1][0]); // Accessing element (4)
4: Array Operations and Manipulation

Arrays support various operations and manipulations, such as insertion, deletion, sorting, searching, and traversal.

  • Traversal (Looping through Arrays):

    int i;
    for (i = 0; i < 5; i++) {
    printf("%d ", numbers[i]); // Printing array elements
    }
  • Array Initialization:

    int values[5] = {0}; // Initialize all elements to 0
  • Sorting Arrays:

    • Using library functions like qsort() for sorting arrays.
  • Searching Arrays:

    • Implementing linear search or binary search algorithms to find elements in arrays.
5: Practice and Examples
  • Practice Exercise:
    • Write a program to find the sum and average of elements in an array.
    • Implement a program to merge two sorted arrays into a single sorted array.
    • Develop a function to transpose a given matrix (2D array).
#include <stdio.h>

// Function to calculate sum and average of elements in an array
void calculateSumAndAverage(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
double average = (double)sum / size;
printf("Sum of elements: %dn", sum);
printf("Average of elements: %.2lfn", average);
}

// Function to merge two sorted arrays into a single sorted array
void mergeSortedArrays(int arr1[], int size1, int arr2[], int size2, int result[]) {
int i = 0, j = 0, k = 0;

while (i < size1 && j < size2) {
if (arr1[i] <= arr2[j]) {
result[k++] = arr1[i++];
} else {
result[k++] = arr2[j++];
}
}

while (i < size1) {
result[k++] = arr1[i++];
}

while (j < size2) {
result[k++] = arr2[j++];
}
}

// Function to transpose a matrix (2D array)
void transposeMatrix(int matrix[][3], int rows, int cols) {
int transposed[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = matrix[i][j];
}
}

// Display transposed matrix
printf("Transposed Matrix:n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transposed[i][j]);
}
printf("n");
}
}

int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
calculateSumAndAverage(numbers, size);

int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8};
int mergedSize = size + size;
int merged[mergedSize];
mergeSortedArrays(arr1, size, arr2, size, merged);
printf("Merged Sorted Array: ");
for (int i = 0; i < mergedSize; i++) {
printf("%d ", merged[i]);
}
printf("n");

int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
transposeMatrix(matrix, 3, 3);

return 0;
}

Arrays are fundamental data structures in C programming used to store and manipulate collections of data efficiently. Mastering array operations and manipulations is crucial for developing robust algorithms and solving real-world problems effectively. Practice working with arrays to strengthen your programming skills and understanding of data structures.