Arrays

October 29, 2004

 

Define:

1.      Declaring an Array:  We declare an array by writing the name of the array, and then the number of items we want in the array in boxed parentheses.

a.     Example:  I would like to make an array called “hours” that can hold up to 4 values. 

                                                                        int hours[4];

2.    Initializing an array:  You must declare the array, and then set it equal to a list of values enclosed in curly braces.  The numbers enclosed in the curly braces are called the Array Initializer.

a.     Example:  I would like to initialize my “hours” array to have specific values.

                                                                        int hours[4] = {3, 34, 53, 321};

3.     int x;

     This is a scalar variable (it only holds one value)

4.    int y[5];

a.     This is an ARRAY (it holds 5 multiple values); y[0], y[1], y[2], y[3], and y[4].

 

Declaring an Array

·        int temps[32];

o       This only Declares the array for temps.  This holds elements temps[0] to temps[31].  If we ignore/waste the value of temps[0] we will have enough for each day of the month.

·        int temps[5] ={ 41, 53, 30, 35, 40};

o        Declares and Initializes the array.  The numbers are contiguous. This initializes so: temps[0]=41, temps[1]=53, temps[2]=30, temps[3]=25, temps[4]=40.

·        int temps[32]={0};

o       This will Declare and Initialize all of the values to be 0.  So now we have temps[0]=0, temps[1]=0… temps[31]=0.

·        int temps[5]={10};

o       This will Declare the array, and Initialize temps[0]=10.  The Array Initializer does not have more values for the rest of the array, so temps[2], temps[3], and temps[4], will be initialized to 0. 

·        int temps[5] = {1,2,3};

o       This will declare and initialize the array.  temps[0]=1, temps[1]=2, temps[2]=3.  But because the Array Initializer has no more values, the remaining (temps[3], temps[4]) will both be initialized to 0.

 

What if you ask for a value outside of the array?

·        If you do this you may get a “Segmentation fault” because you are trying to access information that is not in your allocated memory.  It essentially does not exist in your memory space, for example, you are trying to call on a cell value that is allocated to another user on strauss.

o       An example of asking for a value outside an array would be like trying to print out temps[7], when the declared array was temps[4].  This means that temps[4] obviously does not have a value for temps[7], because it only has 4 values temps[0],temps[1],temps[2],temps[3].  

Note: When you try to access temps[7], it might be that you get a memory location outside your user space and get a segmentation fault. It might also occur that you access memory that is just allocated to another variable on the stack, or a call frame for some function. That is actually worse, becuase your program won't crash; it will just do something odd (like printing out garbage, or you see that some other variable suddenly gets changed even though you didn't change it.)

 

How do I print ALL values of an array?

·        Sample Code:

 

int temps[6]={0, 41, 53, 30, 35, 40};

/*now print the values*/

 

for(i=0; i<6; i++)

     {

          printf(“The temp on October %d was %d”, i, temps[i]);

     }

 

·        Note: when printing all or any values of an array, it is best to use a “for loop.”

 

How do I print an array in a sequence?

·        Example: The temperature represents the hours in the day, so midnight=0 hours, and 11PM=23hours.

 

int temps[5]={42, 51, 13, 14,29};

/*This will only initiate the temps from midnight to 4am*/

 

for(i=0; i<5; i++)

      printf(“Temp at hour %d is %d\n”, i, temps[i]);

 

·        Note:  this also uses a “for loop.”

 

How do I find the sum, max, min, or average of an array?

·        Example of the sum and average of an array called temps.

 

int sum;

double average;

sum=0;

for (i=0; i<5; i++)

     sum+ = temps[i];

average= (sum*1.0)/i;

/* Note: the value of i at the end of the loop is 5 */

 

 

Copyright 2004 by Monica Manno. Permission is given to Phillip T. Conrad to copy this file and post it electronically with modifications, provided that this copyright notice is maintained. All other rights reserved.