Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Desired output: 3 4 9 10 12 15.

Similar presentations


Presentation on theme: "Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Desired output: 3 4 9 10 12 15."— Presentation transcript:

1 Structuring Data: Arrays ANSI-C

2 Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Desired output: 3 4 9 10 12 15 17 21 25 32 36 48 How can this be done? If we had lots of variables we could store each input in a variable. But think about what the program would be like. Is there a better way?

3 Representing multiple homogenous data double grade1, grade2, grade3, grade4, grade5, grade6, grade7, total ; /* initialize grades somehow...*/ total = grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 ; printf( “average = %f \n”, total / 7.0) ; What if we had 500 grades to add up instead of 7?

4 Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Two desired outputs: 1. average of those numbers. int number; int sum = 0; int count = 0; double average = 0.0; printf(“Enter a number of ^Z return to end>>”); while ((scanf (“%d”, &number)!= EOF){ sum = sum + number; count = count + 1; printf(“Enter a number of ^Z return to end>>”); } if (count > 0) average = (float) sum/count;

5 Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Two desired outputs: 2. Number of elements read greater than the average found. How to do that? Would need to declare as many variables as input data to store all values. After computing average, compare with each of those variables

6 Representing multiple homogenous data Problem: –Need to declare multiple variables of the same type –These variables will represent similar data –They all will undergo same computations Solution: Array –Built-in type –Allows for the declaration of multiple variables all of the same homogenous type –All variables will have same prefix for the name.

7 Array attributes An array is said to provide the simplest representation of a list of values. An array has a type: representing the type of all the variables it will contain. An array has a size: the number of entries in the array. Each entry will represent one variable. Each variable in the array will be uniquely identified by its index in the array; this index is indicated by an integer value, variable or expression. This index is also refered as the subscript. The bounds of the array index are between 0 and size of array - 1

8 Syntax: Array declaration For a declaration of an array you must give: –Name of the array –Type of the array –Size of the array Having given the size the subscript to an array will range from 0..size-1 Examples: int table[10]; double column[15] Type name size

9 Syntax: Array declaration Examples: int table[10]; double column[15] table contains 10 variables column contains 15 named table[0], table[1], variables named: table[2], table[3], column[0], … table[8], table[9] column[1], column[2], … column[13], column[14] Type name size

10 Array names are identifiers Therefore: They follow the all usual rules for C –identifiers (start with a letter, etc.) –They must be declared before they are used If you see table[y] in program, then you know that – table should be the name of an array – y must be an integer expression with an integer value y >= 0 and y <= size - 1

11 Index Rule Rule: An array index must evaluate to an int between 0 and n-1, where n is the number of elements in the array. No exceptions! Example: given the declaration int grades[7]; grades[i+3+k] /* OK as long as 0<=i+3+k <= 6 */ The index may be very simple grade[0] or incredibly complex grade[(int) (3.1 * fabs(sin (2.0*PI*sqrt(29.067))))]

12 Syntax: Array initialization To initialize an array you must give each entry in the array an specific value. Explicit initialization: int table[5] = { 1, 2, 3, 4, 5}; int table[5] = { 0, 0, 0, 0, 0}; Restriction: can only use this form of initialization at declaration time. Syntax relief: int table[] = { 1, 2, 3, 4, 5}; int table[] = { 0, 0, 0, 0, 0};

13 Array Initializers int w[4] = {1, 2, 30, -4}; /*w has size 4, all 4 are initialized */ char vowels[6] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; /*vowels has size 6, only 5 initializers */ /* vowels[5] is uninitialized */ Cannot use this notation in assignment statement: w = {1, 2, 30, -4}; /*SYNTAX ERROR */

14 Incomplete Array Size double x[ ] = {1.0, 3.0, -15.0, 7.0, 9.0}; /*x has size 5, all 5 are initialized */ But: double x[ ]; /* ILLEGAL */ But double x[5]; /* Legal */

15 Loops and arrays In general to manipulate an array you will use a while loop. Let’s find the smallest of 5 values placed in an array of size 5. int table[5] = { 5, 8, 11, 3, 9}; int min = table[0]; if (min > table[1]) min = table[1]; if (min > table[2]) min = table[2]; if (min > table[3]) min = table[3]; if (min > table[4]) min = table[4]; We have the following pattern: if (min > table[i]) min = table[i]; for i = 1, 2, 3, 4 So we can use a while loop where the body of the loop contains the above code, and the loop variable varies from 1 to 4.

16 int table[5] = { 5, 8, 11, 3, 9}; int min = table[0]; int i = 0; while (i < 5){ if (min > table[i]) min = table[i]; i = i + 1; }

17 Loops and arrays In general to manipulate an array you will use a while loop. A variable for the loop will be the subscript variable. Example: int table[10]; int i = 0; while (i < 10){ table[i] = i*i; i = i + 1; } for(i = 0; i < 10; i++) table[i] = i*i;

18 Bug: array bounds check int table[10]; int i = 0; while( i <= 10){ table[i] = i*i; i = i + 1; } It will initialize table[0], table[1], table[2]… table[8], table[9], table[10] Bug: variable table[10] does not exist C will not detect this as an error!!!!! C will write something somewhere in memory: in the variable right next to variable[9]. C does not detect array out of bounds errors.

19 Reading user’s data in an array Simplest form: the user provides as many data values as entries in the array. Example: int table[10]; int i = 0; while( i < 10){ printf(“Enter value for entry %d”, i); scanf(“%d”, &table[i]); i = i + 1; }

20 Reading user’s data in an array Common form: user does not provide as many values as there are entries in array: –Provides less entries –Or tries to provide more entries. So we need to have a count on the number of entries that were actually entered by user; that count variable we call length of the array. Since user can provide less, we need to have a way for the user to indicate that there is no more data: –A sentinel value: special value indicating end of data. This must be a value outside the range of values for the array. –^Z : we know it indicates no more data. See: readArray.c

21 Things You Can and Can’t Do You can’t –use = to assign one entire array to another. You can’t –use == to directly compare entire arrays You can’t –directly scanf or printf entire arrays But you can do these things on array elements! And you can write functions to do them


Download ppt "Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: 10 15 4 25 17 3 12 36 48 32 9 21 Desired output: 3 4 9 10 12 15."

Similar presentations


Ads by Google