Programming in C: Unit II (a): Arrays

Introduction to Arrays in C Programming

We will explain the concept of arrays using an analogy. Take a situation in which we have 20 students in a class and we have been asked to write a program that reads and prints the marks of all these 20 students.

Unit II : Arrays and Strings

CHAPTER 5 : ARRAYS

Takeaways

• Array declaration

• Linear and binary search

• Operations on 2D arrays

• Application of arrays

• Array length

• Passing arrays to functions

• Multidimensional arrays

• Operations on arrays

• Two-dimensional arrays

• Array implementation of sparse matrices


INTRODUCTION TO ARRAYS

We will explain the concept of arrays using an analogy. Take a situation in which we have 20 students in a class and we have been asked to write a program that reads and prints the marks of all these 20 students. In this program, we will need 20 integer variables with different names, as shown in Figure 5.1.

Now to read values for these 20 different variables, we must have 20 read statements. Similarly, to print the value of these variables, we need 20 write statements.If it is just a matter of 20 variables, then it might be acceptable for the user to follow this approach. But would it be possible to follow this approach if we have to read and print marks of the students

• in the entire course (say 100 students)

• in the entire college (say 500 students)

• in the entire university (say 10000 students)

The answer is no, definitely not! To process large amount of data, we need a data structure known as array. An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript).

Programming Tip: In an array of size n, the index ranges from 0 to n−1.

Subscript is an ordinal number which is used to identify an element of the array. Some examples where the concept of an array can be used include:

• List of temperatures recorded on every day of the month

• List of employees in a company

• List of students in a class

• List of products sold

• List of customers

Programming in C: Unit II (a): Arrays : Tag: : - Introduction to Arrays in C Programming