What is an Array in C Language?

By
Last updated:

 What is an array?

An array is defined as an ordered set of similar data items. All the data items of an array are stored in consecutive memory locations in RAM. The elements of an array are of same data type and each item can be accessed using the same name.

How to declare and initialize arrays?

  • We know that all the variables are declared before they are used in the program.
  • Similarly, an array must be declared before it is used.
  • During declaration, the size of the array has to be specified.
  • The size used during declaration of the array informs the compiler to allocate and reserve the specified memory locations.

Syntax:- data_type array_name[n] where, n is the number of data items (or) index(or) dimension.
0 to (n-1) is the range of array.
Ex: int a[5];
float x[10];

Initialization of Arrays

The different types of initializing arrays:

  • At Compile time
    • Initializing all specified memory locations.
    • Partial array initialization
    • Initialization without size.
    • String initialization.
  • At Run Time
    • We can initialize the elements of arrays in the same way as the ordinary variables when they are declared.
    • The general form of initialization of arrays is : type array-name[size]={ list of values};

Initializing all specified memory locations

  • Arrays can be initialized at the time of declaration when their initial values are known in advance.
  • Array elements can be initialized with data items of type int, char etc.
  • Ex:- int a[5]={10,15,1,3,20};
  • During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and all these locations are initialized as shown in figure.
  • int a[3]={9,2,4,5,6};
  • //error: no. of initial vales are more than the size of array.

Partial array initialization

  • Partial array initialization is possible in c language.
  • If the number of values to be initialized is less than the size of the array, then the elements will be initialized to zero automatically.
  • Ex:- int a[5]={10,15};
  • Eventhough compiler allocates 5 memory locations, using this declaration statement;
  • the compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically initialized to 0’s by compiler as shown in figure.

Initialization without size

  • Consider the declaration along with the initialization.
  • Ex:- char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
  • In this declaration, even though we have not specified exact number of elements to be used in array b, the array size will be set of the total number of initial values specified.
  • So, the array size will be set to 8 automatically. The array b is initialized as shown in figure.
  • Array initialization with a string:
  • -Consider the declaration with string initialization.
  • Ex:- char b[]=”COMPUTER”;
  • The array b is initialized as shown in figure.

computer

  • Eventhough the string “COMPUTER” contains 8 characters, because it is a string, it always ends with null character.
  • So, the array size is 9 bytes (string length 1 byte for null character).
  • Ex:- char b[9]=”COMPUTER”; // correct
  • char b[8]=”COMPUTER”; // wrong

Run Time Initialization

  • An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays.
  • Ex:- scanf can be used to initialize an array.
  • int x[3];
  • scanf(“%d%d%d”,&x[0],&x[1],&x[2]);

How can we declare and initialize 2D arrays?

  • An array consisting of two subscripts is known as two-dimensional array.
  • These are often known as array of the array. In two dimensional arrays the array is divided into rows and columns.
  • These are well suited to handle a table of data.
  • In 2-D array we can declare an array as :
  • Declaration:-
  • Syntax: data_type array_name[row_size][column_size];
  • Ex:- int arr[3][3];
  • where first index value shows the number of the rows and second index value shows the number of the columns in the array.
  • Initializing two-dimensional arrays: Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.
  • Ex: int a[2][3]={0,0,0,1,1,1};
  • initializes the elements of the first row to zero and the second row to one.
  • The initialization is done row by row.
  • We can also initialize a two-dimensional array in the form of a matrix as shown below int a[2][3]={ {0,0,0}, {1,1,1} };
  • When the array is completely initialized with all values, explicitly we need not specify the size of the first dimension.
  • Ex: int a[][3]={ {0,2,3}, {2,1,2} };
  • If the values are missing in an initializer, they are automatically set to zero.
  • Ex: int a[2][3]={ {1,1}, {2} };
  • Will initialize the first two elements of the first row to one, the first element of the second row to two and all other elements to zero.

how two dimensional arrays can be used to represent matrices

  • These are stored in the memory as given below.
  • Row-Major order Implementation
  • Column-Major order Implementation
  • In Row-Major Implementation of the arrays, the arrays are stored in the memory in terms of the row design, i.e. first the first row of the array is stored in the memory then second and so on.
  • Suppose we have an array named arr having 3 rows and 3 columns then it can be stored in the memory in the following manner.
  • Thus an array of 3*3 can be declared as follows
  • arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  • and it will be represented in the memory with row major implementation as follows
1 2 3 4 5 6 7 8 9
    • In Column Major Implementation of the arrays, the arrays are stored in the memory in the term of the column design,
    • i.e. the first column of the array is stored in the memory then the second and so on.
    • By taking above eg. we can show it as follows
  • arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  • and it will be represented in the memory with column major implementation as follows :
1 4 7 2 5 8 3 6 9

Two dimensional arrays of variable length

  • An array consisting of two subscripts is known as two dimensional array. These are often known as array of the array.
  • In two dimensional arrays the array is divided into rows and columns,. These are well suited to handle the table of data.
  • In 2-D array we can declare an array as :
  • Declaration:- Syntax: data_type array_name[row_size][column_size]; Ex: int arr[3][3] ;
  • Where first index value shows the number of the rows and second index value shows the no. of the columns in the array.
  • These are stored in the memory as given below.

Initialization :- To initialize values for variable length arrays we can use scanf statement and loop constructs.

  • for (i=0 ; i<3; i++)
  • for(j=0;j<3;j++)
  • scanf(“%d”,&arr[i][j]);

Define multi dimensional arrays

Multidimensional arrays are often known as array of the arrays. In multidimensional arrays the array is divided into rows and columns, mainly while considering multidimensional arrays we will be discussing mainly about two dimensional arrays and a bit about three dimensional arrays.

  • Syntax: data_type array_name[size1][size2][size3]——[sizeN];
  • In 2-D array we can declare an array as :
  • int arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  • where first index value shows the number of the rows and second index value shows the number of the columns in the array.
  • To access the various elements in 2-D array we can use:
  • printf(“%d”, a[2][3]); /* output will be 6,
  • as a[2][3] means third element of the second row of the array */
  • int arr[3][3][3] = { 1, 2, 3,4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 };
  • /* here we have divided array into grid for sake of convenience as in above declaration we have created 3 different grids, each have rows and columns */
  • If we want to access the element the in 3-D array we can do it as follows :
  • printf(“%d”,a[2][2][2]);
  • /*
  • its output will be 26, as a[2][2][2] means first value in [] corresponds to the grid no. i.e. 3 and the second value in [] means third row in the corresponding grid and last [] means third column */
  • Ex:- int arr[3][5][12]; float table[5][4][5][3]; arr is 3D array declared to contain 180 (3*5*12) int type elements. Similarly table is a 4D array containing 300 elements of float type.

C Program to find out smallest element in an array

#include<stdio.h>
void main()
{
int a[30],i,n,smallest;
printf("n Enter no of elements :");
scanf("%d",&n);
for(i=0 ; i < n ; i++)                   /* read n elements in an array */
scanf("%d",&a[i]);
smallest = a[0];
for(i = 0 ;i < n ; i++)
{   if ( a[i] < smallest )
smallest = a[i];
}
                                         /* Print out the Result */
printf("nSmallest Element : %d",smallest);
}
#include<stdio.h>
int main()
{
int arr[30],element,num,i,location; printf("n Enter no of elements :"); scanf("%d",&num);
for(i=0 ; i < num ; i++)
scanf("%d",&arr[i]);
printf("n Enter the element to be inserted :");
scanf("%d",&element); printf("n Enter the location"); scanf("%d",&location);
/* create space at the specified location */
for(i = num ;i >= location ; i--)
arr[i] = arr[i-1];
num++;
arr[location-1] = element;
/* Print out the Result of Insertion */
for(i = 0 ;i < num ;i++)
printf("n %d",arr[i]);
return(0);
}

Programming Innovations

In the old days, 20 or so years ago, programmers starting a project would sit down almost immediately and start writing code. However, as programming projects became large and more complicated, it was found that this approach did not work very well. The problem was complexity.

Large programs are probably the most complicated entities ever created by humans. Because of this complexity, programs are prone to error, and software errors can be expensive and even life threatening (in air traffic control, for example). Three major innovations in programming have been devised to cope with the problem of complexity. They are

Object-oriented programming (OOP)
• The Unified Modeling Language (UML)
• Improved software development processes

This book teaches the C++ language with these developments in mind. You will not only learn a computer language, but new ways of conceptualizing software development.

Object Oriented Programming

Why has object-oriented programming become the preferred approach for most software projects? OOP offers a new and powerful way to cope with complexity. Instead of viewing a program as a series of steps to be carried out, it views it as a group of objects that have certain properties and can take certain actions. This may sound obscure until you learn more about it, but it results in programs that are clearer, more reliable, and more easily maintained.

A major goal of this book is to teach object-oriented programming. We introduce it as early as possible, and cover all its major features. The majority of our example programs are object oriented.

The Unified Modeling Language

The Unified Modeling Language (UML) is a graphical language consisting of many kinds of diagrams. It helps program analysts figure out what a program should do, and helps program mers design and understand how a program works. The UML is a powerful tool that can make programming easier and more effective.

We give an overview of the UML, and then discuss specific features of the UML throughout the book. We introduce each UML feature where it will help to clarify the OOP topic being discussed. In this way you learn the UML painlessly at the same time the UML helps you to learn C++.

Languages and Development Platforms

Of the object oriented programming languages, C++ is by far the most widely used. Java, a recent addition to the field of OO languages, lacks certain features such as pointers, tem plates, and multiple inheritance that make it less powerful and versatile than C++. (If you ever do want to learn Java, its syntax is very similar to that of C++, so learning C++ gives you a head start in Java.)

Several other OO languages have been introduced recently, such as C#, but they have not yet attained the wide acceptance of C++.

Until recently the standards for C++ were in a constant state of evolution. This meant that each compiler vendor handled certain details differently. However, in November 1997, the ANSI/ISO C++ standards committee approved the final draft of what is now known as Standard C++. (ANSI stands for American National Standards Institute, and ISO stands for International Standards Institute.) Standard C++ adds many new features to the language, such as the Standard Template Library (STL). In this book we follow Standard C++ (in all but a few places, which we’ll note as we go along).

The most popular development environments for C++ are manufactured by Microsoft and Borland (Inprise) and run on the various flavors of Microsoft Windows. In this book we’ve attempted to ensure that all sample programs run on the current versions of both Borland and Microsoft compilers. (See Appendix C, “Microsoft Visual C++,” and Appendix D, “Borland C++Builder,” for more on these compilers.)

Read More Topics
Virtual function in C++
Variables in C
Nesting of Function in C
Function Declaration in C

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment