Académique Documents
Professionnel Documents
Culture Documents
ARRAY
Variable
with
Multiple
Elements
What is variable?
Data_type
Variable_name;
10
First
Element
20
30
40
50
60
Last Element
Arrays
Declaration
Initialization
Input/Output
Passing arrays to functions
Declaring Array
Data_type arrayName [ arraySize ];
This is called a single-dimensional array.
The arraySize must be an integer constant
greater than zero.
Data_type can be any valid C data type.
Declaring Array
For example, to declare a 10- element
array called balance of type double, use
this statement:
double balance[10];
Now balance is a variable array which is
sufficient to hold up-to 10 double
numbers.
Arrays
A group of contiguous memory
locations used to store a series of
related values
The array name is a pointer to the
first element
All values have the same type
Individual elements of an array are
accessed via an integer index:
array[index]
Element indices start at 0, array[0]
is the first element
9
Initializing Arrays
Arrays may be initialized with a list of
suitable values
No need to specify the number of elements
for a 1D (1-dimensional) array
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
10
0
1000.00
3
4
balance
2.0
1
3.4
2
17.00
50.00
#include <stdio.h>
int main () {
int n[ 10 ]; /* n is an array of 10 integers */
int i,j; /* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100; /* set element at location i to i +
100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
Element[0]
Element[1]
Element[2]
Element[3]
Element[4]
Element[5]
Element[6]
Element[7]
Element[8]
Element[9]
=
=
=
=
=
=
=
=
=
=
100
101
102
103
104
105
106
107
108
109
Example: MonthlyRainfall
Problem: using
Rainfall Table
input month
output mean
rainfall for that
month
month
0
1
2
3
4
5
6
7
8
9
10
11
rainfall1
.c
17
rainfall1
.c
18
19
Example: IORainfall-1
#include <stdio.h>
#define NMONTHS 12
/* Store and print rainfall */
int main()
{
int data[NMONTHS];
int month;
for ( month=0; month < NMONTHS; month++ )
{
scanf("%d", &data[month] );
}
...
rainio1.c
20
rainio1.c
21
22
return 0;
rainio2.c
24
Handling Indices
Arrays have a fixed size
There is no built-in way of checking
if the supplied index is within range
We must check for valid indices
ourselves
25
27
printf("\n");
rainio3.c
28
return 0;
rainio3.c
29
rainio3.c
30
Topics
Two-dimensional arrays
Passing two-dimensional arrays to
functions
31
Two-dimensional Arrays
Each element of an array is like a single item
of a particular type
But an array itself is an item of a particular
type
So, an array element could be another
array
An array-of-arrays is called multidimensional because you need to specify
several ordinates to locate an actual element
32
Example: YearlyRainfall
year
month
0
1
2
3
4
0
30
25
35
30
30
1
40
25
45
40
35
2
75
80
90
70
30
3
95
75
80
70
90
4
130
115
100
90
150
5
220
270
205
180
230
6
210
200
135
180
305
7
185
165
140
210
295
8
135
85
170
145
60
9
80
5
75
35
95
10
40
10
60
85
80
11
45
0
95
80
30
33
NYEARS
NMONTHS
5
12
if ((0 <=
(0 <=
{
return
}
else
{
return
}
-1;
yearly1.
c
34
if (rainfall < 0)
{
printf("Year must be between 1 and %d,\n", NYEARS);
printf("and month must be between 1 and %d.\n", NMONTHS);
}
else
{
printf("Rainfall for year %d, month %d is %d mm.\n",
year, month, rainfall);
}
return 0;
yearly1.
c
35
NYEARS
NMONTHS
5
12
if ((0 <=
(0 <=
{
return
}
else
{
return
}
-1;
yearly1.
c
36
NYEARS
NMONTHS
5
12
if ((0 <=
(0 <=
{
return
}
else
{
return
}
-1;
yearly1.
c
37
if (rainfall < 0)
{
printf("Year must be between 1 and %d,\n", NYEARS);
printf("and month must be between 1 and %d.\n", NMONTHS);
}
else
{
printf("Rainfall for year %d, month %d is %d mm.\n",
year, month, rainfall);
}
return 0;
yearly1.
c
38
if (rainfall < 0)
{
printf("Year must be between 1 and %d,\n", NYEARS);
printf("and month must be between 1 and %d.\n", NMONTHS);
}
else
{
printf("Rainfall for year %d, month %d is %d mm.\n",
year, month, rainfall);
}
return 0;
yearly1.
c
39
if (rainfall < 0)
{
printf("Year must be between 1 and %d,\n", NYEARS);
printf("and month must be between 1 and %d.\n", NMONTHS);
}
else
{
printf("Rainfall for year %d, month %d is %d mm.\n",
year, month, rainfall);
}
return 0;
yearly1.
c
40
Passing Two-Dimensional
Arrays to Functions
In the function definition, the size of
the array needs to be specified
Any changes to array elements
within the function affect the
``original array elements
41
NROWS
NCOLS
3
5
twod2.c
42
NROWS
NCOLS
3
5
while (1)
{
inputEntry(table);
printTable(table);
}
return 0;
twod2.c
43
45
48
49
QUESTIONS?
Array-Bounds Checking
C, unlike many languages, does NOT check array
bounds subscripts during:
Compilation (some C compilers will check literals)
Runtime (bounds are never checked)
Initializing Arrays
Initialization of arrays can be done by a
comma separated list following its definition.
For example:
int array [4] = { 100, 200, 300, 400 };
A Simple Example
#include <stdio.h>
int main() {
float expenses[12]={10.3, 9, 7.5, 4.3, 10.5, 7.5, 7.5, 8, 9.9,
10.2, 11.5, 7.8};
int count,month;
float total;
for (month=0, total=0.0; month < 12; month++)
{
total+=expenses[month];
}
for (count=0; count < 12; count++)
printf ("Month %d = %.2f K$\n", count+1,
expenses[count]);
printf("Total = %.2f K$, Average = %.2f K$\n", total,
total/12);
return 0;
}
Multidimensional Arrays
Arrays in C can have virtually as many
dimensions as you want.
Definition is accomplished by adding
additional subscripts when it is defined.
For example:
int a [4] [3] ;
defines a two dimensional array
a is an array of int[3];
In memory:
a[0][0]a[0][1]a[0]
[2]
a[0]
a[1]
a[2]
a[3]
Initializing Multidimensional
The
following initializes a[4][3]:
Arrays
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} ,
{10, 11, 12} };
=
=
=
=
1;
2;
3;
4;
= 12;
An Example
#include <stdio.h>
#include <stdlib.h>
int main () {
int random1[8][8];
int a, b;
for (a = 0; a < 8; a++)
for (b = 0; b < 8; b++)
random1[a][b] = rand()%2;
for (a = 0; a < 8; a++)
{
for (b = 0; b < 8; b++)
printf ("%c " , random1[a][b] ? 'x' :
'o');
printf("\n");
}
return 0;
}
#include <stdio.h>
int main(){
int i;
int a[3] = { 1, 2, 3 };
printf( "a ? %d\n", a);
printf( "a[0] ? %d\na[1] ?
%d\na[2]
? %d\n", a[0], a[1],
a[2]);
printf( "&a[0] ? %d\n&a[1] ?
%d\n&a[2] ? %d\n", &a[0],
&a[1], &a[2]);
printf( "\na[0] <- 4 \n");
a[0] = 4;
printf( "a ? %d\n", a);
printf( "a[0] ? %d\na[1] ?
%d\na[2]
? %d\n", a[0], a[1],
a[2]);
printf( "&a[0] ? %d\n&a[1] ?
%d\n&a[2] ? %d\n\n", &a[0],
&a[1], &a[2]);
Arrays as Function
In this program, the array
addresses
(i.e., the values
Parameters
of the array names), are
An
Example
-Sorting
int main()
void mysort(int a[ ],int size)
{
int i;
int tab[10] =
{3,6,3,5,9,2,4,5,6,0};
int i,j,x;
for(i=0; i<size; i++)
{
for(j=i; j>0; j--)
{
if(a[ j ] < a[ j-1])
{ /* Change the order of
a[ j ] and a[ j-1] */
x=a[ j ];a[ j ]=a[ j-1]; a[j1]=x;
}
}
}
for(i=0;i<10;i++)
printf("%d ",tab[i]);
printf("\n");
mysort(tab,10);
for(i=0;i<10;i++)
printf("%d ",tab[i]);
printf("\n");
return 0;
}