Arrays in C language
Array
Hello! Hope you all are doing great, in the previous
blog we had learned many topic related to C, for example variable, operator, loops.
In today’s blog we will start a new topic, we will
learn to work with arrays. you will learn to declare, initialize and access
elements of an array with help of simple example.
What is array?
As you listen word array what comes in your mind? That what
is this array and why we are studying this.
An array is a variable that can store multiple value but of same data type.For example, if you want to store 50 Integer, you can create an array for it.An array is a fixed sized sequenced collection of elements of same data type.it is simply a group of like type data.in its simplest form, an array can be used to represent a list of a number, or a list of names.
Some example where the concept of an array can be
used.
1.list of employees in an organization.
2.test scores of a class of students.
3.list of customers and their telephone numbers.
4.table of daily rainfall data.
![]() |
| SMART ENGINEERS |
How to declare an array?
In array declaration we declare size of array and data
type of that array elements.
Datatype arrayName [array size];
For example:-
float marks[5];
Int marks [10];
Here, we declare an array, marks, of float point type
and its size is 5. Meaning, it can hold 5 floating-point values.
It is important to note that the size and type of an
array can not be changed once it is declared.
Access Array Elements
You can access elements of an array by indices. suppose
you declared an array mark as above. The first element is marks [0], the second
element is marks [1] and so on.
Few points to be remember:
1.Arrays have 0 as the first index, not 1. In this
example, marks [0] is the first element.
2.If the size of an array is n, to access the last
element, the n-1 index is used. In this example, marks [4]
3.Suppose the starting address of marks [0] is 2120d.
Then, the address of the marks [1] will be 2124d. Similarly, the address of
marks [2] will be 2128d and so on.
4.This is because the size of a float is 4 bytes.
How to initialize an array?
It is possible to initialize an array during
declaration. For example
1.int marks [5] = {1, 10, 8, 17, 9};
You can also initialize an array like this.
2.int marks [] = {1, 10, 6, 17, 9};
Here, we haven't specified the size. However, the
compiler knows its size is 5 as we are initializing it with 5 elements.
And if you want to change the value in between of array
you can also do that.
Changing of value of array:-
int marks [5] = {1, 10, 8, 17, 9}
// make the value of the third element to -1
marks [3] = -1;
// make the value of the fifth element to 0
marks [2] = 0;
Input and output of array elements:-
1. take input and store it in the 3rd element
scanf("%d", &mark[2]);
2.take input and store it in the individual
location....
scanf("%d", &mark[i-1]);
here's how you can print an individual element of an
array.
1.print the first element of the array
printf("%d", mark[0]);
2.print the third element of the array
printf("%d", mark[2]);
printf("%d", mark[i-1]);
now, I am giving you some simple example of array Implementation.
1.#include<stdio.h>
main()
{
int
a[20],i;
printf("enter
the value:");
for(i=0;i<20;i++)
{
scanf("%d",a);
}
for(i=0;i<20;i++)
{
printf("value
is:%d\n",a);
}
return
0;
}
2. Program
to find the average of n numbers using arrays
#include <stdio.h>
int main()
{
int
marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0;
i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum +=
marks[i];
}
average =
sum/n;
printf("Average = %d", average);
return 0;
}
In this blog we studied about one dimensional array, in next blog we will learn about multi dimensional array in c.

sir...you are great ......what a nice content.
ReplyDelete