What’s in an Array?
An array is a variable that can store multiple items of data unlike a regular variable that stores one pierce of data. Just like any variable, arrays must be declared before they can be accessed.
Initializing Arrays
You can initialize a simple array of built-in types, like integers (int) or characters (char), when you first declare the array. After the array name, put an equal sign and a list of comma separated values enclosed in the braces.
int nums[10] = { 0, 10, 20, 30, 40, 50, 60 ,70, 80, 90 };
This declared nums
to be an array of 10 integers. Remember that the data are stored sequentially in array are elements that are numbered starting at zero. So nums[0]
equals to 0, nums[1]
equals to 10, and so on.
Arrays can be created for any data type but each element may only contain data of the same data type.
Inserting and Printing Elements
int mike[5] = {19, 10, 8, 17, 9}// change 4th element to 9
mike[3] = 9;// take input from the user and insert in third element
cin >> mike[2];// take input from the user and insert in (i+1)th element
cin >> mike[i];// print first element of the array
cout << mike[0];// print ith element of the array
cout >> mike[i-1];
Character Arrays
An array of characters can be used to store a string of text if the final elements contains the special \0 null character. For example:
char name[5] = {'p', 'n', 'o', 'p', '\0'};
Because this character-by-character approach is difficult to type and admits too many opportunities for error, C++ enables a shorthand form of string initialization using a literal:
char name[] = "pnop";
This form of initialization doesn’t require the null
character; the compiler adds it automatically. The string “pnop” is 5 bytes including null.
Multidimensial Arrays
Collectively elements in an array is known as an index. Arrays can have more than one index. Arrays are suitable for data that consists of a known number of elements like a chessboard or coordinates which would be good examples in need of a two dimensional array.
For example:
int coordinates[2][3] = {{1,2,3} , {4,5,6}};
A three-dimensional array has three subscripts:
int cube[5,13,8];
Run the program below to see the output.
https://repl.it/join/hxlyjcst-thanhnguyen91
C-Style Character String
C++ supports a wide range of functions that can manipulate null-terminated strings. The header file <cstring> defines several functions to manipulate C strings and arrays.
╔═════════════════╦════════════════════════════════════════════╗
║ Keyword ║ Functions and Purpose ║
╠═════════════════╬════════════════════════════════════════════╣
║ strcpy(s1,s2) ║ copies string s2 into string s1. ║ ╠═════════════════╬════════════════════════════════════════════╣
║ strcat(s1,s2) ║ concatenates string s2 onto the end of ║
║ ║ string s1. ║
╠═════════════════╬════════════════════════════════════════════╣
║ strlen(s1) ║ Returns the length of string s1; ║ ╠═════════════════╬════════════════════════════════════════════╣
║ strcmp(s1,s2) ║ Returns 0 if s1 and s2 are the same; ║
║ ║ less than 0 if s1<s2; greater than 0 if ║ ║ ║ if s1>s2. ║
╠═════════════════╬════════════════════════════════════════════╣
║ strchr(s1,ch) ║ Returns a pointer to the first occurrence ║ ║ ║ of character ch in string s1. ║
╠═════════════════╬════════════════════════════════════════════╣
║ strstr(s1,s2) ║ Returns a pointer to the first string s2 ║
║ ║ in string s1. ║
╚═════════════════╩════════════════════════════════════════════╝
The header file <cstring> defines several functions to manipulate C strings and arrays.