c++ Arrays

Arrays in c++

In this article, we will learn about the concept of the array in cpp.you will learn, how to declare, initialize,and access the elements of the array in c++ programming.


An array is a series of the element with the same data type placed continuously in the memory.


The concept of the array understands in this way that we need five variables with the same data type.

we declare five variable and use in the code, but CPP language gives to a programmer to use an array instead of using five different variables.



An array of size five works the same as five different variables but, it is easy to use and understand. There is the only difference in the array of size five and five variables is that variables place in different locations in the memory but the array placed five variables in a continuous manner in the memory.


How to declare an array?


The declaration is the same as the declaration of variables but with the given size of the array. Following is an array with name arr with size 5.


Int arr[size of array]

Int arr[5];


How to initialize the array in cpp?


By default, array elements are uninitialized. This means none of the elements of the array set to any particular value .we can initialize at the time of declaration of the array.

For example:


Int Ar[5]={10,20,30,40,50};


initialized values must be in the brackets{}. The above statement declares the array of the name Ar with the size of 5 and int data type.

Graphic representation:



In the initialization take care of values in brackets{} are not exceed from the size of the array. In the above example Ar, write 5 values in the brackets. These values are less than the size of the array but not greater than the size of the array. If values less than the size of the array, left elements of array to initialize with the default values, For example:


Int Ar[5]={10,20,30};





If initialize with no value then each element initializes with zero. for example


int Ar[5]={};




In the case, when the size of array is not written in the bracket[], then c++  automatically creates an array with a number of initialization values write in the brackets. for example:


Int Ar[]={10,30,40};


In the c++, there is a size of Ar is three because of three initialize values write in the curly brackets{}.


How to access the values of the array in c++?


Accessing the values :


Every value of array access like accessing the value of the variable. In the previous example use Ar array with 5 sizes.Actually,there are 5 variables named as Ar[0],Ar[1],Ar[2],Ar[3],and Ar[4].If we want to access the first index of the array then we shall use Ar[0]. The value of the index also changes like a regular variable.


Ar[0]=5;


The value of the index can also store in the variable.


int x=Ar[0];


After assigning the value to x, the value of x becomes 5.

Take care of the data type of array and variable during assigning the value to a variable . The data type of variable and array must be the same.

In the c++, there is no error in accessing the value exceed the limit of the array like Ar[6]. This error not come during the compilation but cause an error during the run time. The reason behind this error we shall learn on the topic of pointers.


For more understanding, following are the solved examples by using an array


Example1


#include <iostream> using namespace std; int main() { int arr[5] = {10, 20, 30, 40, 50}; cout << "The numbers are ="; // Printing the element of array for (int i = 0; i < 5; ++i) { cout << arr[i] << " "; } return 0; }

OUTPUT
The numbers are=10  20  30  40  50



Example2
Find the average of the five number

#include <iostream>
using namespace std;

int main() {
    int arr[5];
int ave=0;
cout<<" enter the 5 number for average ";
for (int i = 0; i < 5; ++i) {
        cin>>arr[i];
    }
    //for loop use  for adding all the values in array
    for (int i = 0; i < 5; ++i) {
        ave+=arr[i];
    }
//find the average by average formula
ave=ave/5;

cout<<" average is = "<<ave;

    return 0;
}

output
average is =36


Example 3:
Find the largest number in the array.

CODE:

#include <iostream>
using namespace std;

int main() {
    int arr[5];
int max=0;
cout<<" enter the 5 number  ";

//use for loop for taking input from the user

for (int i = 0; i < 5; ++i) {
        cin>>arr[i];
    }
    //for loop use  for adding all the values in array
for(int j=0;j<5;j++)
{
if(max<arr[j])
{
max=arr[j];
}
}
//display the max number 

cout<<" largest number is = "<<max;

    return 0;
}

OUTPUT:
enter the 5 number 12345
largest number is = 5.

In the above example, take an array of size 5 of int data type from the user. For loop used for finding a large number. In every alteration, if the condition assigns the number to the max variable if number is greater than the max value.

Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.

Comments

Post a Comment

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

How to format in HTML