Loading....
Coupon Accepted Successfully!

 

Characteristics Of Arrays

  1. The Declaration int a[5] is nothing but a creation of five variables of integer types. Instead of declaring five variables for five values, the programmer can define them in an array.
  2. All the elements of an array share the same name, and they are distinguished from one another with the help of an element number.
  3. The element number in an array plays a major role for calling each element.
  4. Any particular element of an array can be modified separately without disturbing the other elements.
     
    For example, int a[5] = {1,2,3,4,8};
     
     If the programmer needs to replace 8 with 10, he/she is not required to change all the other elements except 8. To carry out this task, the statement a[4] = 10 can be used. Here, the other four elements are left unchanged.
  5. Any element of an array a[] can be assigned/equated to another ordinary variable or an array variable of its type.
     
    1. In the statement b = a[2] or vice versa, the value of a[2] is assigned to ‘b’, where ‘b’ is an integer.
    2. In the statement a[2] = a[3] or vice versa, the value of a[3] is assigned to a[2], where both the elements are of the same data type.
  6. The array elements are stored in contiguous memory locations. The amount of storage required for holding the elements of the array depends on its type and size. The total size in bytes for a single-dimensional array is computed as shown below.

Total bytes = size of (data type) × size of array


The rules for array declaration are similar in both C and C
++. Simple programs on arrays are demonstrated with C and C++.
 

12.1 Program executed in C

12.2 Program executed in C++

#include<stdio.h>

#include<conio.h>

void main()

{

char text[3] = “xyz”;

char txt[3] = “abc”;

clrscr();

printf (“\n %s”,text);

}

#include<conio.h>

#include<iostream.h>

void main()

{

char text[3] = “xyz”;

char txt[3] = “abc”;

clrscr();

cout<<text;

}

OUTPUT
xyz_abcW__

OUTPUT
XyzÅ

 
Explanation: Both the above programs are similar except for the syntax. The first and second programs are executed in C (Ver 2.0) and C++ (ver 3.0), respectively.. The character arrays are declared to be exactly equal to the length of the string. The null character is not taken into account. That is why in C the printf() statement displays the contents of the next character array followed by the character array. In C++, the cout also displays the garbage. It is better to take the null character into account while declaring and initializing the string array. Thus, the above character array can be accurately declared as follows:
 

(a) char text[4] = “xyz”;

(b) char txt[4] = “abc”;


Now, you will get the correct output.

 

Given below are a few programs on one-dimensional arrays
 

12.3 Program to read 5 integers through keyboard and display them.

#include<iostream.h>

#include<conio.h>

int main()

{

int num[5],i;

clrscr();

for(i=0;i<5;i++)

{

cout<<“\n Enter any no:”;

cin>>num[i];

}

cout<<“ Numbers are:”;

for(i=0;i<5;i++)

{

cout<<num[i];

}

return 0;

}

OUTPUT

Enter any no:12

Enter any no:34

Enter any no:45

Enter any no:67

Enter any no:89

Numbers are:12 34 45 67 89

 
12.4 Program to enter 5 numbers through keyboard and display them in ascending order.
 
OUTPUT
Enter 5 numbers:23 56 78 99 2
Numbers in ascending order are as follows: 2 23 56 78 99

Explanation:
 For arranging numbers in the ascending order, the numbers are sorted with two for loops, and the same sorted numbers are displayed.





Test Your Skills Now!
Take a Quiz now
Reviewer Name