7.8 Common Array Algorithms
As you would have learned in earlier courses, there are a number of problems that require the use arrays and the processing of the array elements. In this section, we explore some of the more common algorithms.
Copying an Array
Creating a duplicate copy of an array is a very common operation. Consider two arrays:
- int squares[5] = {0, 1, 4, 9, 16};
- int duplicate[5];
and suppose you want to copy all of the values from the first array to the second. Attempting to assign the source array to the destination results in a syntax error:
duplicate = squares; // Syntax error
You can not assign one static array to another. Instead you must copy the individual values, element by element:
- for(int i = 0; i < 5; i++) {
- duplicate[i] = squares[i];
- }

