Unit 2: Arrays, strings and data structures
Object Oriented Programming using C++ notes · PTU syllabus (PGCA1911)
On this page
Unit summary
Arrays, strings, structures and unions hold collections of data. This unit covers array declaration, one- and multi-dimensional arrays and their memory representation, matrices, strings and string-handling functions, and structures and unions.
After this unit you can
- Declare and process one- and multi-dimensional arrays
- Perform matrix operations
- Handle C-style strings and the string class
- Use structures and unions
PTU syllabus topics
- Array declaration
- single and multi-dimensional arrays
- memory representation
- matrices
- strings and string handling functions
- structures and unions
Holds
Many items of one type
Items of different types
Access
By index: a[2]
By member name: s.age
Example
int marks[5];
struct Student {int roll; char name[20];};
Topic 1
Arrays: declaration, single and multi-dimensional, memory representation
An array is a collection of elements of the same type stored in consecutive memory locations under one name. Elements are accessed by an index starting from 0.
- One-dimensional:
int marks[5] = {70, 82, 65, 90, 77};— marks[0] is 70. - Two-dimensional (matrix):
int a[3][3];— a[i][j] is row i, column j. - Passing an array to a function passes the address of its first element, so the function works on the original array:
void display(int a[], int n);
c/* Largest element of an array */
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max) max = a[i];Topic 2
Matrices
c/* Matrix addition and multiplication */
int a[2][2] = {{1, 2}, {3, 4}}, b[2][2] = {{5, 6}, {7, 8}}, c[2][2] = {0};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
c[i][j] += a[i][k] * b[k][j]; /* c = {{19, 22}, {43, 50}} */- Row-major storage: element a[i][j] of an m × n array is at base + (i × n + j) × size.
- Common programs: addition, multiplication (columns of A = rows of B), transpose, sum of diagonals, checking symmetry.
Topic 3
C-style strings and string-handling functions
A string is an array of characters ending with the null character '\0'. char name[10] = "SBS"; uses 4 bytes ('S', 'B', 'S', '\0').
| Function (string.h) | Purpose |
|---|---|
| strlen(s) | Length, not counting '\0' |
| strcpy(d, s) | Copies s into d |
| strcat(d, s) | Joins s to the end of d |
| strcmp(a, b) | Compares: 0 if equal |
| strrev(s) | Reverses (compiler-specific) |
Topic 4
The C++ string class
cpp#include <string>
#include <algorithm>
string s = "Synetic";
s += " Business School"; // concatenation
cout << s.length() << ' ' << s.substr(0, 7) << ' ' << s.find("School") << '\n';
s.replace(0, 7, "SBS");
reverse(s.begin(), s.end());
string a = "apple", b = "banana";
cout << (a < b) << ' ' << a.compare(b) << '\n'; // lexicographic comparison
for (char &c : a) c = toupper(c); // APPLE- The string class manages memory automatically and supports =, +, ==, <, [] and methods such as length, substr, find, insert, erase, replace — safer than char arrays.
Topic 5
Structures and unions in C++
- In C++ a struct can contain member functions and access specifiers; members are public by default (in a class they are private by default).
- Unions share memory among members; C++ unions can have member functions; anonymous unions allow direct member access.
cppstruct Point {
int x, y;
void show() { std::cout << "(" << x << ", " << y << ")\n"; }
};Key terms
- Array
- Collection of elements of the same type in contiguous memory
- Row-major order
- Storing a 2D array row by row
- Null character
- '\0' marking the end of a C-style string
- Structure
- User-defined type grouping members of different types
- Union
- Type whose members share the same memory
Quick revision
- Declaration and initialisation; index from 0; address formula.
- 2D arrays; matrix addition, multiplication, transpose.
- strlen, strcpy, strcat, strcmp; string class methods.
- struct members, arrays of structures, nesting.
- union shares memory; size equals largest member; C++ structs can have functions.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Give the address formula for a[i] in a one-dimensional array.
- Q2.Write the condition for multiplying two matrices.
- Q3.Distinguish strcpy and strcat.
- Q4.State two advantages of the string class over char arrays.
- Q5.Distinguish a structure and a union.
- Q6.How is a structure in C++ different from one in C?
Long-answer questions
- Q1.Explain one- and two-dimensional arrays with memory representation.
- Q2.Write a program for matrix multiplication.
- Q3.Explain string handling in C++ with programs.
- Q4.Explain structures and unions with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying M.Sc IT at Synetic.
