Drawback of ordinary variables
Consider the following program,
#include <stdio.h>
#include <conio.h>
main()
{
int x;
clrscr();
x=5;
x=10;
printf("x= %d", x);
getch();
}
No doubt, this program will print the value of x as 10. Why so? Because, when a value 10 is assigned to x, the earlier value of x, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value at a time.
Array
Real-time example for an array
In real-time situations, need to store more than value at a time in a single variable. For example, suppose to arrange the percentage marks obtained by 100 students in ascending order. In such a case, have two options to store these marks in memory:
a) Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student’s marks.
b) Construct one variable capable of storing or holding all the hundred values.
Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables.
An array is a collective name given to a group of similar quantities. Note: the quantities must be ‘similar’. Each member in the group is referred to by its position in the group. For example, assume the following group of values, which represent percentage marks obtained by five students. per = {48, 88, 34, 23, 96} If need to refer to the second value of the group, the usual method used is per2, similarly, the fourth value of the group is referred as per4.
But in C, the counting of elements begins with 0 and not with 1. Thus, in this example per[3] refers to 23 and per[4] refers to 96.
In general, the notation would be per[i], where i can take a value 0, 1, 2, 3, or 4, depending on the position of the element being referred. Here, per is the subscripted variable, whereas i is its subscript.
Remember that all elements of any given array must be of the same type i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.
Properties of an array
Important points to be noted while using arrays
Declaration of an array
Like ordinary variables, an array variable should also be declared. An array needs to be declared so that the compiler will know what kind of an array and how large an array we want.
The general form or syntax of declaring an array is datatype arrayName[subscript];
Where,
For example, int marks[5];
Here,
Dimensioning: The subscript used to declare an array is sometimes called a dimension and the declaration for the array is often called to as dimensioning.
Important points to be noted while using subscripts
Accessing individual elements in the array
Assigning values to the elements of an array (Using Assignment Operator - at design time)
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
marks[0] = 60;
marks[1] = 70;
marks[2] = 80;
marks[3] = 90;
marks[4] = 100;
getch();
}
Entering values to the elements of an array (at Runtime)
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
printf("Enter the values one by one: \n");
for(i=0; i<5;i++)
scanf("%d", &marks[i]);
getch();
}
In the above program, for loop causes the process of accepting student’s marks from the user to be repeated 5 times. The first time through the loop, i has a value 0, so the scanf() function will cause the value typed to be stored in the array element mark[0], the first element of the array. The second time through the loop, i has a value 1, so the scanf() function will cause the value typed to be stored in the array element mark[1], the second element of the array. This process will be repeated until i become 4. In scanf() function, the ‘address of’ operator (&) on the element marks[i] of the array for passing the address of this particular array element to the scanf() function.
Displaying values of the array elements after assigning
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
marks[0] = 60;
marks[1] = 70;
marks[2] = 80; /* assigning values to the array elements */
marks[3] = 90;
marks[4] = 100;
printf("\nElements in an array: ");
printf("\n%d”, marks[0]);
printf("\n%d”, marks[1]);
printf("\n%d”, marks[2]); /* displaying the values of the array elements */
printf("\n%d”, marks[3]);
printf("\n%d”, marks[4]);
getch();
}
Displaying values of the array elements using for loop
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
printf("Enter the values one by one: \n"); /* Entering values to the array elements */
for(i=0; i<5;i++)
scanf("%d", &marks[i]);
printf("\nElements in an array: \n"); /* displaying the values of the array elements */
for(i=0; i<5;i++)
printf("%d", marks[i]);
getch();
}
Consider the following program,
#include <stdio.h>
#include <conio.h>
main()
{
int x;
clrscr();
x=5;
x=10;
printf("x= %d", x);
getch();
}
No doubt, this program will print the value of x as 10. Why so? Because, when a value 10 is assigned to x, the earlier value of x, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value at a time.
Array
- Array is a special variable in C.
- A special variable (that is, array) is used for handling collection data of same type.
- C language provides the capability that enables the user to design a set of similar data types called arrays.
- An array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc.
- Arrays are defined in the same manner as ordinary variables except that each array name must be accompanied by a size specification within square braces.
- An array is defined as a set of homogeneous data items of the same type that share a common name and that are differentiated from one another by their positions within the array.
- An array is a collective name given to a group of similar quantities.
- The individual values in the array are called as elements.
- Each array element is referred by specifying the array name followed by one or more subscripts.
- The dimension of an array is determined by the number of subscripts needed to identify each element.
- A subscript is enclosed in square braces [], placed after the array name.
- Each subscript must be expressed as a non-negative integer.
- The value of each subscript can be expressed as an integer constant, integer variable, or a complex integer expression.
- Arrays are structured type of variables, for which, memory is allocated for all the elements declared by the size specification.
- The ability to use a simple name to represent a collection of items and to refer to an item number enables us to develop efficient programs.
Real-time example for an array
In real-time situations, need to store more than value at a time in a single variable. For example, suppose to arrange the percentage marks obtained by 100 students in ascending order. In such a case, have two options to store these marks in memory:
a) Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student’s marks.
b) Construct one variable capable of storing or holding all the hundred values.
Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables.
An array is a collective name given to a group of similar quantities. Note: the quantities must be ‘similar’. Each member in the group is referred to by its position in the group. For example, assume the following group of values, which represent percentage marks obtained by five students. per = {48, 88, 34, 23, 96} If need to refer to the second value of the group, the usual method used is per2, similarly, the fourth value of the group is referred as per4.
But in C, the counting of elements begins with 0 and not with 1. Thus, in this example per[3] refers to 23 and per[4] refers to 96.
In general, the notation would be per[i], where i can take a value 0, 1, 2, 3, or 4, depending on the position of the element being referred. Here, per is the subscripted variable, whereas i is its subscript.
Remember that all elements of any given array must be of the same type i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.
Properties of an array
- The type of an array is the data type of its elements.
- The location of an array is the location of its first element.
- The length of an array is the number of data elements in the array.
- The size of the array is the length of the array times the size of the elements.
Important points to be noted while using arrays
- An array name should be a valid C identifier.
- The name of the array should be unique, similar to other variables.
- The values of the elements stored in the array should be of the same type
Declaration of an array
Like ordinary variables, an array variable should also be declared. An array needs to be declared so that the compiler will know what kind of an array and how large an array we want.
The general form or syntax of declaring an array is datatype arrayName[subscript];
Where,
- arrayName is a valid C variable
- datatype specifies that the type of an array is the data type of its elements.
- subscript is an integer constant indicating maximum number of elements that can be store in the array.
- The bracket [ ] tells the compiler that we are dealing with an array.
For example, int marks[5];
Here,
- int specifies the type of the variable i.e. array
- The word marks specifies the name of the variable i.e. array
- The [5] is new. The number 5 tells how many elements of the type int will be in an array. This number is often called the ‘dimension’ of the array.
Dimensioning: The subscript used to declare an array is sometimes called a dimension and the declaration for the array is often called to as dimensioning.
Important points to be noted while using subscripts
- A subscript value should not be negative.
- A subscript must always be an integer or an expression that gives integer.
- The valid subscript value can be from 0 to n-1, if n is the dimension of the array.
- A subscript must be specified within the square brackets preceding the array name.
- If there is more than one subscript, each must be specified within separate square brackets.
Accessing individual elements in the array
- Once an array is declared, individual elements in the array are accessed with the help of subscripts (the number in the brackets following the array name)
- The subscript specifies the element’s position in the array.
- All the array elements are numbered starting from zero. Thus, in the example int marks[5]; that is, marks[3] is not the third element of the array, but refers to the fourth element of the array.
- In general, the notation would be marks[i], where i can take a value 0, 1, 2, 3, or 4, depending on the position of the element being referred. Here, marks is the subscripted variable, whereas i is its subscript which used to refer various elements of the array.
- The ability to use variables as subscripts makes arrays so useful in writing programs.
Assigning values to the elements of an array (Using Assignment Operator - at design time)
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
marks[0] = 60;
marks[1] = 70;
marks[2] = 80;
marks[3] = 90;
marks[4] = 100;
getch();
}
Entering values to the elements of an array (at Runtime)
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
printf("Enter the values one by one: \n");
for(i=0; i<5;i++)
scanf("%d", &marks[i]);
getch();
}
In the above program, for loop causes the process of accepting student’s marks from the user to be repeated 5 times. The first time through the loop, i has a value 0, so the scanf() function will cause the value typed to be stored in the array element mark[0], the first element of the array. The second time through the loop, i has a value 1, so the scanf() function will cause the value typed to be stored in the array element mark[1], the second element of the array. This process will be repeated until i become 4. In scanf() function, the ‘address of’ operator (&) on the element marks[i] of the array for passing the address of this particular array element to the scanf() function.
Displaying values of the array elements after assigning
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
marks[0] = 60;
marks[1] = 70;
marks[2] = 80; /* assigning values to the array elements */
marks[3] = 90;
marks[4] = 100;
printf("\nElements in an array: ");
printf("\n%d”, marks[0]);
printf("\n%d”, marks[1]);
printf("\n%d”, marks[2]); /* displaying the values of the array elements */
printf("\n%d”, marks[3]);
printf("\n%d”, marks[4]);
getch();
}
Displaying values of the array elements using for loop
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5];
clrscr();
printf("Enter the values one by one: \n"); /* Entering values to the array elements */
for(i=0; i<5;i++)
scanf("%d", &marks[i]);
printf("\nElements in an array: \n"); /* displaying the values of the array elements */
for(i=0; i<5;i++)
printf("%d", marks[i]);
getch();
}