Unit 3 - C1 (Arrays, One Dimentional Array)
Unit 3 - C1 (Arrays, One Dimentional Array)
Topics Covered:
1. Introduction to Arrays
2. One - dimensional arrays
The variables we declare using the data types like int, char, float, double
and so on will store only one value in them at any given point of time.
use a derived data type provided by the C language. This derived data type is called
array.
A data structure is the way data is stored in the machine and the functions used to
access that data.
An array is a data structure that is a collection of variables of one type that are accessed
through a common name.
An array is a contiguous memory locations of same type of elements, and is given one
Each memory cell of an array is accessed by an index value that uniquely identifies it.
First, array has to be declared of some size and of a data type before we use array in our
Array is also called as subscripted data type. This is because the values of the array are
The size of an array is the length of the array times the size of an element.
Types of arrays:
One-dimensional arrays
Two-dimensional arrays
Multi-dimensional arrays
Array whose element are specified by one subscript are called single
Similarly, array whose elements are specified by two and three subscripts
Like any other variable, First we have to declare an array before we use it
data_type array_name[size];
Where,
data_type is any of the data types like int, float, char, double,.. All
Dept. of ECE, RGMCET(Autonomous), Nandyal 9
array_name is any valid name that we want to give to the array.
size is a positive integer value specifying the size of the array, i.e., number of values the
array stores. This size is not the memory size occupied by the array.
Examples:
Here the array name is age
int age[10];
Array age stores 10 integer values.
Here the array name is height
float height[15];
Array height stores 15 float values.
Here the array name is gender
char gender[8];
Array gender stores 8 character values.
Dept. of ECE, RGMCET(Autonomous), Nandyal 10
Giving values to an Array:
After an array is declared, its elements must be initialized. Otherwise, they will
Arrays can also be initialized with values at the declaration time itself.
will declare the variable number as an array of size 3 and will assign zero to
each element.
many elements will be initialized. The remaining elements will be set zero
automatically.
For example,
will initialize the first three elements 0.0, 15.75 and -10.0 and the remaining two elements
to zero.
initialized elements.
For example,
will declare the counter array to contain four elements with initial values 1.
This approach works fine as long as we initialize every element in the array.
In the above example, VAL is the name of the array storing 5 integer values in it.
The values given in the flower braces {…} are assigned to the array and this can be
visualized as below.
So,
Arrays can also be assigned with values at any time after the declaration of it.
VAL[0] = 55;
VAL[1] =22;
Arrays can also be assigned with values by using the standard input function scanf().
scanf(“%d”,&VAL[3]);
Assume that, user entered a value of 231 for the above input function. Then array is,
Subscript 0 1 2 3 4
NOTE:
The array cell index starts with ‘zero’ and not by ‘one’.
So, if the size of a array is ‘5’, then we can subscript the array up to a value of ‘4’,
float PRICE[2];
int QTY[30];
Example 3:
Write a C program which reads the values into array from user and
separately stores the even and odd values into two different arrays.
Dept. of ECE, RGMCET(Autonomous), Nandyal 29
Example 4:
Write a C program which finds the maximum and minimum elements of the
given array.
Example program 5:
Write a C program which finds whether the given element is available or not in
Write a C program which sorts the elements of given array in ascending order.