C Programming
Arrays in C Programming - Algorithm and Practical examples
- How to Declare array in C programming
- How to Access array in C programming
- 5 Arrays examples in C Programming
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 -
- the data type of the elements,
- the name of the array,
- 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[] = {'a', 'b', 'c', 'd'};
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[] = {'a', 'b', 'c', 'd'};
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 < 10; i++) {
printf("%d ", numbers[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:
- Declare an array of integers called “numbers” with a size of 10.
- Initialize the array with a set of values using a for loop.
- Set a variable called “max” to the first element in the array.
- Use a for loop to iterate over the rest of the elements in the array.
- For each element, if it is greater than the current value of “max”, set “max” to the value of the element.
- After the loop has completed, “max” will contain the maximum value in the array.
¶Code:
#include <stdio.h>
int main() {
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 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:
- Declare an array of integers called “numbers” with a size of 10.
- Initialize the array with a set of values using a for loop.
- Declare a variable called “sum” and initialize it to 0.
- Use a for loop to iterate over the elements in the array.
- For each element, add its value to the “sum” variable.
- After the loop has completed, divide “sum” by the size of the array to calculate the average.
¶Code:
#include <stdio.h>
int main() {
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 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:
- Declare an array of integers called “numbers” with a size of 10.
- Initialize the array with a set of unsorted values using a for loop.
- Use a nested for loop to iterate over the elements in the array.
- For each element, use an if statement to compare it to the elements that come after it.
- If the element is greater than one of the elements that come after it, swap their values using a temporary variable.
- Repeat the process until the array is sorted in ascending order.
¶Code:
#include <stdio.h>
int main() {
int numbers[10] = {5, 3, 7, 2, 8, 1, 9, 4, 6, 0};
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:
- Declare two arrays of integers called “source” and “destination” with sizes of 10.
- Initialize the “source” array with a set of values using a for loop.
- Use a for loop to iterate over the elements in the “source” array.
- For each element, assign its value to the corresponding element in the “destination” array.
- When the loop has completed, the “destination” array will contain the same values as the “source” array.
¶Code:
#include <stdio.h>
int main() {
int source[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 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:
- Declare an array of integers called “numbers” with a size of 10.
- Initialize the array with a set of values using a for loop.
- Declare a variable called “search_value” and initialize it with the value that you want to search for in the array.
- 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 <stdio.h>
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] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 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.
Functions in C Programming - with Practical examples
Recursion in C Programming Language with Practical Examples
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development