Follow Us:
Understanding Array in an easy way
An array is a data structure, which can store a fixed-size collection of elements of the same data type. It is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Each item in an array is called an element, and each element is accessed by its numerical index. As such, arrays are sometimes referred to as indexed arrays.
Let’s understand this with a very simple example.
(Let’s take the most general example) Suppose you want to store all your classmates’ names using C. Now you can do-
- Try to declare all the variables one by one, like st_name0, st_name1, st_name3,…,st_name(n). Now the problem you’ll be facing is that if the ‘n’ is a large number let’s say 100, then it will be very difficult to declare all the variables one by one.
- Or you can define all those variables in just one variable, i.e store them in an Array. And this is way better and easier than the previous option.
Declaration:
The general form of declaring an Array is-
<type of array><arrayName>[arraySize]; //This one is user-defined
Or,
<type of array><Name> [ Size ]={list_of_inputs}; //This one is predefined
The difference is straightforward. In the first case, you are just declaring the array while in the second case you are also providing the list.
Now,
- The <type of array> could be int, float, char, or anything else. It signifies what type of data you want to store.
- The array_name could be anything again. It signifies the variable name. For example, you want to store some numbers. Then you give the list a name say A. Now list A will store some numbers.
- Now the array_size is simply specifying the size of the array. That’s why it should be in ‘integer’. The arr_size actually tells the compiler how much space/byte is allocated. Here comes a very important thing. The array is indexed from 0 to (arr_size)-(1). Means it will start like this- arr_size[0], arr_size[1], arr_size[2],…
- The word I used just now i.e index, actually means the location for each variable.
For example,
int A[10]; //An array of 10 Integers
char st_name[50]; //An array of 50 Character
What do they mean?
Now let’s see how it actually works.
int A[10]; //An array that can store upto 10 Integers
Here the number ’10’ indicates the size of the input.
See the diagram below
Index- 0 1 2 3 4 5 6 7 8 9
Input Value-
Let’s see a sample code for better understanding.
The question is-
Find out the sum of all elements from an integer array
Before going to the question, first, let us understand it. In the question we’ve to declare an integer array and then we’ll give some inputs and then we’ll run the code and it will give us the total sum of all the inputs.
So, let’s see the code.
#include <stdio.h>
int main()
{
//Declaring the variables
int n, a[n], sum = 0;
//Asking the user for the size
printf("Enter the size :");
scanf("%d", &n);
//%d is for integer value
//Here we are asking the user to input the elements
for (int i = 0; i < n; i++ )
{
printf("Enter the element here:");
scanf("%d", &a[i]);
}
//Here, in this section we are doing our main work means the sum of all inputs
for (int i = 0; i < n; i++)
{ sum = sum + a[i];
}
printf("Sum of all elements is: %dn", sum);
return 0;
}
I hope you’ve understood the code.
Now we’ll understand the meaning of the code.
- a[n]: This type of declaration means it can store up to n data. So then ‘n‘ is the size of a.
- n is used for collecting the size value from the user.
- Why for-loop? Because this is not an ‘either-or’ condition. This means we are just creating a condition that the code will only work for 0 ≤ i < n. If this condition violates then the program will not work anymore.
Now, you may ask one question.
What if we use a[] or a[]={1,2,3,4,5} instead of a[n]?
Well as we know that we can initialize arrays in many ways. But if you forget, I’m showing it in the form of examples here-
1. int num[7]={1,2,3,4,5,6};
//Here we are declaring the array with the size 7 and the input data i.e {1,2,3,4,5,6}.
2. int num[]={1,2,3,4,5,6};
//Here we are not specifying the space but we are giving the input values
3. int num[];
//Here we've neither specified the size nor the input values
4. int num[n]; //Here we're declaring num[ ] with a size of n. The size can be controllable by the user. So this
In the first case, it is called a predefined array. This means we are declaring all the values on our own.
#include <stdio.h>
int main()
{
int a[7]={1,2,3,4,5,6}, sum = 0;
for (int i = 0; i < 7; i++)
{
sum = sum + a[i];
}
printf("Sum of all elements are: %dn", sum);
return 0;
}
In the 2nd case, we are not specifying the size but we entered 6 inputs. So in this case the code will look like this-
#include <stdio.h>
int main()
{
int a[]={1,2,3,4,5,6}, sum = 0;
for (int i = 0; i < 6; i++)
{
sum = sum + a[i];
}
printf("Sum of all elements are: %dn", sum);
return 0;
}
In the 3rd case, it has no declared storage. So its storage class will become auto-declared storage. And as we also don’t initialize that array, it will take garbage values.
In our main code where we wrote int n,a[100],sum=0;
.
When We wrote a[100] it takes the storage as 100 which means we can store 100 elements in a. The index will be from 0 to 99. Then in the for-loop:
printf("Enter the size :");
scanf("%d", &n);
// This scanf takes an user generated size of a, we'll use it then in the for-loop to take the elements.
for (int i = 0; i < n; i++ )
{
printf("Enter the element here:");
scanf("%d", &a[i]);
}
In this case, n could be anything but can never exceed the value 100, if it does, then the code will stop.
Till now whatever we’ve discussed, is based on a one-dimensional array, but it could be multidimensional too.
Multidimensional Arrays:
Now when we talk about Multidimensional arrays, we generally mean Matrix. But if you don’t know ‘what is matrix’ then I’ll suggest you read that post too from here.
Now, here I’m giving an example that will help me to explain the 2-D array.
Let’s understand the questions. In the question we’ve to add two matrices and the Order of the Matrices(which means the row and column of a matrix) and the elements of the matrices will be taken from the users.
#include <stdio.h>
int main() {
//Declaring the variables
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
//Taking the values of order of the matrices from the users
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
//Asking for the elements of the matrices
printf("nEnter elements of 1st matrix:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("nSum of two matrices: n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
if (j == c - 1)
{
printf("nn");
}
}
return 0;
}
You can clearly see how a 2D array works. So in 2-Ds, we write ‘a[ ][ ]‘ where we are defining two variables (whatever the name of the variables, doesn’t matter) where the first [ ] defines the rows and the second [ ] defines the columns.
Now if we understand the 2-D array then we can easily understand 3-D.
Let’s see this handwritten note, I hope you will understand how multidimensional arrays works
So that is it for today. Hope you’ve understood all these things. If you want to know something more then comment to us below and share this with your friends also.