C++ Arrays

Arrays are a way of storing the number of items at continuous memory locations. Array can store items of simple type int, float etc. It can also store items in user-defined types such as structure and objects. Array can store values of only one data types.

We can use normal variables, but if the number of instances is more, then we cannot use normal variables, so we use array to store large number of elements. The idea of ​​an array is to represent multiple instances in a variable.

Single Dimensional Arrays

Single dimensional arrays are very easy to understand. The index of an array in C++ starts with 0. The array name and the elements are referenced by the subscript or index. The general declaration of the array is given below:

Syntax of Single Dimensional Arrays

type array-name[size];


Where type declares the data type of an array and it will be the data type of each element in an array. The array-name specifies the name of the array and the size defines the number of items the array can hold. We can declare an array by specifying its type and size, or initializing it, or both.

Declare The Array And Specify The Size

   // Array declaration by specifying size
   int a[5];

   // With recent C++ versions, we can also
   // declare an array of user specified size
   int i = 5;
   int a[i];

Array declaration by initializing the elements

    // Array declaration by initializing the elements
   int a[] = { 1, 2, 3, 4 }

   // The compiler creates an array of size 4.
   // above is same as "int a[4] = {1, 2, 3, 4}"

Declare an array by specifying the size and initializing the elements

   // Array declaration by specifying size and initializing
   // elements
   int a[6] = { 1, 2, 3, 4 }

   // Compiler creates an array of size 6, initializes first
   // 4 elements as specified by user and rest two elements as 0.
   // above is same as "int a[] = {1, 2, 3, 4, 0, 0}"

Example Of Single Dimensional Arrays:

#include <iostream>
using namespace std; 
int main()
{ 
int arr[5], i;
for(i = 0; i < 5;i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
printf("\nPrinting elements of the array: \n\n");
for(i = 0; i < 5; i++)
{  
printf("%d ", arr[i]);
}
// signal to operating system program ran fine
return 0;
} 


Output :
Enter a[0]: 11
Enter a[1]: 22
Enter a[2]: 34
Enter a[3]: 4
Enter a[4]: 34
Printing elements of the array:
11   22   34   4   34

In the above example we have declared an array a[] of size 5 and data type int and also one variable i of integer type. Then we take input from user and store it in array and then we print the array elements on the screen.

Two Dimensional Arrays

A two dimensional array is an array of arrays. For instance an array A[M][N] in which M is number of rows and N is number of columns and total number of element in an array is M*N. For example an array A[5][5] containing the number of rows = 5 and the number of columns = 5 and the total number of elements is 25.

Syntax of Two Dimensional Arrays

    type array-name[rows][columns];

Example Two Dimensional Arrays:

#include <iostream>
using namespace std; 
int main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){ 
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);   
}//end of j    
}//end of i
return 0;
} 


Output :
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6


Exercise:-

1. A one-dimensional array of one-dimensional arrays is called

A. Multi-dimensional array
B. Multi-casting array
C. Two-dimensional array
D. Three-dimensional array

View Answer


2. Which of the following correctly declares an array?

A. int array[10];
B. int array;
C. array{10};
D. array array[10];

View Answer



Program

Calculate Standard Deviation by Passing it to Function

#include <iostream>
#include <cmath>
using namespace std;
float calculateSD(float data[]);
int main()
{
int i;
float data[10];
cout << "Enter 10 elements: ";
for(i = 0; i < 10; ++i)
cin >> data[i];
cout << endl << "Standard Deviation = " << calculateSD(data);
return 0;
}
float calculateSD(float data[])
{
float sum = 0.0, mean, standardDeviation = 0.0;
int i;
for(i = 0; i < 10; ++i)
{
sum += data[i];
}
mean = sum/10;
for(i = 0; i < 10; ++i)
standardDeviation += pow(data[i] - mean, 2);
return sqrt(standardDeviation / 10);
}


Output:
Enter 10 elements: 1
2
3
4
5
6
7
8
9
10
Standard Deviation = 2.872281





Visit :


Discussion



* You must be logged in to add comment.