< 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; }
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) ton-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 whilei
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 wheni
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; }
#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; }
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; }
#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 |