15-2 • Know how to declare arrays, access elements of an array. • Understand how character arrays can represent strings. • Use compact C notation such as assignment operators or the increment and decrement operators. • Two dimensional arrays • Related Chapters: ABC Chapter 6.1, 6.2, 6.1215-3 A one dimensional array is a list of data values, with all values having the same data type(the base type), such as: • integer • float • double • char Technically, an array is a uniform data structure. Individual array elements are referenced using the array name and a subscript that identifies the element position in the array.15-4 For a one dimensional array, we specify the array name, its base data type, and the number of storage locations required using declarations such as int a[25]; float x[100], y[100]; which specifies 25 integer locations for a and 100 floating-point locations for arrays x and y. Storage for array elements are in contiguous locations in memory, referenced by subscript(or index) values starting at 0. Thus for array a above, the storage is . . . a[0] a[1] a[24] RAM15-5 We can initialize array elements in declaration statements; e.g., int counts[5] = {1, 2, 3, 4, 5}; int evens[] = {2, 4, 6, 8}; The array size could also be specified using a symbolic constant: #define ARRAY_SIZE 25 int a[ARRAY_SIZE]; . . . /* evens has 4 elements */ We cannot specify more initial values than there are array elements, but we can specify fewer initial values, and the remaining elements will be initialized to 0. e.g., int b[10] = {2}; double x[250] = {0};15-6 Note: when referencing a particular element of an array use square brackets, not parenthesis or curly braces. Given that the array x is declared as: int x[250]; To initialize a large array to a nonzero value - say 10, we can use a loop. e.g., for (k = 0; k <= 249; k = k+1) x[k] = 10; k is a subscript15-7 A subscript value for an array element can be specified as any integer expression. For example, given the following declarations: double y[4] = {-1.0, 12.0, 3.5, 3.2e-2}; int c = 2; the following statement would assign the value of 5.5 to y[1] y[2 * c - 3] = 5.5;15-8 1. Problem Definition Write a program “fib” that prompts the user for an integer n. The program prints the first n Fibonacci numbers. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = n - the count of Fibonacci Numbers to be printed Output= Fib Numbers printed to screen. 3. Develop Algorithm Use the formula: fib0= 1, fib1 =1 and for k > 1 fibk = fibk-1 + fibk-2#include <stdio.h> /* C program to print the first n Fibonacci Numbers. */ void main(void) { int k,n; int fib[100] = {1,1}; /* note: n must be <= 100 , why ? */ printf("How many Fibonacci number do you want listed? "); scanf("%i",&n); for (k = 2; k < n; k++) /* the ++ is the increment operator */ fib[k] = fib[k-1] + fib[k-2]; /* print the results, four per line */ for (k = 0; k < n;++k) { if (k % 4 == 0) printf(“\n”); printf("%8i", fib[k]); /* that’s 8 ints */ } /* end of for loop */ printf("\n"); } /* end of main */ (For large values of parameter n, we would need more space for each printed column. Also, the calculated values may become too large for the allocated storage space for a datatype int.)15-10 : . Since incrementing and decrementing (negative increments) are common programming operations, the C language provides shortcut Increment / Decrement operators. ++k Increment k by 1 and use incremented value in expression containing ++k. k++ Use the current value of k then increment by 1. --k Decrement k by 1 and use decremented value in expression containing --k. k-- Use current value of k then decrement by 1. ++k;15-11 ++k; /*C statement k = k + 1; */ int x,z; int y = 1; int a = 2; x = y + a++; /* now x = 3 , y = 1 , a = 3 */ z = x + --a; /* now a = 2, z = 5, x = 3, y = 1 */ z = --x * ++y; /* now x = 2, y = 2, z = 4, a = 2 */ x = a++ / --y; /* now y = 1, x = 2, a = 3, z = 4 */ Note: The increment/decrement operators can be applied only to single variables. They cannot be applied to expressions.15-12 1. Problem Definition Write a program that inputs a list of numbers into an array. The program then calculates the average value, and then prints a list of differences. The differences are computed by taking the original values in the list minus the average. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = list of reals in a file “input.dat” . We will use Unix redirection to read values from this file. Output= The average and the list of differences.#include <stdio.h> void main(void) { int counter,k; double datAve, sum = 0.0, datVal[500]; /* 500 element max */ /* read the file and compute the average */ counter = 0; while (EOF != scanf("%lf", &datVal[counter])) { sum += datVal[counter]; /* += is an assignment operator */ counter++; } /* compute and print the average */ datAve = sum /counter; printf("The average is:%lf \n", datAve); /* compute and print the diff list */ for(k=0;k<counter;++k) printf("%lf\n", datVal[k]-datAve); }15-14 - a shortcut method for writing assignment statements of the form var1 = var1 op expression; Using an "assignment operator", we can rewrite the above as var1 op= expression; where op can be any one of the arithmetic binary operators: + - * / % sum += datVal[counter];15-15 sum += datVal[counter]; or sum = sum + datVal[counter]; k += 5; or k = k +5; n -= 4; or n = n -4; a *= 2.0; or a = a*2.0; x /= b; or x = x/b; remainder %= 6; or remainder = remainder % 6; newValue += (a-b)/(2*c); or newValue = newValue + (a-b)/(2*c);15-16 Unlike many other languages, there is no data type for character strings in C. We will implement strings in C by using a one dimensional array of characters where the last character is the “NULL” character ‘\0’.15-17 char aString[5] = "Zip!"; char atomic[ ] = "hydrogen"; Declaring character arrays with a string constant IMPORTANT!!! In C, you must always terminate a character array with the NULL character, ‘\0’ . Therefore, the array size of your character array should be one plus the maximum length of the string you want to store. Example: In the declaration char atomic[ ] = "hydrogen"; “atomic” is
View Full Document