Arrays in C

Array is a data type like int, float, char, double etc. Note that int, float, char, double data types can store only one item of data. But array can store set of values (data items). Array stores same kind of data. It is important to note that array stores similar kind of data.

Example: suppose we want to store roll numbers of all the students. Remember that roll number cannot be float. Roll numbers are only integer values. So, we can declare an array of integers. Marks obtained by students can be float (can have decimal values).

Array of integers   = Integer array
Array of floats     = float array
Array of characters = String
Set of similar data types are known as Array.

INTEGER ARRAY

Note: here x is the name of the array. Individual elements of the array are accessed using index (or subscript). In above program variable ‘i’ is used as index. Note that array index always starts from ‘zero — 0’. For above program individual elements are: x[0], x[1], x[2], x[3] and x[4], because the size of the array is 5.

x[0] = first element ……. first element always starts at index ZERO

x[1] = second element

x[2] = third element

x[3] = fourth element

x[4] = fifth element

Note that variables can be initialised directly in the program or can be given using keyboard. The below program illustrates inputting values from the keyboard. Here cin statement is used to give input from the keyboard. Note that cin is always associated with keyboard and cout is always used with printing on the screen.

FLOAT ARRAY

Note that float means some real number, which can contain decimal (fraction) value.

CHARACTER ARRAY

Remember that characters are initialised using single quotes. Suppose 8 is an integer data, but ‘8’ is a character data.

int num[6] = { 2, 4, 12, 5, 45, 5 } ; // array dimension is 6. Total array items = 6
int n[ ] = { 2, 4, 12, 5, 45, 5 } ;   // array dimension can be empty. It is allowed.
float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;

Note the following points carefully:

(a) Till the array elements are not given any specific values, they are supposed to contain garbage values.
(b) If the array is initialised where it is declared, mentioning the dimension of the array is optional as in the 2nd example above.

For example, suppose we wish to arrange the marks obtained by 100 students in ascending order. In such a case we have two options to store these marks in memory:

  • Construct 100 variables to store marks obtained by 100 different students, i.e. each variable containing one student’s marks.
  • Construct one variable (called array or subscripted variable) capable of storing 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’. These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or ages of 50 employees. What is important is that 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 numbers, which represent percentage marks obtained by five students.

per = { 48, 88, 34, 23, 96 }

Thus, an array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is called a ‘string, whereas an array of ints or floats is called simply an array.

A Simple Program Using Array

Q. Let us try to write a program to find average marks obtained by a class of 30 students in a test.

Entering Data into an Array

Here is the section of code that places data into an array:

for ( i = 0 ; i <= 29 ; i++ )
{
cout<< “\nEnter marks “;
cin>>marks[i] ) ;                           //store data in array
}

The for loop causes the process of asking for and receiving a student’s marks from the user to be repeated 30 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 marks[0], the first element of the array. This process will be repeated until i.

SUMMARY

  • An array is a collection of similar elements. The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
  • An array is also known as a subscripted variable.
  • Before using an array its type (i.e., int or float of char or double etc.) and dimension must be declared.
  • However big an array its elements are always stored in successive memory locations. This is a very important point.
  • An array is similar to an ordinary variable except that it can store multiple elements of similar type.
    In a 1-D array, zeroth element is a single value, whereas, in a 2-D array this element is a 1-D array.
  • On incrementing a pointer it points to the next location of its type.
  • Array elements are stored in contiguous memory locations and so they can be accessed using pointers.

What would be the output of the following programs

Q1.

Q2.

Q3.

Q4.

Q5.

Q6.

Q7.

Q8.

Q9.

Q10.

Fill in the blanks

  1.      “A” is a ___________ while ’A’ is a ____________.
  2.       A string is terminated by a ______ character, which is written as ______.
  3.       The array char name[10] can consist of a maximum of ______ characters.
  4.       The array elements are always stored in _________ memory locations.
  5.       In an array int arr[12] the word arr represents the a_________ of the array

Answer the following

(a) An array is a collection of

1. Different data types scattered throughout memory
2. The same data type scattered throughout memory
3. The same data type placed next to each other in memory
4. Different data types placed next to each other in memory

(b) What is the difference between the 5’s in these two expressions? (Select the correct answer)

int num[5] ;
num[5] = 11 ;

1.      First is particular element, second is type

2.      First is array size, second is particular element

3.      First is particular element, second is array size

4.      Both specify array size

State whether the following statements are True or False

1. The array int num[26] has twenty-six elements.
2. The expression num[1] designates the first element in the array
3. It is necessary to initialize the array at the time of declaration.
4. The expression num[27] designates the twenty-eighth element in the array.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »