D A R K - C O D E R

Loading

C Programming – Arrays

Arrays are a fundamental data type in C programming, allowing you to store and manipulate data collection in a structured way. An array is a contiguous block of memory used to store a fixed number of elements of the same data type.

In C, there are two types of arrays:

  • single-dimensional arrays
  • multi-dimensional arrays

Single-dimensional arrays:

are the most basic type of array, and they consist of a contiguous block of memory that stores a fixed number of elements of the same data type. Here is an example of how to declare and initialize a single-dimensional array in C:

#include    

int main() {
  // Declaring and initializing a single-dimensional array of integers
  int arr[5] = {1, 2, 3, 4, 5};

  // Accessing elements of the array using their index
  printf("First element: %d\n", arr[0]);
  printf("Second element: %d\n", arr[1]);
  printf("Third element: %d\n", arr[2]);

  return 0;
}

also known as arrays of arrays, allow you to store and manipulate data in a more complex, multi-layered structure. In C, multi-dimensional arrays are implemented as arrays of arrays, with each inner array representing a row of the multi-dimensional array.

Here is an example of how to declare and initialize a two-dimensional array in C:

#include   

int main() {
  // Declaring and initializing a two-dimensional array of integers
  int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

  // Accessing elements of the array using their indices
  printf("First element: %d\n", arr[0][0]);
  printf("Second element: %d\n", arr[0][1]);
  printf("Third element: %d\n", arr[0][2]);
  printf("Fourth element: %d\n", arr[1][0]);
  printf("Fifth element: %d\n", arr[1][1]);
  printf("Sixth element: %d\n", arr[1][2]);
  printf("Seventh element: %d\n", arr[2][0]);
  printf("Eighth element: %d\n", arr[2][1]);
  printf("Ninth element: %d\n", arr[2][2]);

  return 0;
}

The output of this program would be:

First element: 1
Second element: 2
Third element: 3
Fourth element: 4
Fifth element: 5
Sixth element: 6
Seventh element: 7
Eighth element: 8
Ninth element: 9

It is important to note that the size of an array must be a constant expression, which means it must be a fixed value that can be determined at compile time. This means that you cannot use a variable as the size of an array.

In C, arrays are indexed starting at 0, so the first element of an array is at index 0, the second element is at index 1, and so on. Here is an example of how to declare and initialize an array in C:

#include    

int main() {
  // Declaring and initializing an array of integers
  int arr[5] = {1, 2, 3, 4, 5};

  // Accessing elements of the array using their index
  printf("First element: %d\n", arr[0]);
  printf("Second element: %d\n", arr[1]);
  printf("Third element: %d\n", arr[2]);

  return 0;
}

The output of this program would be:

First element: 1
Second element: 2
Third element: 3

You can also use a loop to iterate over the elements of an array and perform operations on them. Here is an example of using a for loop to sum the elements of an array:

#include    

int main() {
  int arr[5] = {1, 2, 3, 4, 5};
  int sum = 0;

  // Iterating over the elements of the array and adding them to the sum
  for (int i = 0; i < 5; i++) {
    sum += arr[i];
  }

  printf("Sum: %d\n", sum);

  return 0;
}

The output of this program would be:

Sum: 15

It is important to note that in C, arrays are passed to functions by reference, which means that changes made to the array within the function are reflected in the original array. Here is an example of a function that takes an array as an argument and modifies its elements:

#include    

// Function that takes an array and its size as arguments and multiplies each element by 2
void multiply_by_2(int *arr, int size) {
  for (int i = 0; i < size; i++) {
    arr[i] *= 2;
  }
}

int main() {
  int arr[5] = {1, 2, 3, 4, 5};

  // Printing the original array
  printf("Original array: ");
  for (int i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");

  // Calling the function and passing the array and its size as arguments
  multiply_by_2(arr, 5);

  // Printing the modified array
  printf("Modified array: ");
  for (int i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");

  return 0;
}

The output of this program would be:

Original array: 1 2 3 4 5
Modified array: 2 4 6

To remove an element from an array in C, you can either shift the elements after the removed element to the left to fill the gap, or overwrite the element with the last element in the array and then reduce the size of the array.

Here is an example of how to remove an element from an array by shifting the elements to the left:

#include   

int main() {
  // Declaring and initializing an array of integers
  int arr[5] = {1, 2, 3, 4, 5};
  int size = 5;

  // Removing the element at index 2 by shifting the elements to the left
  for (int i = 2; i < size - 1; i++) {
    arr[i] = arr[i + 1];
  }
  size--;

  // Printing the modified array
  for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");

  return 0;
}

The output of this program would be:

1 2 4 5

How to overwriting an element from Arrays in c with code?

Here is an example of how to remove an element from an array by overwriting it with the last element:

#include   

int main() {
  // Declaring and initializing an array of integers
  int arr[5] = {1, 2, 3, 4, 5};
  int size = 5;

  // Removing the element at index 2 by overwriting it with the last element
  arr[2] = arr[size - 1];
  size--;

  // Printing the modified array
  for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");

  return 0;
}

The output of this program would be:

1 2 5 4

It is important to note that these methods modify the original array and do not create a new one without the removed element.

This information has been helpful in understanding the types of arrays in C programming. Thank you for reading!