Arrays in C programming with examples


Arrays in C programming with examples




An array is a group (or collection) of same data types. For example an int array holds the elements of int types while a float array holds the elements of float types.

Why we need Array in C Programming?

Consider a scenario where you need to find out the average of 100 integer numbers entered by user. In C, you have two ways to do this: 1) Define 100 variables with int data type and then perform 100 scanf() operations to store the entered values in the variables and then at last calculate the average of them. 2) Have a single integer array to store all the values, loop the array to store all the entered values in array and later calculate the average.
Which solution is better according to you? Obviously the second solution, it is convenient to store same data types in one single variable and later access them using array index (we will discuss that later in this tutorial).

How to declare Array in C

int num[35];  /* An integer array of 35 elements */
char ch[10];  /* An array of characters for 10 elements */
Similarly an array can be of any data type such as doublefloatshort etc.

How to access element of an array in C

You can use array subscript (or index) to access any element stored in array. Subscript starts with 0, which means arr[0] represents the first element in the array arr.
In general arr[n-1] can be used to access nth element of an array. where n is any integer number.
For example:
int mydata[20];
mydata
[0] /* first element of array mydata*/
mydata
[19] /* last (20th) element of array mydata*/

Example of Array In C programming to find out the average of 4 integers

#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;

/* Array- declaration – length 4*/
int num[4];

/* We are using a for loop to traverse through the array
* while storing the entered values in the array
*/

for (x=0; x<4;x++)
{
printf
("Enter number %d \n", (x+1));
scanf
("%d", &num[x]);
}
for (x=0; x<4;x++)
{
sum
= sum+num[x];
}

avg
= sum/4;
printf
("Average of entered number is: %d", avg);
return 0;
}
Output:
Enter number 1 
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
Lets discuss the important parts of the above program:

Input data into the array

Here we are iterating the array from 0 to 3 because the size of the array is 4. Inside the loop we are displaying a message to the user to enter the values. All the input values are stored in the corresponding array elements using scanf function.
for (x=0; x<4;x++)
{
printf
("Enter number %d \n", (x+1));
scanf
("%d", &num[x]);
}

Reading out data from an array

Suppose, if we want to display the elements of the array then we can use the for loop in C like this.
for (x=0; x<4;x++)
{
printf
("num[%d]\n", num[x]);
}

Various ways to initialize an array

In the above example, we have just declared the array and later we initialized it with the values input by user. However you can also initialize the array during declaration like this:
int arr[5] = {1, 2, 3, 4 ,5};
OR (both are same)
int arr[] = {1, 2, 3, 4, 5};
Un-initialized array always contain garbage values.

C Array – Memory representation

More Topics on Arrays in C:
2D array – We can have multidimensional arrays in C like 2D and 3D array. However the most popular and frequently used array is 2D – two dimensional array. In this post you will learn how to declare, read and write data in 2D array along with various other features of it.
Passing an array to a function– Generally we pass values and variables while calling a function, likewise we can also pass arrays to a function. You can pass array’s element as well as whole array (by just specifying the array name, which works as a pointer) to a function.
Pointer to array – Array elements can be accessed and manipulated using pointers in C. Using pointers you can easily handle array. You can have access of all the elements of an array just by assigning the array’s base address to pointer variable.

Post a Comment

Previous Post Next Post