0% found this document useful (0 votes)
5 views17 pages

Understanding Arrays in C++ Programming

Module 4 covers the concept of arrays in programming, explaining their importance for storing multiple data elements of the same type efficiently. It details the structure of one-dimensional and multi-dimensional arrays, including how to declare, initialize, and access their elements. Key points include the fixed size of arrays, the indexing system starting at 0, and the potential for undefined behavior when accessing out-of-bounds elements.

Uploaded by

Heng Dalux
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Understanding Arrays in C++ Programming

Module 4 covers the concept of arrays in programming, explaining their importance for storing multiple data elements of the same type efficiently. It details the structure of one-dimensional and multi-dimensional arrays, including how to declare, initialize, and access their elements. Key points include the fixed size of arrays, the indexing system starting at 0, and the potential for undefined behavior when accessing out-of-bounds elements.

Uploaded by

Heng Dalux
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 4: Array

Instructor: Khouv Tannhuot


Phone: 087 42 47 36 (Telegram)
Array
● So far we've learned about strings and numbers. They allow us to store any
characters that we want to come back to, and also to do mathematical operations.
● However, sometimes we need to store many things and we want to be able to easily
use that group of things. For example, we could want to store a list of our friends,
our books, or even the types of coffee we've tried.
● As soon as we start wanting to deal with multiple different things, many students
initially turn to defining many variables, like this:
Array
However, this poses a few problems:
● First, that your program becomes tied to this style of variable definition. What
happens if you want to add more friends?
● Second, that allowing your program to grow becomes difficult. What if you want to
add 100 friends?
// allocate 100 int variables // input 100 friends’ name // output 100 friends
string friend1; cin >> friend1; cout << friend1;
string friend2; cin >> friend2; cout << friend2;
string friend3; cin >> friend3; cout << friend3;
//… //… //…
string friend100; cin >> friend100; cout << friend100;
Array
However, this poses a few problems:
● First, that your program becomes tied to this style of variable definition. What
happens if you want to add more friends?
● Second, that allowing your program to grow becomes difficult. What if you want to
add 100 friends?
● Third, that performing operations on all friends becomes difficult. For example, what
if you wanted to print out friend names that start with "B"?
What is an Array?
Array is an indexed collection of data elements of the same type.
● Indexed means that the array elements are numbered (starting at 0).
● The restriction of the same type is an important one, because arrays are stored in
consecutive memory cells. Every cell must be the same type (and therefore, the
same size).
Why do we need Arrays?
Instead of declaring each variable and assigning it a value individually, we can declare
one variable (the array) and add the value of the various variables to it.

● They provide a more convenient way of storing variables or a collection of data of a


similar data type together instead of storing them separately.
● Each value of the array will be accessed separately.
Types of Array
There are two types of C++ arrays:

● One dimensional Array


● Multi-dimensional Array
One-Dimensional Array
● Name many variables with one name
● Each variable is called an array element
● All of the variables or elements must be the same data type
● Individual variables or elements are accessed with an index of subscript
● The size of the array is specified when the array is created
● All of the variables of elements are arranged contiguously (i.e. next to each other) in
memory
Declaring Arrays
● An array declaration is similar to the form of a normal declaration (Datatype,
variableName), but we add on a size:
○ datatype variable_name[size];
● This declares an array with the specified size, named variable_name, of type datatype.
● The array is indexed from 0 to size-1. The size (in square brackets) must be a integer
literal or a constant variable. The compiler uses the size to determine how much space to
allocate (i.e. how many bytes).
Example:
int list[30]; // an array of 30 integers
char name[20]; // an array of 20 characters
double nums[50]; // an array of 50 decimals
Initializing Arrays
To insert values to it, we can use an array literal - place the values in a comma-separated
list, inside curly braces:

● int scores[5] = {0, 1, 2, 3, 4};


● int scores[] = {0, 1, 2, 3, 4};
● int scores[5] = {};
Accessing Array
Defining an array:
● int test[10];

Accessing array elements:


● test[3] = 100;
● sum += test[2];
● cout << test[8] << endl;
Copying Arrays
● If we have these two arrays, how do we copy the contents of list2 to list1?
○ int list1[5];
○ int list2[5] = {3, 5, 7, 9, 11};
● With variables, we use the assignment statement, so this would be the natural tendency
-- but it is wrong!
○ list1 = list2; // this does NOT copy the array contents We must copy between arrays
element by element.
A for loop makes this easy, however:
for (int i = 0;i < 5;i++) {
list1[i] = list2[i];
}
Points to Remember
● Array is fix size
● Array is accessed by an index and always starts with index 0
● Array is out of bound
○ If we declare an array of size 10, then the array will contain elements from index
0 to 9
○ However, if we try to access the element at index 10 or more than 10, it will
result in Undefined Behaviour.
Multidimensional Arrays
● This is an array in which data items are arranged to form an array of arrays.
● Multi-dimensional array can have any number of dimensions, but two-dimensional
and three-dimensional arrays are common.
● Syntax:
○ datatype array-name[d1][d2][d3]...[dn];

You might also like