SIT112: Programming and
Logic
Arrays and Strings in C++
Structured Programming 1
Arrays
◼ An array is an identifier that refers to a collection of data
items that all have the same name.
◼ The data items must be of the same type (e.g. all integers
or all characters).
◼ The individual array elements are distinguished from one
another by the value that is assigned to a subscript.
Structured Programming 2
Arrays
◼ When you declare an array variable, you state the type of its
elements and its size.
◼ Below is a representation of an array with 5 character elements.
Each element can be accessed by a combination of the
arrayName and its position in the array (subscript).
I T E M S
0 1 2 3 4
Structured Programming 3
Arrays
int names[4];
names[0] = 101;
names[1] = 232;
names[2] = 231;
names[3] = 0;
◼ We created an array called names, which has space for four
integer variables.
◼ You may also see that we stored 0 in the last space of the
array. This is a common technique used by C++ programmers
to signify the end of an array.
Structured Programming 4
Array syntax
◼ Arrays have the following syntax, using square brackets to access
each indexed value (called an element).
x[i]
so that x[5] refers to the sixth element in an array called x. In C++,
array elements start with 0. Assigning values to array elements is
done by,
x[10] = g;
and assigning array elements to a variable is done by,
g = x[10];
Structured Programming 5
Array example
◼ In the following example, a character based array named
word is declared, and each element is assigned a
character.
◼ The last element is filled with a zero value, to signify the
end of the character string (in C++, there is no string
type, so character based arrays are used to hold strings).
◼ A cout statement is then used to print out all elements of
the array.
Structured Programming 6
Array example
#include <iostream>
using namespace std;
int main() {
char word[20];
word[0] = 'H';
word[1] = 'e';
word[2] = 'l';
word[3] = 'l';
word[4] = 'o';
word[5] = 0;
cout << "The contents of word[] is -->" << word << endl;
return 0;
}
Structured Programming 7
Array example
#include <iostream>
int main() {
char word[20];
word[0] = 'H';
word[1] = 'e';
word[2] = 'l';
word[3] = 'l';
word[4] = 'o';
word[5] = 0;
std::cout << "The contents of word[] is -->" << word << std::endl;
return 0;
} Structured Programming 8
Declaring arrays
◼ Arrays may consist of any of the valid data types.
◼ Arrays are declared along with all other variables in the declaration section
of the program.
#include <iostream>
using namespace std;
int main() {
int numbers[100];
float averages[20];
numbers[2] = 10;
--numbers[2];
cout << "The 3rd element of array numbers is " << numbers[2] << endl;
return 0;
}
Structured Programming 9
Declaring an array
#include <iostream>
using namespace std;
int main() {
int ageArray[5];
ageArray[0] = 10;
ageArray[1] = 11;
ageArray[2] = 9;
ageArray[3] = 8;
ageArray[4] = 12;
cout << "The first child's age is " << ageArray[0] << " years." << endl;
cout << "The second child's age is " << ageArray[1] << " years." << endl;
cout << "The third child's age is " << ageArray[2] << " years." << endl;
cout << "The fourth child's age is " << ageArray[3] << " years." << endl;
cout << "The fifth child's age is " << ageArray[4] << " years." << endl;
return 0;
}
Structured Programming 10
Declaring an array
#include <iostream>
using namespace std;
int main() {
int ageArray[5];
ageArray[0] = 10;
ageArray[1] = 11;
ageArray[2] = 9;
ageArray[3] = 8;
ageArray[4] = 12;
for (int z = 0; z <= 4; z++) {
cout << "The child's age is " << ageArray[z] << " years." << endl;
}
return 0;
}
Structured Programming 11
Declaring an array
#include <iostream>
using namespace std;
int main() {
int ageArray[5];
for (int x = 0; x <= 4; x++) {
cout << "Enter age for child " << x << ": ";
cin >> ageArray[x];
}
for (int z = 0; z <= 4; z++) {
cout << "The child's age is " << ageArray[z] << " years." << endl;
}
return 0;
}
Structured Programming 12
Assigning initial values to arrays
◼ The declaration is preceded by the word static. The initial values are
enclosed in braces.
#include <stdio.h>
main()
{
static int values[] = { 1,2,3,4,5,6,7,8,9 };
static char word[5] = { 'H','e','l','l','o' };
printf("%s", word);
}
Structured Programming 13
Example 2
#include <iostream>
static int values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main() {
std::cout << "Original Array: ";
for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
std::cout << values[i] << " ";
}
return 0;
}
Structured Programming 14
Example 2
#include <iostream>
static int values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
using namespace std;
int main() {
cout << "Original Array: ";
for (int i = 0; i <9; i++) {
cout << values[i] << " ";
}
return 0;
}
Structured Programming 15
Example 3
#include <iostream>
static int values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main() {
// Calculate the sum of elements in the array
int sum = 0;
for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
sum += values[i];
}
std::cout << "Sum of elements: " << sum << std::endl;
return 0;
}
Structured Programming 16
Processing arrays
◼ Single operations which involve entire arrays are not
permitted in C.
◼ Thus if array1 and array2 are similar arrays (i.e. same
data type, same dimensionality and same size),
assignment operations, comparison operations, etc. must
be carried out on an element by element basis.
◼ This is usually accomplished within a loop, where each
pass through the loop is used to process one array
element.
Structured Programming 17
Multi dimensioned arrays
◼ Multi-dimensioned arrays have two or more index values
which specify the element in the array.
multi[i][j]
◼ In the above example, the first index value i specifies a
row index, whilst j specifies a column index.
Structured Programming 18
Declaration and calculations
◼ int m1[10][10];
◼ static int m2[2][2] = { {0,1}, {2,3} };
◼ sum = m1[i][j] + m2[k][l];
Structured Programming 19
Example
#include <iostream>
using namespace std;
int main() {
// Create a 2D array (3x3)
int multiArray[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access and print elements of the 2D array
Structured Programming 20
Example
cout << "Multidimensional Array:" << endl;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << multiArray[row][col] << " ";
}
cout << endl;
}
return 0;
}
Structured Programming 21
Output
Structured Programming 22
Strings
◼ String data types are represented by an array of characters.
◼ They are used to store data such as names.
#include <iostream>
using namespace std;
int main() {
char name[30];
cout << "Enter your first name: ";
cin >> name;
cout << "You have indicated that your first name is: " << name << ". ";
return 0;
}
Structured Programming 23
Strings
◼ In the example above, scanf reads up to the first white
space character.
◼ You can direct scanf to read all characters of a specific
type. If it finds a character which is not of the specified
type, it stops reading and returns what has been read so
far.
◼ This is useful for instance when you enter to enter both
the first name and surname which are separated by white
spaces and one needs to read them together.
Structured Programming 24
Strings
◼ The characters scanf should read are included in square
brackets e.g. [a-z] means continue reading as long as the
characters are alphabetic(lowercase) for both uppercase
and lowercase use [a-zA-Z].
◼ You can use the character ^(circumflex) to negate the
meaning of the characters in the square brackets.
◼ [^1-9] means read every character except a digit. When
scanf comes across a digit, it stops reading and return
what it as read so far.
Structured Programming 25
Strings
#include <iostream>
using namespace std;
int main() {
char name[50];
cout << "Enter your name: ";
cin.getline(name, 50);
cout << "You have indicated that your name is: " << name << endl;
return 0;
}
Structured Programming 26
Null terminated strings
Consider the following program,
#include <iostream>
using namespace std;
int main() {
char name1[] = {'H', 'e', 'l', 'l', 'o'};
char name2[] = "Hello";
cout << name1 << endl;
cout << name2 << endl;
return 0;
}
Structured Programming 27
Built in functions for string handling
•toupper(): Convert characters to uppercase.
•tolower(): Convert characters to lowercase.
std::toupper(): Function for converting
characters to uppercase (requires <cctype>).
std::tolower(): Function for converting
characters to lowercase (requires <cctype>).
Structured Programming 28
toupper()
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
cout << "Enter a name in lowercase: ";
string name;
cin >> name;
for (size_t i = 0; i < name.length(); ++i) {
name[i] = toupper(name[i]);
}
cout << "The name in uppercase is: " << name << endl;
return 0; Structured Programming 29
}
toupper()
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
cout << "Enter a name in Uppercase: ";
string name;
cin >> name;
for (size_t i = 0; i < name.length(); ++i) {
name[i] = tolower(name[i]);
}
cout << "The name in Lowercase is: " << name << endl;
return 0; Structured Programming 30
}