Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give (enumerate) each of the values which a variable of that type can have typedef enum {entertainment, rent, utilities, food, clothing, miscellaneous} expense_t;
Enumerated Types We can now use the type expense_t to declare variables expense_t expense_var; –Then the value entertainment will be represented by a 0, rent by a 1, etc. –We can assign values to a variable of this type as follows: expense_var = entertainment; –We can perform any operation on this variable that we can perform on an integer
Enumerated Types 4 Use enumerated types rather than integer codes to make your program more readable 4 Typedefs should be placed just after the precompiler directives (and before any variable declarations which use the type) 4 You cannot read in or print out enumerated types - only the integer equivalents 4 The order of enumeration defines the collation order of the type
Arrays 4 Simple variables use a single storage cell to store a single variable –If we want to store (for example) 5 values, we have to declare 5 variables –Sometimes we process a group of related data items (e.g. exam scores for a class) –In this case, it is awkward to declare individual variables for each value so we use a data structure called an array to hold all of the values
Arrays 4 An array is a collection of two or more adjacent memory cells called array elements We can declare an array with a single variable declaration, e.g. double x[8]; declares an array of eight doubles –The array has a single name –To refer to individual elements of the array we use the array name with an array subscript
Arrays x[5] = 77.0; printf(“%f\n”, x[5]); This example refers to the fifth value of the array x Note that the first value in array x is x[0] - that is, array subscripts start with 0 What is the last element of array x then? 4 Generally, we will always refer to individual elements of the array (i.e. we will always use subscripts with the array name)
Arrays The declaration of an array of a given type is the same as the declaration of a simple variable of that type except that we follow the name of the array with [size] where size is the size of the array –Note that we don’t specify upper and lower bounds for the array, just the size 4 Parallel arrays store related information in corresponding fields of two separate arrays
Array Initialization 4 A simple variable can be initialized when it is declared: int int_var = 0; –An array can also be initialized when it is declared int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; –Note that we don’t need to give the size of the array if we initialize it in the declaration (otherwise, we do)
Array Subscripts 4 A subscript can be an integer literal, a constant given in a #define, or any expression which evaluates to an integer value –The expression must evaluate to a value in the range of 0 to size of array - 1, otherwise a run- time error will occur –The expression cannot evaluate to a floating point number
Examples i = 5; x[0]=16.0; x[1]=12.0; x[2]=6.0; x[3]=8.0; x[4]=2.5; x[5]=12.0; x[6]=14.0; x[7]=-54.5; printf( “%d %.1f“, 4, x[4]); printf( “%d %.1f“, i, x[i]); printf( “%.1f“, x[i] + 1); printf( “%.1f“, x[i] + i); printf( “%.1f“, x[i + 1]); printf( “%.1f“, x[i + i]); printf( “%.1f“, x[2 * i]);
Examples printf( “.1f“, x[2 * i - 3]); printf( “.1f“, x[(int)x[4]]); printf( “.1f“, x[i++]); printf( “.1f“, x[--i]); x[i-1] = x[i]; x[i] = x[i+1]; x[i] - 1 = x[i];
Using for Loops with Arrays 4 Since the elements of an array are related, we often want to perform the same processing on each element of the array 4 We can do this by using a for loop to step through the array, element-by-element –The loop variable is initialized to the lower bound of the array –The condition is to exit when the upper bound is reached
Using for Loops with Arrays –The loop variable is used as the subscript of the array in the loop body for (i = 0; i < SIZE; ++i) square[i] = i * i; for (j = 0; j < SIZE; j++) printf(“The score for %d is %d\n”, j, score[j]); for (k = 0; k < MAX_ITEM; ++k) scanf(“%lf”, &x[k]);
Using for Loops with Arrays #include #define MAX_ITEM 8 int main(void) { double x[MAX_ITEM], mean, st_dev, sum, sum_sqr; int i; printf(“Enter %d numbers separated by blanks “ “or s\n”, MAX_ITEM); for (i = 0; i < MAX_ITEM; ++i) scanf(“%lf”, &x[i]); sum = 0; sum_sqr = 0;
Using for Loops with Arrays for (i = 0; i < MAX_ITEM; ++i) { sum += x[i]; sum_sqr += x[i] * x[i]; } mean = sum / MAX_ITEM; st_dev = sqrt(sum_sqr / MAX_ITEM - mean*mean); prinf(“The mean is %.2f.\n”, mean); printf(“Standard deviation is %.2f.,st_dev); printf(“\nTable of differences\n); printf(“IndexItemDifference\n”); for (i = 0; i < MAX_ITEM; ++i) printf(“%3d%4c%9.2f%5c%9.2F\n”,i,’ ‘,x[i], ‘ ‘,x[i]-mean); return(0); }
Using Array Elements as Function Arguments 4 Individual array elements can be used as actual array arguments –printf(“%d\n”,x[i]); –scanf(“%d %d”, &x[0], &x[1]); 4 The array of the element which we are passing must be of the same type as the formal argument of the function –Note that an array element can be used as either an input argument - x[i] - or an output argument - &x[0]
Array Arguments 4 We can also write functions that have arrays as arguments (i.e. that manipulate all or part of an array) 4 We can declare an array function as an argument with no subscript –int list[] –This declaration allows us to use the array as an output parameter or input/output parameter (but not input only)
Array Arguments 4 An important point is that in the argument declaration we don’t indicate the size of the array –If we want to do this, we must use another argument for this purpose –int list[], int size_of_list 4 C does not allocate memory for a local copy of the array, instead it passes the address of the first element of the array
Array Arguments 4 Given a formal argument for an array of the type shown previously, we can call the function by using the name of the array as the actual argument in the function call: func_name(my_array, ARRAY_SIZE); –We don’t use a subscript in the function call –The actual argument is just the first element of the array, so we could also use: func_name(&my_array[0], ARRAY_SIZE); –The first version is preferred
Array Arguments In the formal parameter list, instead of int list[], we could also int *list –This is because C passes the address of the first element of the array –You probably won’t use this form until we start to work with strings ANSI C we can include a qualifier in the declaration of the array formal parameter to notify the compiler that the array is input only - void my_func(const int my_array[]);
Array Arguments 4 In C, it is not legal for a function’s return type to be an array –We must therefore use an output argument to return the array –The calling function must supply an array for the output results (i.e. the calling function must declare an array of the desired size and pass the address of the array, but it need not initialize the array)
Array Arguments 4 It is often the case that we don’t know exactly how many data items we must process in a program –Suppose we are processing these data items in an array –We can declare the array to be as large as the largest data set we will have to process and then just use a part of the array if the data set is smaller than that