Arrays

February 18 2002

announcements

the midterms are graded, you will get them back in lecture on tuesday.

introduction to arrays

an array is a collection of variables of the same type. arrays always have a fixed size. for example:

int my_array[10];

this creates an array of 10 integers called my_array. in other words, my_array consists of 10 integer variables. the variables that make up the array are assigned "index" numbers, starting from 0, and we can refer to the individual variables within the array by using these index numbers. so, for my_array, we can refer to the following 10 integer variables:

my_array[0] my_array[1] my_array[2] my_array[3] my_array[4] my_array[5] my_array[6] my_array[7] my_array[8] my_array[9]

don't forget that we start counting from zero, and that my_array[10] does not exist! also, we can't assume anything about the values of these variables, since we didn't specify initial values.

a variable in an array can do all the things a normal variable can. so, we can do all the usual things, such as:

my_array[5] = 24;
my_array[4] = my_array[5] - 12;
scanf("%d", &my_array[6]);
printf("%d\n", my_array[4]);

declaring arrays

there are several important details about declaring arrays. the first is that the size of arrays must be constant [for now]. this means that we can't use a variable as the size of the array. so, we can't do this:

/* this does not work */
int x = 5;
int oops[x];

however, we can use symbolic constants as the size of the array:

#define NUM_INTS 24

/* void main(), etc... */

/* this works */
int array[NUM_INTS];

if you want to specify an initial value for an array, you do it like this:

int array[] = { 24, 51, 32, 13, 54 };

this is equivalent to:

int array[5];
array[0] = 24;
array[1] = 51;
array[2] = 32;
array[3] = 13
array[4] = 54;

note that we don't have to specify the size of the array when we specify initial values.

operations on entire arrays

if you want to do something with all the elements of an array, we can use a for loop. suppose we want to add up all the numbers in our array. we can say:

int total = 0;
int i;
for(i=0; i < 10; i++) 
{
  total = total + my_array[i];
}

this for loop will run 10 times, with i values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. this is great, because the elements of my_array have those index numbers. so, each time we go through the for loop, it will get the value of another variable in our array, and add it to the total.

this type of for loop is very useful, and it shows up a lot, so make sure you understand how it works.

passing arrays to functions

to declare a function that receives an array as input, we can do this:

/*
 * compute the average of the numbers in an array. 
 *
 * inputs: an array of integers, and the number of integers in the
 *         array
 * output: the average
 */
double average(int array[], int size)
{
  int i;
  double total = 0;
  for(i=0; i < size; i++)
  {
     total = total + array[i];
  }

  return total / size;
}

note that the brackets are empty for the array that we accept as input. that's why we need the second argument, which tells us how many numbers we have in the array.

strings

a string is just an array of characters, terminated with null [don't forget that the null character is '\0'].

string constants are always enclosed in double quotes [in the same way that character constants are always enclosed in single quotes]. so, we can initialize strings like this:

char my_string[80] = "hello";

this is equivalent to:

char my_string[80];
my_string[0] = 'h';
my_string[1] = 'e';
my_string[2] = 'l';
my_string[3] = 'l';
my_string[4] = 'o';
my_string[5] = '\0';

note how the null character is used to mark the end of the string.

we can use the %s format specifier in printf and scanf to print strings to the screen, and to read strings from the keyboard. for example:

scanf("%s", my_string);
printf("%s\n", my_string);

a couple things to note here: scanf will read one word [a sequence of characters without spaces between them], store the word into my_string [scanf will put the null terminator on the string for us], and then printf will display the string, up to and not including the null terminator. so, if i run this program, and i type in "hello there", the program will output "hello".

very important: we don't need an ampersand in front of my_string in scanf! hopefully we'll see why next week.

exercises

write a function array_max that accepts an array of integers and the number of integers in the array as inputs, and outputs the largest number in the array. write a main() to test your function.

write a program that gets a word from the user, and outputs the number of letters in the word. so, if i type in "hello", the program should output 5.

write a program that gets a word from the user, and prints out the word with all vowels removed. so, if i type in "hello", the program should output "hll".