Documente Academic
Documente Profesional
Documente Cultură
Introduction
• Array
– Group of consecutive memory locations
– Same name and type
• To refer to an element, specify
– Array name
– Position number
• Format:
arrayname[ position number ]
– First element at position 0
– n element array named c:
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Array Declaration
• Like a regular variable, an array must be declared before it is
used.
• Syntax: type name [elements];
– type is a valid type (like int, float...),
– name is a valid identifier
– the elements field (which is always enclosed in square brackets []),
specifies how many of these elements the array has to contain.
• must be a constant value
– Example
• int myArray [5];
– Declaring multiple arrays of same type
• int b[ 100 ], x[ 27 ];
Initialization of Arrays
• Array declaration doesn’t give value to its elements
– Locally declared arrays (e.g. within a function)
• their content will be undetermined until we store some value in them.
– Global and static arrays are automatically initialized with their default
values,
• for all fundamental types they are filled with zeros.
• Two Options to initialize arrays
– when they are declare
int myArray[ 5 ] = {16, 2, 77, 40, 12071};
• If not enough initializerss, rightmost elements become 0
• All elements 0
int n[ 5 ] = { 0 }
• If too many - a syntax error is produced syntax error
Initialization of Arrays…
If size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array
X[0] = 12;
X[1] = 4;
X[2] = 5;
Accessing the values of an array.
We can access the value of any of arrays elements individually as
if it was a normal variable using the format: name[index]
E.g.
myArray[2]= 2;
to store the value 75 in the third element of my array
a = myArray[2];
to pass the value of the third element of billy to a variable called a
Some other valid operations with arrays:
myArray[0] = a; – b = myArray [a+2];
myArray[a] = 75; – myArray[myArray[a]] = myArray [2] + 5;
Example
The examples
#include<iostream.h>
show how to
#include<conio.h>
use arrays.
int myArray [] = {16, 2, 77, 40, 12071};
int n, result=0;
It adds contents
void main (){
of myArray
for ( n=0 ; n<5 ; n++ )
result += myArray[n];
cout << "Total = "<<result;
getch();
}
Multidimensional Arrays
Multidimensional arrays can be described as "arrays of arrays“
For example, a bidimensional array can be imagined as a
bidimensional table made of elements, all of them of a same
uniform data type.
string str(s);
All of these functions admit one parameter and return a value of the
requested type (int, long or float).
The String Class Operators
Assignment Operator (=): str = value;
Assigns a copy of value to str
value may be a string, char array, char
String concatenation Operator (+): str + value or value + str
returns the result of concatenating str and value
value may be a string, char array, char
Add to itself Operator (+=) : str += value;
Appends a copy of value to str
value may be a string, char array, char
Subscript Notation: str[p]
returns a reference to the character stored in str at position p
The String Class Operators…
Relational Operators – the comparison is alphabetically
str < s s may be a string or char array
str <= s s may be a string or char array
str > s s may be a string or char array
str >= s s may be a string or char array
str == s s may be a string or char array
str != s s may be a string or char array
The String Class Operators, Examples
Examples
String aa;
String bb("Bill Clinton");
aa = "put some value string"; // assignment operator
aa += "add some more"; // Add to itself and assign operator
aa = "My name is" + " Alavoor Vasudevan "; // string cat operator
if (bb == "Bill Clinton") // boolean equal to operator
cout << "bb is equal to 'Bill Clinton' " << endl;
if (bb != "Al Gore") // boolean 'not equal' to operator
cout << "bb is not equal to 'Al Gore'" << endl;