C Array
- Array in C language is a collection or group of elements (data).
- All the elements of c array are homogeneous (similar). It has contiguous memory location.
- C array is beneficial if you have to store similar elements. Suppose you have to store marks of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to manage.
- For example we can not access the value of these variables with only 1 or 2 lines of code.
- Another way to do this is array. By using array, we can access the elements easily. Only few lines of code is required to access the elements of array.
Array in C programming |
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.
3) Easy to sort data: To sort the elements of array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the limit. So, it doesn't grow the size dynamically like Linked-List which we will learn later.
How to declare an array?
dataType arrayName[arraySize];
- For example,
float mark[5];
- Here, we declared an array, mark, of floating-point type. And its size is 5.
- Meaning, it can hold 5 floating-point values.
Note:- It's important to note that the size and type of an array cannot be changed once it is declared.
Some important Points to be noted
int mark[5] = {19, 20, 21, 26, 28};
int mark[] = {19, 20, 21, 26, 28};
Let's know how to insert and print array elements
int mark[5] = {19, 20, 21, 26, 28};
// change 4th element to 9
mark[3] = 9;
// take input from the user and insert in third element
cin >> mark[2];
// take input from the user and insert in (i+1)th element
cin >> mark[i];
// print first element of the array
cout << mark[0];
// print ith element of the array
cout >> mark[i-1];
C++ program to store and calculate the sum of 5 numbers entered by the user using arrays.
#include <iostream.h>
#include<conio.h>
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
// Storing 5 number entered by user in an array
// Finding the sum of numbers entered
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Expected Output:-
Enter 5 numbers: 3
4
5
4
2
Sum = 18
Some important Points to be noted
- Arrays have 0 as the first index not 1. So, mark[0] is the first element.
- If the size of an array is n, to access the last element, (n-1) index is used. So, mark[4] is the last element.
- Suppose the starting address of mark[0] is 2140. Then, the next address, a[1], will be 2144, address of a[2] will be 2148 and so on. It's because the size of float is 4 bytes.
- It's possible to initialize an array during declaration. For example,
int mark[5] = {19, 20, 21, 26, 28};
- Another method to initialize array during declaration:
int mark[] = {19, 20, 21, 26, 28};
Let's know how to insert and print array elements
int mark[5] = {19, 20, 21, 26, 28};
// change 4th element to 9
mark[3] = 9;
// take input from the user and insert in third element
cin >> mark[2];
// take input from the user and insert in (i+1)th element
cin >> mark[i];
// print first element of the array
cout << mark[0];
// print ith element of the array
cout >> mark[i-1];
C++ program to store and calculate the sum of 5 numbers entered by the user using arrays.
#include <iostream.h>
#include<conio.h>
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
// Storing 5 number entered by user in an array
// Finding the sum of numbers entered
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Expected Output:-
Enter 5 numbers: 3
4
5
4
2
Sum = 18