Array

C Programming Tutorial : Arrays

Arrays in C Programming

Welcome to the Codes With Pankaj "Arrays in C Programming" tutorial! This tutorial will guide you through the concepts and practical applications of arrays in C.

Table of Contents


1. Introduction to Arrays

An array in C is a collection of elements of the same data type, stored in contiguous memory locations. Arrays provide a way to store multiple values in a single variable, which can be accessed using an index.

2. Declaring and Initializing Arrays

Declaring an Array

To declare an array, specify the data type, array name, and size.

data_type array_name[size];

Example:

int numbers[5];

Initializing an Array

Arrays can be initialized at the time of declaration.

data_type array_name[size] = {value1, value2, ...};

Example:

int numbers[5] = {1, 2, 3, 4, 5};

If fewer values are provided, the remaining elements are initialized to 0.

int numbers[5] = {1, 2}; // {1, 2, 0, 0, 0}

3. Accessing Array Elements

Array elements are accessed using their index, starting from 0.

array_name[index]

Example:

int numbers[5] = {1, 2, 3, 4, 5};
printf("%d\n", numbers[0]); // Output: 1
printf("%d\n", numbers[4]); // Output: 5

4. Array Operations

Common operations on arrays include traversal, searching, and modifying elements.

Traversal Example:

#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        printf("%d\n", numbers[i]);
    }
    return 0;
}

Searching Example:

#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    int search = 3;
    int found = 0;

    for (int i = 0; i < 5; i++) {
        if (numbers[i] == search) {
            found = 1;
            break;
        }
    }

    if (found) {
        printf("%d found in array\n", search);
    } else {
        printf("%d not found in array\n", search);
    }

    return 0;
}

5. Multi-dimensional Arrays

Multi-dimensional arrays are arrays of arrays. The most common is the two-dimensional array, used for matrices.

Declaring a Two-dimensional Array

data_type array_name[rows][columns];

Example:

int matrix[3][3];

Initializing a Two-dimensional Array

data_type array_name[rows][columns] = {{value1, value2, ...}, {value3, value4, ...}, ...};

Example:

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

6. Passing Arrays to Functions

Arrays can be passed to functions by specifying the array name without indices.

Function Declaration:

void function_name(data_type array_name[], int size);

Example:

#include <stdio.h>

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    printArray(numbers, 5);
    return 0;
}

7. Common Array Manipulations

Finding the Maximum Element

#include <stdio.h>

int findMax(int arr[], int size) {
    int max = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    printf("Maximum element: %d\n", findMax(numbers, 5));
    return 0;
}

Reversing an Array

#include <stdio.h>

void reverseArray(int arr[], int size) {
    for (int i = 0; i < size / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[size - 1 - i];
        arr[size - 1 - i] = temp;
    }
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    reverseArray(numbers, 5);
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");
    return 0;
}

8. Common Mistakes and Best Practices

Common Mistakes

  1. Array Out of Bounds: Accessing elements outside the array's size.

    int numbers[5] = {1, 2, 3, 4, 5};
    printf("%d\n", numbers[5]); // Error: Index out of bounds
  2. Uninitialized Arrays: Using arrays without initializing them can lead to undefined behavior.

Best Practices

  1. Bounds Checking: Always ensure that array indices are within valid bounds.

  2. Consistent Initialization: Initialize arrays to avoid undefined values.

  3. Meaningful Names: Use descriptive names for arrays to enhance code readability.

9. Exercises

Try these exercises to practice using arrays in C:

  1. Exercise 1: Write a program to find the sum of all elements in an array.

  2. Exercise 2: Write a program to copy elements from one array to another.

  3. Exercise 3: Write a program to sort an array in ascending order.

  4. Exercise 4: Write a program to find the second largest element in an array.

  5. Exercise 5: Write a program to add two matrices.


We hope this tutorial has helped you understand arrays in C programming. Practice with the exercises provided to reinforce your understanding. Happy coding!

For more tutorials, visit www.codeswithpankaj.com.

Last updated