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

7.6 Subarray Processing

As indicated earlier, once created the size of the array can not be changed. Thus, it is very common when using a static array to create it larger than what you assume will be needed. You must then keep track of the number of elements actually used during the execution of the program. This is known as subarray processing and is the same technique that is used to implement the Python list or a vector.

To use subarray processing with an array, there are two values that must be maintained:

  • The capacity or number of elements allocated for the array must be maintained in order to know when the array is full.
  • The size or number of elements used within the array is maintained as a separate value to know which elements contain valid information.

The elements of an array in C always contain values, even if we do not show them in the array diagrams. When the array is created, contiguous bytes of memory are allocated for the array. Since a computer's memory always contains data as there is no concept of clearing memory, the array will initially contain the values that are stored in the area of memory that is used to store the array. When an element is explicitly set with a value during the execution of the program, we say that element contains a valid value in relation to to the problem solved by the program.

Consider the following code segment which contains two declarations:

  1. const int MAX_GRADES = 12;
  2.  
  3. int main()
  4. {
  5.   int grades[MAX_GRADES];
  6.   int numGrades = 0;

Here we have created an array grades with 12 elements as indicated by the constant variable MAX_GRADES. This value represents the capacity of the array. The numGrades variable will be used to indicate the number of elements within the array, starting at position 0, that store the actual values. Initially, there are no values within the subarray, so this variable is initialized to 0. A memory diagram illustrating the array and its associated size variable is provided below:

Suppose we want to read a collection of grades from the user via standard input. We can extend the main function from above to read the values and store them into the array:

  1.   int value;  
  2.  
  3.   printf("Enter a grade or -1 to stop: ");
  4.   scanf("%d", &value);
  5.   while(value >= 0) {
  6.     grades[numGrades] = value;
  7.     numGrades++;
  8.  
  9.     printf("Enter a grade or -1 to stop: ");
  10.     scanf("%d", &value);
  11.   }
  12. }

Note the use of the numGrades variable to keep track of both the size of the array (the number of valid elements) and the position in to which the next value is stored.

If the user enters the seven grades

    70  89  93  76  84  92  81 

the array and variable will now contain the values as shown below

Question 7.6.1

What is the capacity of the grades array?

Select the correct answer below by clicking on the appropriate box.
  1. 6
  2. 7
  3. 11
  4. 12

The array was created to store 12 elements, which is the full capacity of the array.

Question 7.6.2

What is the size of the subarray within the grades array?

Select the correct answer below by clicking on the appropriate box.
  1. 6
  2. 7
  3. 11
  4. 12

The size of the subarray is indicated by the number of valid elements that the array contains. That number is indicated by the value of numGrades, which is 7.

Question 7.6.3

Suppose we want to extend the program from above to compute the average grade using the values stored in the array. Consider the following code segment for achieving this result:

  1. int sum = 0;
  2. float avg;
  3.  
  4. for(int i = 0; i < MAX_GRADES; i++)
  5.   sum = sum + grades[i];
  6.  
  7. avg = sum / float(MAX_GRADES);
  8. printf("average = %0.2f\n", avg);

Unfortunately, this solution contains a logic error. When executed, it does not print the correct answer of 83.57. Identify the error.

The code segment sums up the values for the full capacity of the array in which some of those elements do not contain valid values. Those invalid values are included in the sum, which produces an incorrect result.

Question 7.6.4

Provide a correct solution for computing and printing the average grade in our sample program above.

  1. int sum = 0;
  2. float avg;
  3.  
  4. for(int i = 0; i < numGrades; i++)
  5.   sum = sum + grades[i];
  6.  
  7. avg = sum / float(numGrades);
  8. printf("average = %0.2f\n", avg);