Lab Engineer Dept A&A ARRAYS Collection of variables/elements of same type that are referred to through common name Placed in contiguous memory location Store multiple values in a single variable, instead of declaring separate variables for each value Individual elements can be referenced using an index to a unique identifier
Wednesday, March 27, 2024 Arrays and Strings 3
ONE DIMENSIONAL ARRAYS Syntax is datatype arrayName[arraySize]; For example: int a[5]; An array named a is declared which will store integer values and 5 values can be stored in it The array may be logically represented as follow, where each blank panel represent an element of the array
Wednesday, March 27, 2024 Arrays and Strings 4
ARRAYS INITIALIZATION By default, arrays are not initialized i.e., no particular values are assigned to elements Values can be assigned during declaration if required The syntax for assigning values int x[5] = { };
int y[5] = { 16, 2, 77, 40, 12071};
int z[5] = { 10, 20, 30};
Wednesday, March 27, 2024 Arrays and Strings 5
ARRAYS INITIALIZATION (Contd.)
Array size can be left blank
In that case, compiler will assume the array size that matches the number of elements included within { } int x[] = { 10, 20, 30}; The = sign is also optional in array initialization int x[] = { 10, 20, 30}; int x[] { 10, 20, 30};
Wednesday, March 27, 2024 Arrays and Strings 6
ARRAYS INITIALIZATION (Contd.)
Can be any data type
Suppose a class has 20 students and we need to store the percentages and grades of each student Instead of declaring 40 variables, we can declare only 2 arrays float percentage[20]; char grade[20]; Some other examples are string cars[4] = {“Volvo”, “BMW”, “Ford”, “Mazda”}; char vowels[ ] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};
Wednesday, March 27, 2024 Arrays and Strings 7
ACCESSING ELEMENTS OF AN ARRAY Each element of an array is associated with an index number Array index An element of an array can be accessed using syntax array[index]; For an array initialized as x[6]
Wednesday, March 27, 2024 Arrays and Strings 8
ACCESSING ELEMENTS OF AN ARRAY (Contd.) Array indices start with 0 x[0] is the first element stored in array x at index 0 For an array of size n, the last element is stores at index n-1 For array declared as x[6], the last element is accessed using x[5] Elements of an array have consecutive memory addresses The total memory size of an array depends on data type and number of elements
Wednesday, March 27, 2024 Arrays and Strings 9
STRINGS
Strings are collection of characters surrounded by double
quotes string g = “Hello” For using string, you must include <string> library #include <string> string g = "Hello"; Individual characters of a string can be accessed the same way as elements in arrays g is a string of length 5 g[0] g[1] g[2] g[3] g[4] H e l l o Wednesday, March 27, 2024 Arrays and Strings 10 STRINGS (Contd.)
Generally, strings do not include blank spaces
Wednesday, March 27, 2024 Arrays and Strings 11
STRINGS (Contd.)
To include blank spaces in a string, getline function is used
Syntax is getline (cin, variableName);
Wednesday, March 27, 2024 Arrays and Strings 12
OPERATIONS WITH ARRAYS
Wednesday, March 27, 2024 Arrays and Strings 13
ARRAYS EXAMPLES
Wednesday, March 27, 2024 Arrays and Strings 14
ARRAYS WITHIN LOOPS EXAMPLES
Wednesday, March 27, 2024 Arrays and Strings 15
ARRAYS WITHIN LOOPS EXAMPLES (Contd.)
Wednesday, March 27, 2024 Arrays and Strings 16
ARRAYS WITHIN LOOPS EXAMPLES (Contd.)
Wednesday, March 27, 2024 Arrays and Strings 17
STRING LENGTH/SIZE
Length of a string can be found using syntax
stringName.length() or stringName.size()
Wednesday, March 27, 2024 Arrays and Strings 18
ARRAY LENGTH/SIZE
Length of an array can be found using syntax
sizeof(arrayName) / sizeof(arrayName[i])
Wednesday, March 27, 2024 Arrays and Strings 19
STRINGS WITHIN LOOPS
Strings can be used within loops the same way as arrays are used