Title: Structuring Data: Arrays
1Structuring Data Arrays
2Representing 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?
3Representing 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?
4Representing 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 endgtgt)
- while ((scanf (d, number)! EOF)
- sum sum number
- count count 1
- printf(Enter a number of Z return to endgtgt)
-
- if (count gt 0)
- average (float) sum/count
5Representing 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
6Representing 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.
7Array 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
8Syntax 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 table10 double column15
9Syntax Array declaration
- Examples
- int table10 double column15
- table contains 10 variables column
contains 15 - named table0, table1,
variables named - table2, table3,
column0, - table8, table9
column1, column2, -
column13, column14
10Array 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 tabley 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 gt 0 and y lt size - 1
11Index 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 grades7
- gradesi3k / OK as long as 0lti3k lt 6 /
- The index may be very simple
- grade0
- or incredibly complex
- grade(int) (3.1 fabs(sin (2.0PIsqrt(29.067)))
)
12Syntax Array initialization
- To initialize an array you must give each entry
in the array an specific value. - Explicit initialization
- int table5 1, 2, 3, 4, 5
- int table5 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
13Array Initializers
- int w4 1, 2, 30, -4
- /w has size 4, all 4 are initialized /
- char vowels6 a, e, i, o, u
- /vowels has size 6, only 5 initializers /
- / vowels5 is uninitialized /
- Cannot use this notation in assignment statement
- w 1, 2, 30, -4 /SYNTAX ERROR /
14Incomplete 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 x5 / Legal /
15Loops and arrays
- In general to manipulate an array you will use a
while loop. - Lets find the smallest of 5 values placed in an
array of size 5. - int table5 5, 8, 11, 3, 9
- int min table0
- if (min gt table1)
- min table1
- if (min gt table2)
- min table2
- if (min gt table3)
- min table3
- if (min gt table4)
- min table4
We have the following pattern if (min gt
tablei) min tablei 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 table5 5, 8, 11, 3, 9
- int min table0
- int i 0
- while (i lt 5)
- if (min gt tablei)
- min tablei
- i i 1
17Loops 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 table10
- int i 0
- while (i lt 10)
- tablei ii
- i i 1
for(i 0 i lt 10 i) tablei ii
18Bug array bounds check
- int table10
- int i 0
- while( i lt 10)
- tablei ii
- i i 1
-
- It will initialize table0, table1, table2
table8, table9, table10 - Bug variable table10 does not exist
- C will not detect this as an error!!!!!
- C will write something somewhere in memory in
the variable right next to variable9. - C does not detect array out of bounds errors.
19Reading users data in an array
- Simplest form the user provides as many data
values as entries in the array. - Example
- int table10
- int i 0
- while( i lt 10)
- printf(Enter value for entry d, i)
- scanf(d, tablei)
- i i 1
-
20Reading users 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
21Things You Can and Cant Do
- You cant
- use to assign one entire array to another.
- You cant
- use to directly compare entire arrays
- You cant
- directly scanf or printf entire arrays
- But you can do these things on array elements!
- And you can write functions to do them