Array, Declaring Arrays and Array Initialization
What is an Array in C?
Array is a data structure provided by the C programming language. It is a data structure which is able and used to store a collection of data. In other words, it can be said that an array is a collection of data / variables of same type.
Instead of specifying separate or individual variables such as number1, number..., the user creates a single array variable such as numbers and uses numbers [0],], numbers [30, and..., numbers 97] to denote them.
To access a specific element in an array, an index is used.
Array | 10 | 20 | 30 | 40 | 50 |
Location | 0 | 1 | 2 | 3 | 4 |
The above array is declared as int q [5];
q [0] = 10; q [1] = 20; q [2] = 30; q [3] = 40; q [4] = 50;
In the above figure 10,20,30,40,50 are actual data items and 0, 1, 2, 3, 4 are index variables.
Similarly,
If we declare an array as int w [10];
q [0] = 1; q [1] = 8; q [2] = 3; q [3] = 40; q [4] = 5;q [5] = 79; q [6] = 22; q [7] = 6; q [8] = 4; q [9] = 59;
Array | 1 | 8 | 3 | 40 | 5 | 79 | 22 | 6 | 4 | 59 |
Location | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Array declarations in C
While Declaring Arrays in C, a programmer declares an array by specifying the type of elements and the number of elements required by the array as follows:
[arraySize] type arrayName;
A single-dimensional array is what this is called. arraySize must be a positive integer constant, and type can be any valid C data type. For example, to declare a 30-element double-type array called amount, use the following phrase:
[30] double amount
Amount is now a variable array that can hold up to 30 double numbers.
Another example is:
#include<stdio.h>
int main(){
int i=0;
int q[5];//declaration of array
q[0]=10;//initialization of array
q[1]=20;
q[2]=30;
q[3]=40;
q[4]=50;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",q[i]);
}//end of for loop
return 0;
}
Array Initialization
In C, you can create an array one by one or with a single statement like this:
Double amount[5]={10.0,32.1,52.1,69.32,32.30}
The number of values between {} braces cannot exceed the number of members in the array declared between square brackets [].
If you don't specify the array size, a small array is generated to hold the initialization. As a result, if you write:
Double amount[]={10.0,32.1,52.1,69.32,32.30}
The array you build will be identical to the one you created in the previous example. Here's an example of how to declare an array and find the average of all of its elements:
#include <stdio.h>
int main()
{
int average = 0;
int sum =0;
int p=0;
/* Array- declaration – length 4*/
int number[4];
/* We are using a for loop to traverse through the array
* While also storing the entered values in the array
*/
for (p=0; p<4;p++)
{
printf("Enter number %d \n", (p+1));
scanf("%d", &number[p]);
}
for (p=0; p<4;p++)
{
sum = sum+number[p];
}
average = sum/4;
printf("Average of entered number is: %d", average);
return 0;
}
#include <stdio.h>
int main()
{
int sum =0;
int p=0;
/* Array- declaration – length 5*/
int numbers[4];
/* We are using a for loop to traverse through the array
* Whilealso storing the entered values in the array
*/
for (p=0; p<4;p++)
{
Printf("\n Enter number no. %d = ", (p+1));
scanf("%d", &numbers[p]);
}
for (p=0; p<4;p++)
{
sum = sum+numbers[p];
}
printf("Sum of entered number is: %d", sum);
return 0;
}