Chapter 5 (Array and Strings)
Chapter 5 (Array and Strings)
Agenda
Introduction
Declaration and Initialization of Arrays
Accessing Elements of Arrays
Multidimensional Arrays
Arrays as parameters
Pointers and Arrays
Strings
The C-Style Character String
The string class
The String Class Operators
The string Class functions
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.
Bubble sort: if
elements out of order,
swap them.
Pointers and Arrays
The pointers and arrays have a very close relationship.
The name of an array is actually a pointer to the first element in the array.
For example,
int age[];
int *p;
p=&age;
p will point to the address of the first element of the array.
Writing age[3] tells the compiler to return the element that is 3 away
from the starting element of age.
Pointers are helpful where size of the array is unknown.
Declaring the array with a size of large value wastes lot of space.
Pointers improve the execution time.
Here is a program which illustrates the working of arrays and pointers
Pointers and Arrays, example
#include<iostream>
using namespace std;
void main() {
int age[5];
int *p;
int sum=0,i;
The pointer p points to the
char yes='y';
first element of the array
p=age;
for(i=0;i<5;i++) {
cout << "Enter the age of a student" << endl;
cin >> *p;
sum=sum+*p;
p++;
}
p=age;
cout << "The sum of the ages" << sum << endl;
cout << "The age of the last student is : " << *(p + 4) << endl;
}
Strings
A string is a series of characters.
The only strings you've seen until now have been unnamed string
constants used in cout statements, such as
cout << "hello world.\n";
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;