Lesson Proper For Week 7: C++ Arrays
Lesson Proper For Week 7: C++ Arrays
Lesson Proper For Week 7: C++ Arrays
C++ ARRAYS
In this tutorial, we will learn to work with arrays. We will learn to declare, initialize, and access array
elements in C++ programming with the help of examples.
In C++, an array is a variable that can store multiple values of the same type. For example,
Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating
27 separate variables, we can simply create an array:
double grade[27];
In C++, the size and type of arrays cannot be changed after its declaration.
dataType arrayName[arraySize];
For example,
int x[6];
Here,
In C++, each element in an array is associated with a number. The number is known as an array
index. We can access elements of an array by using those indices.
array[index];
· The array indices start with 0. Meaning x[0] is the first element stored at index 0.
· If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the
last element.
· Elements of an array have consecutive addresses. For example, suppose the starting address
of x[0] is 2120d. Then, the address of the next element x[1] will be 2124d, the address of x[2] will be
2128d and so on.
Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.
Here, we have not mentioned the size of the array. In such cases, the compiler automatically
computes the size.
C++ Array With Empty Members
In C++, if an array has a size n, we can store upto n number of elements in the array. However, what
will happen if we store less than n number of elements.
For example,
Here, the array x has a size of 6. However, we have initialized it with only 3 elements.
In such cases, the compiler assigns random values to the remaining places. Oftentimes, this random
value is simply 0.
mark[3] = 9;
#include <iostream>
int main() {
}
}
return 0;
Run Code
Output
Here, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we have
printed numbers[i].
We again used a range based for loop to print out the elements of the array. To learn more about
this loop, check C++ Ranged for Loop.
Note: In our range based loop, we have used the code const int &n instead of int n as the range
declaration. However, the const int &n is more preferred because:
1. Using int n simply copies the array elements to the variable n during each iteration. This is not
memory-efficient.
&n, however, uses the memory address of the array elements to access their data without copying
them to a new variable. This is memory-efficient.
2. We are simply printing the array elements, not modifying them. Therefore, we use const so as
not to accidentally change the values of the array.
#include <iostream>
int main() {
}
}
return 0;
Run Code
Output
Enter 5 numbers:
11
12
13
14
15
Once again, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we took an input
from the user and stored it in numbers[i].
Example 3: Display Sum and Average of Array Elements Using for Loop
#include <iostream>
int main() {
// initialize an array without specifying size
sum += n;
++count;
}
cout << "\nTheir Sum = " << sum << endl;
cout << "Their Average = " << average << endl;
return 0;
}
Run Code
Output
Their Sum = 92
In this program:
1. We have initialized a double array named numbers but without specifying its size. We also
declared three double variables sum, count, and average.
Here, sum =0 and count = 0.
2. Then we used a range based for loop to print the array elements. In each iteration of the loop,
we add the current array element to sum.
3. We also increase the value of count by 1 in each iteration, so that we can get the size of the
array by the end of the for loop.
4. After printing all the elements, we print the sum and the average of all the numbers. The
average of the numbers is given by average = sum / count;
A normal for loop requires us to specify the number of iterations, which is given by the size of the
array.