Creation and use of arrays in C++<\/span><\/p>\n C++ provides a data structure,\u00a0the array<\/strong>, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.<\/p>\n Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.<\/p>\n All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.<\/p>\n To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:<\/p>\n This is called a single-dimension array. The\u00a0arraySize<\/strong>\u00a0must be an integer constant greater than zero and\u00a0type<\/strong>\u00a0can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:<\/p>\n You can initialize C++ array elements either one by one or using a single statement as follows:<\/p>\n The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array:<\/p>\n If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:<\/p>\n You will create exactly the same array as you did in the previous example.<\/p>\n The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above:<\/p>\n Credits:<\/span><\/p>\n <\/a>http:\/ <\/a>http:\/ \u00a0<\/p>\n #TC<\/a>1017 #Mastery<\/a>24<\/span><\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" Creation and use of arrays in C++<\/span><\/p>\n C++ provides a data structure, the array<\/strong>, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.<\/p>\n Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.<\/p>\n All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.<\/p>\n To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:<\/p>\n This is called a single-dimension array. The arraySize<\/strong> must be an integer constant greater than zero and type<\/strong> can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:<\/p>\n You can initialize C++ array elements either one by one or using a single statement as follows:<\/p>\n The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array:<\/p>\n If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:<\/p>\n You will create exactly the same array as you did in the previous example.<\/p>\n The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above:<\/p>\n<\/p>\n Credits:<\/span><\/p>\n <\/a>http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_arrays.htm<\/a><\/span><\/p>\n <\/a>http:\/\/www.cplusplus.com\/doc\/tutorial\/arrays\/<\/a><\/span><\/p>\n <\/p>\n #TC<\/a>1017 #Mastery<\/a>24<\/span><\/span><\/p>\n<\/div>\nDeclaring Arrays:<\/h2>\n
#d6d6d6;\">type arrayName <\/span>[<\/span> arraySize <\/span>];<\/span><\/pre>\n
#d6d6d6;\">double<\/span> balance<\/span>[<\/span>10<\/span>];<\/span><\/pre>\n
Initializing Arrays:<\/h2>\n
#d6d6d6;\">double<\/span> balance<\/span>[<\/span>5<\/span>]<\/span> =<\/span> {<\/span>1000.0<\/span>,<\/span> 2.0<\/span>,<\/span> 3.4<\/span>,<\/span> 17.0<\/span>,<\/span> 50.0<\/span>};<\/span><\/pre>\n
#d6d6d6;\">double<\/span> balance<\/span>[]<\/span> =<\/span> {<\/span>1000.0<\/span>,<\/span> 2.0<\/span>,<\/span> 3.4<\/span>,<\/span> 17.0<\/span>,<\/span> 50.0<\/span>};<\/span><\/pre>\n
#d6d6d6;\">balance<\/span>[<\/span>4<\/span>]<\/span> =<\/span> 50.0<\/span>;<\/span><\/pre>\n
Declaring Arrays:<\/h2>\n
#d6d6d6;\">type arrayName <\/span>[<\/span> arraySize <\/span>];<\/span><\/pre>\n
#d6d6d6;\">double<\/span> balance<\/span>[<\/span>10<\/span>];<\/span><\/pre>\n
Initializing Arrays:<\/h2>\n
#d6d6d6;\">double<\/span> balance<\/span>[<\/span>5<\/span>]<\/span> =<\/span> {<\/span>1000.0<\/span>,<\/span> 2.0<\/span>,<\/span> 3.4<\/span>,<\/span> 17.0<\/span>,<\/span> 50.0<\/span>};<\/span><\/pre>\n
#d6d6d6;\">double<\/span> balance<\/span>[]<\/span> =<\/span> {<\/span>1000.0<\/span>,<\/span> 2.0<\/span>,<\/span> 3.4<\/span>,<\/span> 17.0<\/span>,<\/span> 50.0<\/span>};<\/span><\/pre>\n
#d6d6d6;\">balance<\/span>[<\/span>4<\/span>]<\/span> =<\/span> 50.0<\/span>;<\/span><\/pre>\n