Intro to Computer Science and
programming
Arrays
●Data Types
● So far, we have seen only simple data types, such as int,
float, and char.
● Simple variables can hold only one value at any time during
program execution, although that value may change.
● A data structure is a data type that can hold multiple values at
the same time. (Synonyms: complex data type, composite
data type)
● The array is one kind of data structure.
Arrays
●
● An array is a group of related data items that all have the
same name and the same data type.
● Arrays can be of any data type we choose.
● Arrays are static in that they remain the same size
throughout program execution.
● An array’s data items are stored contiguously in memory.
● Each of the data items is known as an element of the
array. Each element can be accessed individually.
●
●ARRAYS
▪ An array is a collection of elements of the same type that are
referenced by a common name.
▪ Compared to the basic data type (int, float & char) it is an
aggregate or derived data type.
▪ All the elements of an array occupy a set of contiguous memory
locations.
▪ Why need to use array type?
▪ Consider the following issue:
"We have a list of 1000 students' marks of an integer
type. If using the basic data type (int), we will
declare something like the following…"
int studMark0, studMark1, studMark2, ..., studMark999;
● ARRAYS
▪ Can you imagine how long we have to write the
declaration part by using normal variable
declaration?
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …,
…, studMark998, stuMark999, studMark1000;
…
…
return 0;
}
●Array Declaration and Initialization
int numbers[ 5 ] ;
● The name of this array is “numbers”.
● This declaration sets aside a chunk of memory that is
big enough to hold 5 integers.
● It does not initialize those memory locations to 0 or
any other value. They contain garbage.
● Initializing an array may be done with an array
initialization, as in :
● int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;
● numbers ● 5 ● 2 ● 6 ● 9 ● 3
● Accessing Array Elements
● Each element in an array has a subscript (index)
associated with it.
●
● numbers 5 ● 2 ● 6 ● 9 ● 3
●
●
● ● 0
Subscripts are integers 1and2always
3 4begin at zero.
● Values of individual elements can be accessed by
indexing into the array. For example,
printf(“The third element = %d.\n”, numbers[ 2 ] ) ;
would give the output
The third element = 6.
●ARRAYS
▪ Examples of the one-dimensional array declarations,
int xNum[20], yNum[50];
float fPrice[10], fYield;
char chLetter[70];
▪ The first example declares two arrays named xNum and yNum of type int.
Array xNum can store up to 20 integer numbers while yNum can store up to 50
numbers.
▪ The second line declares the array fPrice of type float. It can store up to
10 floating-point values.
▪ fYield is basic variable which shows array type can be declared together
with basic type provided the type is similar.
▪ The third line declares the array chLetter of type char. It can store a string
up to 69 characters.
▪ Why 69 instead of 70? Remember, a string has a null terminating character
(\0) at the end, so we must reserve for it.
Accessing Array Elements (con’t)
●
● A subscript can also be an expression that evaluates to
an integer.
numbers[ (a + b) * 2 ] ;
● Caution! It is a logical error when a subscript evaluates
to a value that is out of range for the particular array.
Some systems will handle an out-of-range error
gracefully and some will not (including ours). Normally,
when you see a file named core (or core*) it means
you exceeded the end of an array!
●
●Modifying Elements
● Individual elements of an array can also be modified using
subscripts.
numbers[ 4 ] = 20 ; /*changes the value of
the element found at subscript 4 to
20 */
● Initial values may be stored in an array using indexing,
rather than using an array initialization.
numbers[ 0 ] = 5 ;
numbers[ 1 ] = 2 ;
numbers[ 2 ] = 6 ;
numbers[ 3 ] = 9 ;
numbers[ 4 ] = 3 ;
Filling Large Arrays
●
● Since many arrays are quite large, using an array
initialization can be impractical.
● Large arrays are often filled using a for loop.
● for ( i = 0; i < 100; i++ )
● {
● values [ i ] = 0 ;
● }
● would set every element of the 100 element array “values”
to 0.
Example problems
● Add any 10 numbers using an array
● Find the largest number in a given set of numbers