Arrays in C++ :

Array Definition :

An array is a one of the data structure in C++, that can store a fixed size sequential collection of elements of same data type. Arrays are of different types :

  • One-dimensional (Single Dimensional) arrays
  • Multi-dimensional (Two Dimensional) arrays

A two-dimensional array is the simplest of multidimensional arrays. However, C++ allows arrays of more than two dimension.


Single Dimensional (1-D) Arrays :

The simplest form of an array is a singel dimensional array. The array is given a name and its elements are referred to by their subscripts or indices. C++ arrays index numbering starts with 0. The general form of an array declaration is as shown below :

type array-name[size];

Example :

  1. /* Description: array use to print your age in C++ language */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int age[1]={23};
  7. int i,ayan;
  8. for(i=0;i<=1;i++)
  9. {
  10. ayan=age[i];
  11. }
  12. cout << "My age is - " <<ayan;
  13. }

Output :

C++ language array use to print your age

Two Dimensional (2-D) Arrays :

A two-dimensinal array is an array in which each element is itself an array. For instance, an array a[m][n] in an M by N table with M rows and N columns containing M x N elements.

To read or process a 2-D array, you need to use nested loops. one loop processes the rows and another the columns.

Example :

  1. /* Description: matrix sum */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int a[10][10],b[10][10],sum[10][10];
  7. int i,j,m,n;
  8. cout<<" Enter order of Matrix Row | Columns :";
  9. cin>>m>>n;
  10. cout<<"\n";
  11. for(i=1;i<=m;i++)
  12. {
  13. for(j=1;j<=n;j++)
  14. {
  15. cout<< " No. "<<i <<"\t Enter 1st matrix Values : \n";
  16. cin>>a[i][j];
  17. }
  18. }
  19. cout<<"\n";
  20. for(i=1;i<=m;i++)
  21. {
  22. for(j=1;j<=n;j++)
  23. {
  24. cout<< " No. "<<i <<"\t Enter 2nd matrix Values : \n";
  25. cin>>b[i][j];
  26. }
  27. }
  28. cout<<"\n Calculating...\n";
  29. for(i=1;i<=m;i++)
  30. {
  31. for(j=1;j<=n;j++)
  32. {
  33. sum[i][j]=a[i][j]+b[i][j];
  34. }
  35. }
  36. cout<<" Result :\n";
  37. for(i=1;i<=m;i++)
  38. {
  39. for(j=1;j<=n;j++)
  40. {
  41. cout<<sum[i][j]<<"\t";
  42. }
  43. cout<<"\n";
  44. }
  45. }

Output :

C++ language matrix sum

Array with Strings :

In C++, the one-dimensional array of characters are called strings, which is terminated by a null character ‘\0’.

Example :

  1. /* Description: simple array with string programming in C++ language */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. char name[5] = {'A', 'y', 'a', 'N', '\0'};
  7. cout << " My name is : " << name << endl;
  8. }

Output :

C++ language simple array with string programming

Next Update

Loading...

We Are Currently Working On Our Android Application Development and We already have two YouTube channels their name is CodeX or NightHawk. CodeX is for programming related videos and NightHawk is for online game streaming so please supporting us and Subscribe to our channels, Thank you