Introduction to Programming in C

Index   |   Using VC++
 
Introduction
Basic Output
Variables
Conditionals
Loops
Example 1
More on Conditionals
Further Exploration
Algorithm Design
10 Arrays
 
< Previous

Arrays

 

There are times when you will want to store a list of values, e.g. the number of inches of snow each day during a one-week period, or letter grades for each of your classes. While you could declare a variable for each value, it is much more convenient to use an array in these cases.

When you declare an array, you can specify how many values it will hold. They must all be the same type.

  • Create a project called Arrays.
  • Create a source file called main.c.
  • Type in (or copy/paste) the following code:
  • #include <stdio.h>
    
    int main()
    {
    	int list[3];  // can hold 3 items
    
    	/* initialize the array items */
    	list[0] = 5;
    	list[1] = -2;
    	list[2] = 23;
    
    	/* output... */
    	printf("The first item in the array is %d\n",
    		list[0]);
    	printf("The last item in the array is %d\n",
    		list[2]);
    
    	return 0;
    }
    
  • Save your file.
  • Build your program (F7).
  • Check the Output window for errors.
  • Run your program (Ctrl-F5).

Explanations

  • As with all variables, an array needs to be declared. The syntax for declaring an array is to start with the type (as usual), then the name for your array, followed by brackets ( [ ] ) containing an integer value that specifies the number of items that can be stored into the array. The code above declares a variable called "list" that can hold 3 integers.
  • In the declaration, the value in the brackets must be a constant (either a literal, as in the code above, or a #define value).
  • Note that as with other variables, declaring an array does not initialize its values. Your code will need to set a value for each item in the array.
  • To access an item in the array (to set or read it), use the variable name followed by brackets containing the index of the desired item. Note that index values start at 0, not 1. This means that if there are n items in an array, the valid index values are from 0 (the first item) to n-1 (the last item).
  • Note that you need to set each item individually.

Changes

Make the following changes to your program:

  • It is common to use loops to access each item in an array. A typical loop to output values is shown below. (Existing code is in gray; the loop replaces the two printf statements above.) Note that the loop variable (i) starts at 0, and the loop continues while i is less than the number of items in the array -- this means that the loop will continue for index value 2 (the last valid index), but the loop will exit when i becomes 3 (the first invalid index value).
  • #include <stdio.h>
    
    int main()
    {
    	int list[3];  // can hold 3 items
    	int i;
    
    	/* initialize the array items */
    	list[0] = 5;
    	list[1] = -2;
    	list[2] = 23;
    
    	/* output... */
    	for (i = 0; i < 3; ++i)
    		printf("Value = %d\n", list[i]);
    
    	return 0;
    }
    
  • Add a loop to input values. When you run this program, note that the prompt labels values starting at 1 (not 0) since that's a nicer user interface.
  • #define _CRT_SECURE_NO_DEPRECATE
    
    #include <stdio.h>
    
    int main()
    {
    	int list[3];  // can hold 3 items
    	int i;
    
    	/* initialize the array items */
    	for (i = 0; i < 3; ++i)
    	{
    		printf("Please enter a value");
    		printf(" for item %d: ", i+1);
    		scanf("%d", &list[i]);
    	}
    
    	/* output... */
    	for (i = 0; i < 3; ++i)
    		printf("Value = %d\n", list[i]);
    
    	return 0;
    }
    
  • To change to type of item stored in the array, change the type specified in the declaration. Note that the index is always an integer, regardless of the type of item stored in the array. The code below also changes to specify a floating point value in the input (scanf) and output (printf) statements. Test this program by entering values that include decimal points.
  • #define _CRT_SECURE_NO_DEPRECATE
    
    #include <stdio.h>
    
    int main()
    {
    	double list[3];  // can hold 3 items
    	int i;
    
    	/* initialize the array items */
    	for (i = 0; i < 3; ++i)
    	{
    		printf("Please enter a value");
    		printf(" for item %d: ", i+1);
    		scanf("%lf", &list[i]);
    	}
    
    	/* output... */
    	for (i = 0; i < 3; ++i)
    		printf("Value = %f\n", list[i]);
    
    	return 0;
    }
    
  • Notice that the value 3 is used in several places in this code. What if you wanted to change to have the array hold 5 values? You'd need to make sure to change all of the 3's to 5's. (Picture forgetting to change the 3 in the input loop -- you'd have two uninitialized values at the end of your array.) We can make our code easier to maintain, and therefore less prone to bugs, by using a #define value to specify the number of items in the array:
  • #define _CRT_SECURE_NO_DEPRECATE
    
    #include <stdio.h>
    
    #define numItems 3
    
    int main()
    {
    	double list[numItems];
    	int i;
    
    	/* initialize the array items */
    	for (i = 0; i < numItems; ++i)
    	{
    		printf("Please enter a value");
    		printf(" for item %d: ", i+1);
    		scanf("%lf", &list[i]);
    	}
    
    	/* output... */
    	for (i = 0; i < numItems; ++i)
    		printf("Value = %f\n", list[i]);
    
    	return 0;
    }
    

Exercises

  • Change the number of items in the array.
  • Change the output code to show something like:
    Value #1 = 5
    Note that it's a nice user interface to show output starting with the number 1 (i.e. not "Value #0").
  • Add code to calculate (and show) the sum of the items in the array.
  • Add code to show all the values in the array that are greater than the average value.

When you are done working with this project, select File/Close Solution.

Summary

Coming later...

< Previous