2. ARRAYS: ARRAY NOTATION AND REPRESENTATION,
MANIPULATING ARRAY ELEMENTS, USING MULTI-
DIMENSIONAL ARRAYS.
STRUCTURE, UNION, ENUMERATED DATA TYPES.
UNIT-4 TOPICS
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
2
3. UNIT-4 ARRAYS
An arrayarray is a named collection of homogeneous items in which
individual items are accessed by their place within the collection.
An arrayarray is a collection of variables of the same type that are referred
to by a common name.
Eg.
product part numbers:
int part numbers[] = {123, 326, 178, 1209};
student scores:
int scores[10] = {1, 3, 4, 5, 1, 3, 2, 3, 4, 4};
characters:
char alphabet[5] = {’A’, ’B’, ’C’, ’D’, ’E’};
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
3
4. INITIALIZATION OF ARRAY
Array – a set of elements all of the same type stored contiguously
in memory – e.g.,
int A[25]; // 25 integers
struct Str B[15]; /* 15 objects of
type struct Str */
double C[]; /* indeterminate #
of doubles */
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
4
5. ARRAY NOTATION
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
5
An array of size N is indexed from zero to N-1
79 87 94 82 67 98 87 81 74 91scores
The entire array
has a single name
Each value has a numeric index
This array holds 10 values that are indexed from 0 to 9
6. MANIPULATING ARRAY ELEMENTS
Some other examples of array declarations:
float[] prices = new float[500];
boolean[] flags;
flags = new boolean[20];
char[] codes = new char[1750];
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
6
7. 2D ARRAY
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
7
A one-dimensional array stores a list of elements
A two-dimensional array can be thought of as a table of
elements, with rows and columns
one
dimension
two
dimensions
8. 3D ARRAY
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
8
An array can be declared with multiple dimensions.
2 Dimensional 3 Dimensional
Multiple dimensions get difficult to visualize graphically.
•
int [][][] table3 = { { {1,2}, {3,4} },
{ {5,6,7} , {8}, {9,10}
}
};
9. USING MULTI-DIMENSIONAL ARRAYS
An array can have many dimensions – if it has more than one
dimension, it is called a multidimensional array
Each dimension subdivides the previous one into the specified
number of elements
Each dimension has its own length constant
Because each dimension is an array of array references, the
arrays within one dimension can be of different lengths
these are sometimes called ragged arrays
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
9
10. MULTIDIMENSIONAL ARRAY
Arrays with more than one index
number of dimensions = number of indexes
Arrays with more than two dimensions are a simple extension of
two-dimensional (2-D) arrays
A 2-D array corresponds to a table or grid
one dimension is the row
the other dimension is the column
cell: an intersection of a row and column
an array element corresponds to a cell in the table
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
10
12. STRUCTURE:
A structure is a collection of one or more components (members).
Structures are called records in many other programming languages.
Members are known as fields.
Declaring a structure:-
struct {
char name[25];
int id, age;
char sex;
} s1, s2;
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
12
13. INITIALIZING A STRUCTURE:-
struct {
char name[25];
int id, age;
char sex;
}
s1 = { "Smith, John", 2813, 25, 'M'},
s 2 = { "Smith, Mary", 4692, 23, 'F'};
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
13
14. ACCESSING THE MEMBERS OF A STRUCTURE:
The members of a structure are accessed by writing first the name
of the structure, then a period, then the name of the member:
struct student {
char name[25];
int id, age;
char sex;
} s;
strcpy(s.name, "Doe, John");
s.id = 18193;
s.age = 18;
s.sex = 'M';
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
14
17. INITIALIZATION OF STRUCTURE ARRAYS
Structure arrays are initialized by enclosing the list of values of its
elements within a pair of braces.
Example:
struct unit
{ char ch ;
int i ;
} ;
struct unit series [3]=
{ (‘a’, 100) (‘b’, 200) (‘c’, 400)
};
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
17
18. UNION:
A union is similar to a structure, except that its members are
overlaid (located at the same memory address).
A union is like a structure in which all members are stored at the
same address.
Example:
union {
int i;
double d;
} u;
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
18
19. ACCESSING THE MEMBER
The members of a union are accessed in the same way as members of
a structure:
u.i = 15;
or
u.d = 8.89;
Since u.i and u.d have the same memory address, changing the value
of one alters the value of the other.
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
19
20. Difference Between Structure & Union
08/11/18ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
20
// declaring structure
struct struct_example
{
int integer;
float decimal;
char name[20];
};
// declaring union
union union_example
{
int integer;
float decimal;
char name[20];
};