Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

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:

  1. int squares[5] = {0, 1, 4, 9, 16};
  2. 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:

  1. for(int i = 0; i < 5; i++) {
  2.   duplicate[i] = squares[i];
  3. }
Question 7.8.1

Design and implement a function named copy that can be used to create a duplicate copy of an integer array of any size.

  1. void copy(int dest[], int src[], int size)
  2. {
  3.   for(int i = 0; i < size; i++)
  4.     dest[i] = src[i];
  5. }