Arrays in C Programming - Algorithm and Practical examples

An array in C programming is a collection of variables of the same data type, stored contiguously in memory and accessed using a common name. Each element in the array is identified by an index, which is an integer value that specifies the position of the element in the array.

¶How to Declare array in C programming

To declare an array in C, you need to specify -

  1. the data type of the elements,
  2. the name of the array,
  3. and the size of the array.

For example, the following code declares an array of integers called numbers with a size of 10 :

int numbers[10]; 

This code declares an array of integers called “numbers” with a size of 10. The size of the array determines the number of elements that it can store. In this case, the “numbers” array can store 10 integers.

¶Some other declaring array examples

Here are some other examples of how to declare an array in C:

Example 2: Declaring an array of characters:

char letters[5]; 

This code declares an array of characters called letters with a size of 5 . The letters array can store 5 characters.

Example 3: Declaring an array of structures:

struct Person < char name[50]; int age; >; struct Person people[10]; 

This code declares a structure called Person that has two members: a character array called name and an integer called age . It then declares an array of Person structures called people with a size of 10 . The people array can store 10 Person structures.

Example 4: Declaring arrayy with a set of values: It’s also possible to initialize an array with a set of values at the time of declaration. For example, the following code declares an array of characters called “letters” and initializes it with the values ‘a’, ‘b’, ‘c’, and ‘d’:

char letters[] = ; 

In summary, declaring an array in C involves specifying the data type of the elements, the name of the array, and the size of the array. You can also initialize the array with a set of values at the time of declaration. Arrays are useful for storing and manipulating large amounts of data efficiently and can be processed using loops.

¶How to Access array in C programming

To access an element in the array, you can use the array name followed by the index of the element in square brackets.

For example, to access the third element in the numbers array, you would use the following code:

int third_element = numbers[2]; 

Note that the index of the first element in an array is 0 , not 1 . Therefore, the third element in the numbers array has an index of 2 .

You can also initialize an array with a set of values at the time of declaration. For example, the following code declares an array of characters called “letters” and initializes it with the values ‘a’, ‘b’, ‘c’, and ‘d’:

char letters[] = ; 

You can also use a loop to process each element in an array.

For example, the following code uses a for loop to print all the elements in the “numbers” array:

for (int i = 0; i

In summary, an array in C programming is a collection of variables of the same data type that are stored contiguously in memory and accessed using a common name and an index. They can be used to store and manipulate large amounts of data efficiently and can be processed using loops.

¶5 Arrays examples in C Programming

¶Example 1: Finding the maximum value in an array

This example demonstrates how to use an array and a loop to find the maximum value in a set of numbers.

¶Algorithm:

  1. Declare an array of integers called “numbers” with a size of 10.
  2. Initialize the array with a set of values using a for loop.
  3. Set a variable called “max” to the first element in the array.
  4. Use a for loop to iterate over the rest of the elements in the array.
  5. For each element, if it is greater than the current value of “max”, set “max” to the value of the element.
  6. After the loop has completed, “max” will contain the maximum value in the array.

¶Code:

#include int main() < int numbers[10] = ; int max = numbers[0]; for (int i = 1; i < 10; i++) < if (numbers[i] >max) < max = numbers[i]; >> printf("The maximum value in the array is %d.\n", max); return 0; > 

¶Output:

The maximum value in the array is 10. 

¶Example 2: Calculate the average of a set of numbers:

¶Algorithm:

  1. Declare an array of integers called “numbers” with a size of 10.
  2. Initialize the array with a set of values using a for loop.
  3. Declare a variable called “sum” and initialize it to 0.
  4. Use a for loop to iterate over the elements in the array.
  5. For each element, add its value to the “sum” variable.
  6. After the loop has completed, divide “sum” by the size of the array to calculate the average.

¶Code:

#include int main() < int numbers[10] = ; int sum = 0; for (int i = 0; i < 10; i++) < sum += numbers[i]; >float average = (float)sum / 10; printf("The average of the numbers is %.2f.\n", average); return 0; > 

¶Output:

The average of the numbers is 5.50. 

In this example, the array numbers is used to store a set of numbers, and a loop is used to iterate over the elements and calculate the sum. The average is then calculated by dividing the sum by the size of the array. Arrays are a useful tool for storing and manipulating large amounts of data efficiently, and they can be used in a variety of contexts to solve many types of problems.

¶Example 3: Sorting an array in ascending order

This example demonstrates how to use an array and a loop to sort a set of numbers in ascending order.

¶Algorithm:

  1. Declare an array of integers called “numbers” with a size of 10.
  2. Initialize the array with a set of unsorted values using a for loop.
  3. Use a nested for loop to iterate over the elements in the array.
  4. For each element, use an if statement to compare it to the elements that come after it.
  5. If the element is greater than one of the elements that come after it, swap their values using a temporary variable.
  6. Repeat the process until the array is sorted in ascending order.

¶Code:

#include int main() < int numbers[10] = ; for (int i = 0; i < 10; i++) < for (int j = i + 1; j < 10; j++) < if (numbers[i] >numbers[j]) < int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; >> > printf("The sorted array is: "); for (int i = 0; i < 10; i++) < printf("%d ", numbers[i]); >printf("\n"); return 0; > 

¶Output:

The sorted array is: 0 1 2 3 4 5 6 7 8 9 

¶Example 4: Copying the elements of one array to another

This example demonstrates how to use two arrays and a loop to copy the elements of one array to another.

¶Algorithm:

  1. Declare two arrays of integers called “source” and “destination” with sizes of 10.
  2. Initialize the “source” array with a set of values using a for loop.
  3. Use a for loop to iterate over the elements in the “source” array.
  4. For each element, assign its value to the corresponding element in the “destination” array.
  5. When the loop has completed, the “destination” array will contain the same values as the “source” array.

¶Code:

#include int main() < int source[10] = ; int destination[10]; for (int i = 0; i < 10; i++) < destination[i] = source[i]; >printf("The destination array is: "); for (int i = 0; i < 10; i++) < printf("%d ", destination[i]); >printf("\n"); return 0; > 

¶Output:

The destination array is: 1 2 3 4 5 6 7 8 9 10 

¶Example 5: Searching for a value in an array

This example demonstrates how to use an array and a loop to search for a specific value in an array.

¶Algorithm:

  1. Declare an array of integers called “numbers” with a size of 10.
  2. Initialize the array with a set of values using a for loop.
  3. Declare a variable called “search_value” and initialize it with the value that you want to search for in the array.
  4. Use a for loop to iterate over the elements in the array.

For each element, use an if statement to check if it is equal to the search value. If the element is equal to the search value, print a message indicating that the value has been found and return from the function. If the loop has completed and the search value has not been found, print a message indicating that the value is not in the array.

#include void search(int numbers[], int size, int search_value) < for (int i = 0; i < size; i++) < if (numbers[i] == search_value) < printf("%d found at index %d\n", search_value, i); return; >> printf("%d not found in the array\n", search_value); > int main() < int numbers[10] = ; search(numbers, 10, 5); search(numbers, 10, 11); return 0; > 

¶Output:

5 found at index 4 11 not found in the array 

In summary, arrays are a useful data structure in C programming that allow you to store and manipulate large amounts of data efficiently. They can be declared by specifying the data type of the elements, the name of the array, and the size of the array. You can use loops to process the elements of an array and perform various operations such as finding the maximum value, sorting the elements, reversing the order of the elements, copying the elements to another array, or searching for a specific value.