Computer Programming by C++
Arrays and Strings
M A H K A M E S H A R B AT D A R
FA L L 1 4 0 3
Why it is Necessary to Use Arrays?
➢ If we wanted to read a list of 1000 values and print them in int main()
{
reverse order, we could write a program of this form int value0;
int value1;
int value2;
.
.
int main() .
int value999;
{
cin >> value0;
int value[1000]; cin >> value1;
int number; cin >> value2;
.
.
.
for (number = 0; number < 1000; number++) cin >> value999;
cin >> value[number];
cout << value999 << endl;
for (number = 999; number >= 0; number--) cout << value998 << endl;
cout << value997 << endl;
cout << value[number] << endl; .
return 0; .
.
} cout << value0 << endl;
return 0;
}
2
Why it is Necessary to Use Arrays?
•So far we have used variables to store values in memory for later reuse. We now explore a means to store
multiple values together as one unit, the array.
•An array is a fixed number of elements of the same type stored sequentially in memory.
•The size of the array is referred to as its dimension.
•Each element of the array can be individually referenced by adding an index to a unique identifier.
int arr [4];
float angle [4];
int testScore [10];
3
Accessing Individual Components
int arr [4];
float angle [4];
int testScore [10];
angle[0] = 4.93;
angle[1] = -15.2;
angle[2] = 0.5;
angle[3] = 1.67;
4
Array Initialization
•Sometimes it is more convenient to leave out the size of the array and let the compiler determine the array's
size for us, based on how many elements we give it:
• Here, the compiler will create an integer array of dimension 8.
5
Index in an Array
#include <iostream>
using namespace std;
int main()
{
int arr[4];
cout << “Please enter 4 integers:“ << endl;
for(int i = 0; i < 4; i++)
cin >> arr[i];
cout << “Values in array are now:“;
for(int i = 0; i < 4; i++)
cout << “ “ << arr[i];
cout << endl;
return 0;
}
6
Arrays as Arguments to Functions
#include <iostream>
using namespace std;
int sum(const int array[], const int length)
{
long sum = 0;
for(int i = 0; i < length; sum += array[i++]); It is important to note that arrays are
return sum; passed by reference and so any changes
} made to the array within the function
will be observed in the calling scope.
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
cout << "Sum: " << sum(arr, 7) << endl;
return 0;
}
7
Arrays as Arguments to Functions
8
Arrays as Arguments to Functions
void Copy ( /* out */ int destination [] ,
/* in */ const int source [],
/* in */ int size )
{ The word const
int i; guarantees that any attempt
for (i = 0 ; i < size; i++) to modify the source
destination [i] = source [i]; array within the Copy
} function results in a
compile-time error
9
No Aggregate Array Operations in C++
➢ Aggregate Operation is an operation on a data structure as a whole, as opposed to an operation on an
individual component of the data structure.
➢ An aggregate operation is one that manipulates the array as an entire unit.
➢ Some programming languages allow aggregate operations on arrays but C++ does not provide aggregate
operations on arrays.
10
Function Overloading
11
Multidimensional Arrays: an Array of Arrays
12
Multidimensional Arrays: an Array of Arrays
int main() {
int twoDimArray[2][4];
twoDimArray[0][0] = 6;
twoDimArray[0][1] = 0;
twoDimArray[0][2] = 9;
twoDimArray[0][3] = 6;
twoDimArray[1][0] = 2;
twoDimArray[1][1] = 0; Note that dimensions must always be provided when initializing
twoDimArray[1][2] = 1; multidimensional arrays, as it is otherwise impossible for the
twoDimArray[1][3] = 1; compiler to determine what the intended element partitioning is.
for(int i = 0; i < 2; i++) { For the same reason, when multidimensional arrays are
for(int j = 0; j < 4; j++) specified as arguments to functions, all dimensions but the first
cout << twoDimArray[i][j]<<“ “; must be provided (the first dimension is optional).
}
cout << endl;
return 0;
}
13
Multidimensional Arrays: Equivalent 1D Array
14
Multidimensional Arrays
Suppose we want to sum row number 3 (the fourth row) in array table and print the result
15
Multidimensional Arrays
Suppose we wanted to sum
and print two rows: row 2
and row 3
16
Multidimensional Arrays
17
Multidimensional Arrays
18
19
Strings
➢ String literals are actually represented by C++ as a sequence of characters in memory. In other words, a
string is simply a character array and can be manipulated as such.
int main()
{
char helloworld[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ‘,
'w', 'o', 'r', 'l', 'd', '!', '\0’ };
cout << helloworld << endl;
return 0;
}
➢ Note that the character array ends with a special character known as the null character. This character
is used to indicate the end of the string.
20
Strings
initialized with string literal constants
uninitialized
21
Strings
22
cctype library
The is-functions check whether a
given character is an alphabetic
character, an uppercase letter, or
a punctuation character,
respectively. These functions
return a Boolean value of either
true or false. The tolower
function converts a given
character to lowercase.
23
cstring library
#include <cstring>
This example creates and initializes two strings, fragment1 and
int main()
fragment2. fragment3 is declared but not initialized.
{
finalString is partially initialized (with just the null character).
char fragment1[] = "I'm a s";
fragment1 is copied into fragment3 using strcpy_s, in effect
char fragment2[] = "tring!";
initializing fragment3 to I'm a s. strcat_s is then used to
char fragment3[20];
concatenate fragment3 onto finalString (the function
char finalString[20] = "";
overwrites the existing null character), thereby giving
finalString the same contents as fragment3. Then strcat_s is
strcpy_s(fragment3, fragment1);
used again to concatenate fragment2 onto finalString.
strcat_s(finalString, fragment3);
strcat_s(finalString, fragment2); finalString is displayed, giving I'm a string!.
cout << finalString;
return 0;
}
24
25
26