Académique Documents
Professionnel Documents
Culture Documents
Declaration of an Array
• Standard array declaration is as follows:
type var_name[size];
• For example:
int temp[7];
Here the int specifies the type of variable, and temp is
the name of array.
[7] tells how many variables of type int will be in the
array.
Each separate variable in array is called an “element”
More Examples:
double height[10];
float width[20];
1
Arrays in Memory
• In C Language, arrays starts at position 0.
• The elements of the array occupy adjacent locations
in memory
• Example: int temp[10];
• The k-th element of array A is specified by A[k-1]
(0 based)
Subscripts: the numbers in brackets following the
array name are called subscript and are used to refer to
individual elements of array.
0 1 2 3 4 5 6 7 8 9 …
int temp[10]
10 elements
temp[2]
largest element one less than size of
array
2
Using Arrays
0 1 2 3 4 2 4 6 8 10
a[0] a[1] a[2] a[3] a[4] a[0] a[1] a[2] a[3] a[4]
3
• The program reads in 7 temperatures, stores them in
an array and then to calculate average temperature,
reads them back out of the array, adding them
together, dividing by 7.
Output:
Enter temperature for day0: 74
Enter temperature for day1: 76
Enter temperature for day2: 77
Enter temperature for day3: 77
Enter temperature for day4: 64
Enter temperature for day5: 66
Enter temperature for day6: 69
Average is 71.
4
Using Different Variable Type
void main (void)
{
float temper [ 7 ] , sum;
int day ;
for ( day = 0 ; day < 7 ; day + + )
{
printf ( “Enter Temperature for day %d: “, day ) ;
scanf ( “ %f “, &temper [ day ] ) ;
}
sum = 0.0 ;
for ( day = 0; day < 7; day ++ )
sum + = temper [ day ] ;
printf ( “ Average is %.1f “, sum / 7.0 );
}
5
• For loop is replaced with a do while loop, which
repeatedly asks the user to enter a temperature and store
the responses in the array temper, until a temperature
of zero or less is entered.
• When the last item has been typed, the variable day will
have reached a value 1 greater than the total number of
items entered because it counts 0 (or –ve number)
which the user entered to terminate the input.
• Thus to find the number of items entered, num we
subtract 1 from day.
• num is then used as limit in the second for loop, which
adds up temperatures, and its also used as divisor of the
resulting sum.
Bound Checking
• In the previous program we have made the size of the
array to be 40. But a user decided to enter 2 months
worth of data? As it turns out, there probably would be
big trouble. How?
• In C there is no check to see if the subscript used for an
array will exceed the size of array.
• Data entered with too large a subscript will simply be
placed in memory outside the array, this will lead to
unpredictable results.
• C Language does not warn you when array subscripts
exceed the size of the array.
6
Bound Checking
• Solution:
– If there is the slightest possibility that the user might
enter too many items, we have to check for this
possibility.
Bound Checking
do
{
if ( day > = 40 ) /* beyond array end */
{
printf ( “ Buffer Size Full “ ) ;
day + + ;
break ; /* exit loop*/
}
7
Initializing Arrays
• Suppose we want to compile our program with
specific values already fixed in the array.
• This is analogous to initializing a simple variable
i.e. int marks = 45
# define LIM 5
Output
Value of array[0] = 10
Value of array[1] = 20
Value of array[2] = 30
Value of array[3] = 45
Value of array[4] = 50
8
Array Size and Initialization
• There is a change in the array definition in the previous
two programs.
9
Preprocessor Directives
• Normal program statements are instructions to the
microprocessor ; Preprocessor directives are
instructions to the compiler
• Rather than being translated into machine language,
they are operated on directly by the compiler before
the compilation process even begins; hence the
name preprocessor.
• Preprocessor directives always start with a number
sign ( # )
• The directives can be placed anywhere in a
program, but are often used at the beginning of a
file, before main ( ), or before the beginning of
particular functions.
one two
dimension dimensions
10
This program records, not one list of data as our previous programs
have, but 2 lists side-by-side. It stores the travel expenses for a no.
of secret agents who are known only by their code numbers.
11
Output:
12
row Column 0 Column 1
0 101 2331.50
1 007 8640.00
2 901 123.25
agents [2] [0] 3 904 500.60
4
agents [4] [1]
5
6
7
8
9
agents [index] [0]
13
• Output:
1 2 3 4 5 6 7 8 9 0
14
Arrays as Arguments
# define MAXSIZE 20
int max ( int [ ] , int ) ;
void main (void)
{
int list [ MAXSIZE ] , num, size = 0 ;
do
{
printf ( “ Type number: “ ) ;
scanf ( “ %d “, &list [ size ] ) ;
}
while ( list [ size ++ ] ! = 0 ) ;
size--;
num = max ( list, size-1 );
printf ( “ Largest number is %d “, num ) ;
getch ( ) ;
}
Arrays as Arguments
int max ( int list [ ] , int size )
{
int dex, max ; max = list [ 0 ] ;
for (dex = 1 ; dex < size ; dex + + )
if ( max < list [ dex ] )
max = list [ dex ] ;
rerurn ( max ) ;
}
Output
Type number: 45
Type number: 1
Type number: 15
Type number: 99
Type number: 50
Type number: 0
Largest number is 99
15
Arrays as Arguments
• In the above program there is a new element here in this
statement:
num = max ( list, size-1 ) ;
• This is call to the function max ( ), which returns the largest
number. There are two arguments to the function: the first is
the array list, the second is the variable size.
16
• To summarize, to tell the compiler the address of array, we use
the name of the array, with no brackets following it.
• Thus call to the function max() passes the address of array,
represented by the word list, to the function.
• When a simple variable name is passed to the function , it takes
the value corresponding to this variable name and installs it as a
new variable in a new memory location created by the function
for that purpose.
• Since arrays can be very large, it is better to have only one copy
of array, no matter how many functions wanted to access it.
• So instead of passing the values in the array, only address of
array is passed.
• The function then uses the address to access the original array.
• So passing an array name to function does not create a new
copy of the array.
17