Array in C++ Programming Lecture
Array in C++ Programming Lecture
LECTURE # 8: ARRAY - A
BSE 1
Joddat Fatima
1
joddat.fatima@gmail.com
Department of C&SE
Bahria University Islamabad
BASICS
2
INTRODUCTION
Array
Consecutivegroup of memory locations
Same name and type (int, char, etc.)
To refer to an element
Specify array name and position number (index)
Format: arrayname[ position number ]
First element at position 0
Example:
4
INITIALIZING ARRAYS
For loop
Set each element
Initializer list
Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements 0
If too many syntax error
ARRAY [] USAGE
In an array declaration, [ ]'s enclose the size of the array
When referring to one of the indexed variables, the [ ]'s enclose
a number identifying one of the indexed variables
The value in the [ ]'s can be any expression that evaluates to one
6
of the integers
VARIABLES & DECLARATION
Example:
cout << "Enter number of students: ";
cin >> number;
int score[number];
Problems
Array declared as int a[6]; int i=7; executing the statement a[i] = 238;
causes…
The computer to calculate the address of the illegal a[7]
(This address could be where some other variable is stored)
The value 238 is stored at the address calculated for a[7] 8
No warning is given!
EXAMPLE-1
9
EXAMPLE-2
#include <iostream>
int main() OUTPUT
32
{ 27
// use initializer list to initialize array n 64
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 18
95
for ( int i = 0; i < 10; i++ )
14
cout << n[ i ] << endl; 90
return 0; 70
60
}
37
10
ARRAY SIZE & EXAMPLE
Array size
Can be specified with constant variable (const)
const int size = 20;
Example OUTPUT
#include <iostream> 2
int main() 4
{ 6
8
const int arraySize = 10;
10
int s[ arraySize ]; 12
for ( int i = 0; i < arraySize; i++ ) 14
s[ i ] = 2 + 2 * i; 16
18
for ( int j = 0; j < arraySize; j++ )
20 11
cout << << s[ j ] << endl;
return 0;}
EXAMPLE
#include <iostream>
int main()
{
const int arrSize = 10;
int a[ arrSize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
for ( int i = 0; i < arrSize; i++ )
total += a[ i ];
cout << "Total of array element values is " << total <<
endl;
return 0;
12
} Total of array element values is 55
Element Value Histogram
0 19 *******************
1 3 ***
EXAMPLE 2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
#include <iostream> 6 13 *************
#include <iomanip> 7 5 *****
int main() 8 17 *****************
{ 9 1 *
Strings
Arrays of characters
All strings end with null ('\0')
Examples
char string1[] = "hello";
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
14
STRING ARRAYS
Input from keyboard
char string2[ 10 ];
cin >> string2;
Puts user input in string
Stops at first whitespace character
Printing strings
cout << string2 << endl;
Does not work for other array types
15
STRING ARRAYS EXAMPLE
Enter the string "hello there": hello there
string1 is: hello
#include <iostream>
string2 is: string literal
int main()
string1 with spaces between characters is:
{ hello
char string1[ 20 ];
char string2[] = "string literal";
17
PASSING ARRAYS TO FUNCTIONS
18
PASSING ARRAYS TO FUNCTIONS
Arrays passed-by-reference
Functions can modify original array data
Value of name of array is address of first element
Function knows where the array is stored
Can change original memory locations
19
PASSING ARRAYS TO FUNCTIONS
20
EXAMPLE
#include <iostream>
void modifyArray( int [], int );
void modifyElement( int );
int main()
{
const int arraySize = 5;
int a[ arraySize ] = { 0, 1, 2, 3, 4 };
cout << "Effects of passing entire array by reference:" << "\n\nThe values of the original array are:\n";
for ( int i = 0; i < arraySize; i++ )
cout << a[ i ];
cout << endl;
modifyArray( a, arraySize );
cout << "The values of the modified array are:\n";
for ( int j = 0; j < arraySize; j++ )
cout << a[ j ];
cout << "\n\n\n"<< "Effects of passing array element by value:"<< "\n\nThe value of a[3] is " << a[ 3 ]
<< '\n';
modifyElement( a[ 3 ] );
cout << "The value of a[3] is " << a[ 3 ] << endl;
return 0; 21
}
EXAMPLE CONST..
void modifyArray( int b[], int sizeOfArray ) Effects of passing entire array by reference:
{
for ( int k = 0; k < sizeOfArray; k++ ) The values of the original array are:
b[ k ] *= 2; 0 1 2 3 4
} The values of the modified array are:
0 2 4 6 8
void modifyElement( int e )
{
cout << "Value in modifyElement is “ Effects of passing array element by value:
<< ( e *= 2 ) << endl;
} The value of a[3] is 6
Value in modifyElement is 12
The value of a[3] is 6
22