c++ array

c++ array

c++ array

c++ array

 

In daily life we normally group related items into units. We buy eggs by carton. In computer languages we also need to group organized data items of similar nature. Most basic mechanism that achieves this in C++ is array . in Arrays data items may be few or thousands .  Any type of items can be defined in array like int, float ,char and Objects. Array exist almost in every computer language . Arrays in C++ are alike to those in other computer programming languages.

In this tutorials we are going to learn about C++ array.

c++ array

basic of array

In this tutorials we are going to learn about Array in C++.

Array Fundamentals

A very simple example program will help to familiarize arrays . This program , creates an array of 6 integers demonstrating  age of sex peoples . It then asks user to enter four values , which will be place in array  and after that  It will display all sex values.

 

#include <iostream>

int main(){

int  age[6];                                                                //defining array

for( int x = 0 ; x<6 ; x++) {                                       //getting values

cout<<”Enter An Age ”;

cin>>age[x];                                                              //accessing array elements

}

for ( x = 0 ; x<4 ; x++)

<<”You entered ”<<age[x]<<endl;

return  0;

}

 

 

First for loop gets ages from user and places them in array , while second reads them from array and displays them .

 

Its Explanation

Now understand above Program cod step by step.

 

c++ array

c++ array

Defining Arrays

 

An array must be defined before it can be used to store information  like other variable.  An array definition specifies a variable type and a name . but it includes another feature a size .  Size states how many data items array will hold. Instantly follows name, and is enclosed by square braces .

In above example , the array is type int . the name of array comes next , followed instantly by an opening bracket, array size and a closing brackets.  Number in brackets must be a constant, and should also be an integer

Array elements

The items in an array are called elements of array , which are also called members. In example code array has sex elements, first item in array has 0 index. Thus, since there are 6 elements in array , last is number 5.

How to access array elements

In example code we access each array elements double. first time , we insert a value into array , with line

cin >> age[x];

second time , we read it out with line

cout<<” You entered”<<age[x];

in both cases expression for array element is

age[j];

this consist of name of array, followed by brackets delimiting a variable x . which of sex array elements is identified by this expression depends on value of x ; age[0]

refers to first element, age[1 ] to second .,age[2] to third , and age[3] to fourth and so on . The variable in brackets[] is called index of array.

 

Since j is loop variable in both for loops , it starts at 0 and is incremented until it reaches 5, thereby accessing each of array elements in turn.

Initializing Arrays

You can give values to each array elements when array is first defined . here an example , days that sets 12 arrays elements in array day_per_month to number of days in each month .

#include<iostream>

int main () {

int month, day  , total-days;

int day-per-month[12] = {31,26,31,30,31,30,31,31,30,31,30,31};

cout<<”Enter month (1  to 12):” ;

cin>>month;

cout<<”Enter day (1 to 31) :” ;

cin>>day;

total-days = day;

for(int x = 0; j<month-1; x++)

total-days += days_per_month[x];

cout<<”Total days from start of year is : ” << total-days

<<endl;

Return 0;

}

 

 

Above program Logic:

Above program calculates number of days from beginning of year to a date specified by user.

Once it gets month   and day values , program first assign day value to total_days variable. Then it cycles through a a loop , where it adds values from days_per_month array to total_days. The number of such values to add is noe less than number of months. For example if user enters month 5 , values of first four array elements (31,28,31, and 30) are added to total.  Values to which days_per_month is initialized are surrounded by braces and seprated by commas.

Main point in above program  is we don’t define array size and we directly initialize all array elements, since compiler can figure it out by counting  initializing variable .

Summary :

in this tutorials C++ array , you have learned what is Array in C++ , how to define c++ array & how to access its elements . If you are confuse or want to know something more about C++ array , please let me know in comments .

Also Check this tutorial

struct in c++

Leave a Reply

Your email address will not be published. Required fields are marked *