Lecture 14: Problems with Lots of Similar Data What is an Array? An array is a data structure consisting of related data items.

Download Report

Transcript Lecture 14: Problems with Lots of Similar Data What is an Array? An array is a data structure consisting of related data items.

Lecture 14: Problems with
Lots of Similar Data
What is an Array?
An array is a data structure consisting of related data
items of the same type.
memory
Stored in a group of memory locations
Defining arrays Name of the array
int arrayName[ 100 ];
Specifying the type of each element
Examples
int a[5];
float b[120], x[24];
The number of the elements
a[0]
a[1]
10
a[2]
15
a[3]
a[4]
3
5
23
What is an Array?
Defining arrays (cont.)
Using a symbolic constant for the size of an array.
Making programs more scalable.
#define SIZE 100
Not a variable, cannot assign it a value
int a[ SIZE ];
in an executable statement.
float b[ SIZE ];
Using only uppercase letters for symbolic constant
names.
Referring to Array Elements
Formally called a subscript
Format
or an index
arrayName[ position number ]
First element at position 0
The i-th element of array a is referred
to as a[i-1]
A subscript must be an integer or an
integer expression. Ex.
(k = 2, j = 1)
a[k+j] += 12;
The expression is evaluated to
determine the subscript.
Name of array (all
elements of this
array have the
same name a)
memory
a[0]
a[1]
10
a[2]
15
a[3]
a[4]
3
5
subscript of
the element
within array a
23
Referring to Array Elements
An array a of size of n has the following
elements:
a[0], a[1], a[2], …, a[n-1]
Array elements are like normal variables
printf(“%d”, a[0]*a[3]);
x = (a[2]+a[3])/2;
For a defined array without initialization,
the values of the elements are
undefined.
Avoid to referring to an element outside
the array bounds.
C has NO array bounds checking to prevent the computer
from referring to an element that does not exist.
memory
a[0]
a[1]
10
a[2]
15
a[3]
a[4]
3
5
23
Initializing the Array
Using a loop to initialize the array’s elements
Initialized to a uniform value
Initialized to different values - using scanf
Initializing an array in a definition with an initializer list
Defining an array followed by an equals sign and braces, { },
containing a comma-separated list of initializers.
int n[5] = {1, 2, 3, 4, 5};
If not enough initializers, rightmost elements become 0.
int n[5] = {1, 2}
int n[5] = {0} --- all elements are 0.
If too many initializers, a syntax error occurs
If size omitted, # of initializers determine the size
int n[ ] = {1, 2, 3, 4, 5, 6};
6 initializers, therefore 6 element array.
Practice Question
Q. What is the output of the following program?
#include<stdio.h>
int aFunc( int );
int x = 1; /* global variable */
int main( void )
{
int y = 0;
A.
B.
C.
D.
100 100
10 100
10 10
100 10
x = 10;
y = aFunc( x );
printf(”%d %d\n", x, y);
return 0;
}
int aFunc( int x)
{
x *= 10;
return x;
}
Solution: B
Practice Question
Q. What is the output of the following code fragment?
#define SIZE 5
int aray[SIZE] = {2, 3, 4};
printf(“%d\n”, aray[1] + aray[2] + aray[3]);
A.
B.
C.
D.
2
5
9
7
Solution: D
Practice Question
#define SIZE 5
int aray[SIZE] = {2, 3, 4, 5};
Q. What is the value of aray[5]?
0
4
Not sure
5
Solution: C
A.
B.
C.
D.
Q. What is the value of aray[1]?
2
0
Not sure
3
Solution: D
A.
B.
C.
D.
Practice Question
Q. To initialize an array to be {1, 2, 3, 4, 0}, which of the following is WRONG?
A.
B.
C.
D.
int ary[ ] = {1, 2, 3, 4, 0};
int ary[5] = {0};
int k;
for (k = 0; k < 4; k++)
ary[k] = k+1;
int ary[5];
ary = {1, 2, 3, 4, 0};
int ary[5] = {1, 2, 3, 4};
Solution: C